How to build custom components based on <Box>
?
#1418
-
Hey everyone 👋 First of all, thank you all for the effort you're putting into this project. It's helped me immensely and even after having read a lot of PRs, issues, and discussions on here, I'm still not grasping the bigger picture. So kudos! Now, I have read about the import { Box } from '@theme-ui/components'
const Text = React.forwardRef(function Text(props, ref) {
return (
<Box
ref={ref}
as="p"
sx={{
// custom styles
}}
/>
);
}); I tried a lot of different versions of 1.
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Happy New Year @gregoralbrecht
This sounds like its Emotion context version mismatch (new Theme UI depends on Emotion 11), and if it isn't this then it's serious. Do you have a repro?
It works for me? I'll send you a CodeSandbox in a bit, and we'll try to troubleshoot your issue.
Well, Theme UI doesn't export a styled component factory, and I think it won't. You can build it on top of sx prop and |
Beta Was this translation helpful? Give feedback.
-
Okay, so one TypeScript error you can see is
Here's a CodeSandbox for you. Sidenote: The root cause of the problem is the fact that This doesn't work because.
type Ref<T> = { bivarianceHack(instance: T | null): void }["bivarianceHack"] | RefObject<T> | null;
interface RefObject<T> {
readonly current: T | null;
} |
Beta Was this translation helpful? Give feedback.
Okay, so one TypeScript error you can see is
Here's a CodeSandbox for you.
Ping me if you have any more problems :)
Sidenote:
@theme-ui/components
already exports Text. If you want to build your own primitive components you can ignore it entirely, and just use@theme-ui/core
. You can build your own lightweightBox
component with JSX pragma or manually usingjsx
imported from@theme-ui/core
.The root cause of the problem is the fact that
React.forwardRef
infersprops: { children?: ReactNode }, ref: Ref<unknown>
, so you assignedRef<unknown>
toRef<HTMLDivElement>
.This doesn't work because.