Variant on components
See original GitHub issueAt the moment, this is the best way I found to render components based on state:
const useStyles = ({ variant }) => ({
fontWeight: 'bold',
py: 2,
px: 4,
rounded: 'full',
fontSize: 'xs',
d: 'inline-block',
...variant === 'full' && {
bg: 'tomato',
color: 'white',
},
...variant === 'outlined' && {
bg: 'tomato',
color: 'white',
},
});
const stylesProps = useStyles(props)
return <Box {...stylesProps} />
Is this a good pattern ? Is there any better solution ?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Creating and organizing Variants
This guide will cover some useful tips and practical examples for getting the most out of variants. If you have existing components you'd...
Read more >Component Variants
Components use custom variants to allow you to completely customize the look and feel of each component. The following table lists each component's...
Read more >Framer Learn: Component Variants
Variants are different variations of your components. You can also use them for visual transitions. The Hover Variant. In the isolation view, select...
Read more >Variants
Use the variant API to apply complex styles to a component based on a single prop. This can be a handy way to...
Read more >When you should use variants VS creating separate ...
Variants allow all a component's, well, variations, to be accessed in any order. You no longer have to drill through a half dozen...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @bulby97,
Your solution works for now. I understand it’s a bit painful to handle variants that way.
We’re solved this permanently in the next release. Let me give you a glimpse:
sx
prop in any component@bulby97, it’s mostly to have one source of truth for component styles and variants.
This means you can simply import any Chakra components, and override their “global” style from the theme or even make your own styles.
It improves the themeability experience, and ensure all apps built with Chakra don’t look the same 😀