PricingBlog

Component variants

  • riisud-1460757196710215882

    Riisud

    19 hours ago

    So i'm trying to figure out what is the best and easiest way to maintain a component with variants?

    So for example i want to create a button component with variant options (primary, secondary, ghost, ...) i get it to work with creating classes and then applying different styles to those classes + styling pseudo classes attached to each variant class.

    But now i was trying it out with style variables and i got this to work as well. With style variables i do not need to use classes anymore but i just want to know what's the agreed upon way of doing it?

    Problem with style variables is that i can not use my theme color variables. So if i ever want to update my primary colors i am forced to go update every style variable i ever used that hex code in. If i could get my theme color variables to work with the style variables that would'nt be a problem anymore. Unless style variables is the wrong way of creating variants and i should stick with classes???
    1
  • lucasg-1460760532981252188

    Lucas G

    19 hours ago

    There is no "agreed upon way" really
  • Whatever works for you
  • roccles-1460760560328249505

    chrisr

    19 hours ago

    You're not limited to using hexcodes in style variables, you can reference your theme styles just like any other CSS variable.

    We're also using a color attribute so that you'd be able to change the color of the component. Especially useful for setting colors for different states (e.g., active, hover) with a single attribute.
    For example, bg-color would be var(--success-1) and hover could be referencing var(--success-2).
    1460760560130986233-image.png
  • lucasg-1460760588090081494

    Lucas G

    19 hours ago

    Also, you can use theme variables in style variables
  • riisud-1460761666571731026

    Riisud

    19 hours ago

    Ooh ok, i did not know i had to use var(my variable name) for it to work. So no agreed upon way but style variables is cleaner and better maintainable then having a bunch of classes and styling those?
  • riisud-1460762057585594503

    Riisud

    19 hours ago

    With the classes i already had 10 different styles targetting each class, each hover for that class etc etc but with style variables i can keep it to only a few.
  • riisud-1460762627394506873

    Riisud

    19 hours ago

    i was trying to look at packages (shoelace) how they dealt with variant buttons but it seems like they import stylesheets so kind of defeats the purpose, i want to try to do as much as possible within Nordcraft itself.
  • lucasg-1460765024032260209

    Lucas G

    18 hours ago

    You can look at the Spark One starter as well
  • It's a native build
  • riisud-1460770068563624170

    Riisud

    18 hours ago

    Yea, i know about spark one but i really want to learn to do it myself. I don't want to use a template and only use what it provides, i want to build my own thing and see how far i get with it.
  • lucasg-1460773578197831771

    Lucas G

    18 hours ago

    Spark Core is the packge, Spark One is a starter that uses Spark Core. You don't have to use Spark One but you can look at how they implemented variants
  • Just clarifying
  • In case you're not using Spark Core though, I highly recommend you do. Implementing a11y, keyboard management, floating-ui, etc yourself is unnecessary
  • riisud-1460775782585602072

    Riisud

    18 hours ago

    I tried it once before and i know the package and template are very well made. I'll look up the documentation tommorow to figure it out. Thanks for your help today Lucas.
    👍1
  • Tod-1460775785177809048

    Tod

    18 hours ago

    Great job @Riisud! Your contribution to the Nordcraft Community just made you advance to Community Level 1! 🌲