question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Variant on components

See original GitHub issue

At 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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

24reactions
segunadebayocommented, Feb 24, 2020

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:

  1. You will be able to use the sxprop in any component
  2. You can define a component schema to support different variants, sizes and color schemes.
// step 1: define component schema in theme.components
const theme = {
  components: {
    MyComponent: {
      // you can define variants
      variants: {
        outlined: {}, // your style props
        full: {}, // your style props
      },
      // you can also define sizes
      sizes: {
        small: {}, // your style props
        big: {}, // your style props
      },
    },
  },
}

// step 2: create a chakra component
const MyComponent = createChakra("button", { themeKey: "MyComponent" })

// step 3: Just use the component :)
<MyComponent variant="outlined" size="small"/>
5reactions
segunadebayocommented, Mar 20, 2020

@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 😀

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found