-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CSS: cleaner pattern for variable use #488
Comments
I think I would feel more comfortable with the pattern "Set variable defaults on parent DOM :root". It's the easiest to follow in my opinion and the easiest to communicate to users. Regarding the downside point, "We have to defince the default values outside each addon", I think we could have a As a side note, I saw you mentioned:
and then you used Edit: oh, should we keep using |
Yeah considered that but it's still a separate file. I think we could avoid a secondary stylesheet by exporting a Lit stylesheet from each addon and combining them all into adoptedStyleSheets at the index though.
|
Here's an example using This feels like the best of the first two examples. I like that this keeps all CSS variables defined in the exact place we'll be using them, inside each addon CSS file, and still supports overriding at the parent |
Here are a couple options for cleaning up our CSS variable patterns. I started to experiment with some of these to see how it felt to override them and author rules around them. I wanted to note this before moving on.
Current issues
font-family: var(--readthedocs-font-family, "Lato", ...)
in multiple places.--addons
prefixed variables inconsistently, so the pattern feels unclear but also redundant.--readthedocs-flyout-dd-font-size
.calc()
instead ofem
rules makes variable values probably more confusing than avoidingem
helps.em
feels safer in a shadow DOM, sorem
doesn't feel as necessary as working in the parent DOM.Solutions
Set variable defaults on parent DOM
:root
This would be a stylesheet added to
document.adoptedStyleSheets
:And when we use the variable, we don't need to set a default value now:
And users can override these variables with:
--addons
variablesUsers set defaults on our elements, not :root
Another approach would be to set the variable defaults on each addon Element
:host
:This would differ from the approach above in that users would override variables on elements, not on the parent DOM
:root
:This works because the parent DOM
readthedocs-flyout
is the same element as the shadow DOM:host
. We can't do the above pattern if we instruct users to override variables at:root
, this is the reason we have the--addons
prefixed variables.:root
, which is also a fairly common pattern:root
while others can't be overridden. If users try to set variables at:root
styles will seeming randomly not work.Standardize local scoped variables
This is what we started to do so far. We set defaults at the addons' CSS file, but use a secondary variable name to give a locally scope variable to the addon (currently these are the
--addons
prefixed variables).A couple things we could do to make this pattern better:
--addons
prefix variables so it's less redundant and more clear these are local variables ----local-flyout-font-size
,--flyout-font-size
,--local-font-size
, etc.--readthedocs-font-size
and--readthedocs-font-family
.:root
stillvar(--readthedocs-flyout-..., var(--readthedocs-..., "and still sometimes a default value too"))
The text was updated successfully, but these errors were encountered: