Theme management with Solid
I would like to have a Dark and Light Mode on my page. For this I use Solid in combination with Astro.
How would you proceed now?
My current status is that I have created a wrapper object "Scaffold" on the Astro page.
Scaffold is a solidjs component which looks like this:
There I import a "signal" for the theme:
12 Replies
Now my question is how would you apply a style based on the theme. I like to work with Sass/Scss very much.
By default, the system theme should always be used and the user should be able to change the theme manually using a button.
Already created a _config.scss for the theme colors.
I'd use a combination of CSS variables, a media query and JS to change on button click, like so:
And some JS, which sets the
data-theme
attribute on the root (document.querySelector("root")
) when the button is clicked.
Two tips: Use the dataset
attribute instead of setAttribute("data-*")
and you don't need a store here. Stores are for nested state that needs to get tracked granuarly
And you could also store the theme in localstorage
to persist itAnd how would you use sass functions on css-variables? For ex.
darken(var(--primary), 10%)
does not workI would set the vars to the darkened value:
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
What bothers me is that it flickers a bit at first load.... However, I do not want that
Is your default theme dark or light?
my default theme is dark. but someone's default theme might be light
Does it flicker without the localstorage stuff too?
I have not tried it on my end. @RobBobs website just flickered.
You should try on your end, as far as Isee @RobBobs site has no media query in CSS.
And if the default theme of your site is light, then you should of course call
setTheme("light")
in the else clause. Or, better, just do nothing there and let the CSS do the stuff as long as the user doesn't select a theme activelyUnknown User•2y ago
Message Not Public
Sign In & Join Server To View