I'm trying to add a simple light/dark mode toggle to my Jekyll site.
The way I have it setup now is that the body has a dark
class that is switched to a light
class on toggle, and the theme is persisted in localStorage
and loaded whenever the user goes to a new page. The SCSS simply has selectors for .dark
and .light
that will show the corresponding color scheme.
The problem with this approach is that my default theme is dark
, so if the theme is set to light
there's a very clear .2 second flash as the classes switch from dark
to light
when I load a new page.
I've tried solving the issue by setting the body's visibilty
to hidden
then making it visible
again once the switch has been made, but unfortunately this approach introduces another annoying flash/lag, and there's no good way to stop it from flashing on every page load.
Some of the potential solutions I've thought of, but don't know if they are possible or how to implement them are:
Have Jekyll somehow read from
localStorage
and change the class of the rendered HTML based on that (probably the best solution, but also probably not possible)Somehow have Jekyll produce two separate style sheets from the SCSS and use JS to select the correct one
Would appreciate any insight on how to do these things or potential alternate solutions, thanks!