Replies: 2 comments 1 reply
-
Thanks for outlining this @mejiaj ! Maybe a good thing to check for the styles section of our landscape analysis. I'm curious what approaches other Design Systems are taking for CSS structure. |
Beta Was this translation helpful? Give feedback.
-
I think this is a misunderstanding. What you describe is generally a recommended approach, but the options and details can vary a lot based on the amount of customization that you want to support. Here are some examples to consider:
This is probably obvious, but custom properties provide targeted, property level customization whereas CSS parts allow users to customize any properties for a given, otherwise not selectable, element. Given that it looks like you do want to support at least theme and component levels, consider a setup like this using a conceptually private property: :host {
--_usa-banner-background-color: var(--usa-banner-background-color, var(--usa-base-lightest));
}
.banner {
background-color: var(--_usa-banner-background-color);
} This way the user can customize the Providing the default value here can be done a few ways, but I would suggest not baking in hardcoded values into each element as your example shows. This is bloated and hard to maintain. Instead, consider defining properties with |
Beta Was this translation helpful? Give feedback.
-
Curious to see approaches and hear thoughts on shared styles across web components.
Problem
We want to set global theme styles, such as colors, spacing, and font tokens.
Example
And re-use that theme link color in another component, such as banner.
The lit recommendation
Creating a module that exports tagged styles.
Source: https://lit.dev/docs/components/styles/#sharing-styles
The problem here is that the variables can't be attached to
:root
and instead go to the component (:host
). This could lead to redundant/duplicate code. As well as making it difficult to theme because it'll be hard to sift through all the other variables. There's no clear hierarchy from semantic i.e. "primary color" to component token i.e. "banner background color".Alternatives
One possible alternative is creating a CSS file that has global tokens attached to
:root
and using that in components.This approach allows us to set a token hierarchy, re-use tokens, and users a familiar way to use tokens.
Possible downsides
A possible downside is it's now a new dependency. It's not a "recommended" lit approach. CSS props are mentioned, but variables are attached to
:host
1Questions to answer
Footnotes
https://lit.dev/docs/components/styles/#customprops ↩
Beta Was this translation helpful? Give feedback.
All reactions