theme system
Hey, what would ya'll recommend as a good method to go about creating themes for a site? I had the idea of applying variables to
html
and having a data attribute toggle along with prefers-color-scheme
(or lightdark()
) e.g.
I'm not sure if this is a good maintainable way of doing something like this though. I'd appreciate any insight. Thanks in advance22 Replies
good way to go about it imo
You could make the tokens a bit better maybe and not so specific.
i would do something better
your implementation has a possible bug, where stuff is a mix of dark and light
I typically override the root
š¤
what you can do is something like this:
or something like that
also, you only define the colors in 1 place
i hope it works, because i didnt try it
š¤ doesnt seem to work, so, i will explore
there, updated it
here's a more refined example:
Because of the prefers color scheme? Like if someone has prefers color scheme dark and the site is default to the data attribute light it could conflict?
yeah, and there's the risk that some stuff is light and other stuff is dark
also, you're repeating yourself
How so?
you're doing the same work twice
oh i see. i was thinking that way because one is the browser preference and the other is a toggle switch
this works exactly the same: just don't add the
data-theme
attribute
and you don't repeat yourself
and when you add the data attribute, it respects the attribute
and it's easier to extend as well
by the way, you have to replicate the styles to the ::backdrop
element, if you use dialogs or videos
::backdrop
is special - it doesn't inherit any propertiesoh okay i see. in your solution, what are the positives of the
@container style()
declarations? couldn't you do this? or is that unecessary repetition?
the positives: you dont repeat yourself
by not repeating yourself, you also don't make any mistakes
imagine you have 30 variables
now, you need to update the same value twice per theme
So are you setting the html El as a container-type for this?
im just doing this
nothing else
that's the entire css
So if no container is defined, @container refers to the html
yes, the body and ::backdrop
can you explain theses lines ? @container style(--theme: white) {
i don't quite get it... even if i kind of understand what it does
it's a container query that checks the value of the custom property, and compares to the value given
think of this as an if that checks if --theme is equal to white
i can't find the mdn doc of style() function -_-
https://caniuse.com/css-container-queries-style
is this it ?
yes it is
you can also just google "query containers with style" and it should show the mdn page
you're welcome