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.

defaultVariant styles inherited in other variants

See original GitHub issue

if i add some buttons variants to my theme and declare one of these as the defaultVariant for my Button component, styles from the defaultVariant must be manually reset or overridden in my other variant options.

for example, take the following excerpt from my theme object:

  buttons: {
    medium: {
      borderRadius: "$xlarge",
      py: 14,
      px: "$8",
    },
    circle: {
      borderRadius: "$rounded",
      alignItems: "center",
      justifyContent: "center",
      width: 40,
      height: 40,
    },
  },

with the following component definition:

const Button = styled(Pressable, {
  themeKey: "buttons",
  defaultVariant: "medium",
})({
  alignSelf: "center",
});

when rendering <Button variant={'circle'} /> it will have the py and px padding from the medium variant. i have to add px: 0, py: 0 to my circle variant to prevent this.

i didn’t expect it to inherit this way. it seems like it could be a bug because if i do not set a defaultVariant then no inheriting occurs. let me know if my expectations are misplaced here tho.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jjenzzcommented, Sep 14, 2022

I have to admit, I’ve found myself using the variant prop somewhat infrequently over time. I prefer to see the styles closer to the components.

i was also unsure about declaring styles in theme at first as I am used to stitches where variants are declared as part of the component, however, your approach has opened my eyes somewhat in that it could allow designers to more easily have control over visual styles. the components themselves could handle functional styles only.

i’ve been trialing going all in on this approach and enjoying it thus far!

What if we added an fallbackVariant option, which gets overridden by variant used on the component itself? It’s a little confusing to add more to the API, but I think this more clearly states that variant will “fallback” if nothing is provided.

this would be super helpful and would suit my use-case perfectly 🙏

1reaction
nandorojocommented, Jul 28, 2022

thanks for the issue! i see, you’re expecting variant || defaultVariant. i’ll look into this for you and see what’s up.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Should base rules override inherited base variants? · Issue #629
One way to get around this, is by extending the color variant in RedBox and overriding the defaultVariant there. But seems like a...
Read more >
Variants - Stitches.dev
In the case of needing to set styles of a variant, based on a combination of other variants, you can use the compoundVariants...
Read more >
Create interactive components with variants - Figma Help Center
Variant properties are a type of component property specific to variants and component sets. You can add as many properties and values as...
Read more >
font-variant - CSS: Cascading Style Sheets - MDN Web Docs
The font-variant CSS shorthand property allows you to set all the font variants for a font.
Read more >
Variants - Styled System
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 >

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