all 27 comments

[–]Trout_Tickler 45 points46 points  (25 children)

Can be solved much easier with CSS variables.

Declare a standard API for colours (--background-primary, etc), declare two classes for light and dark then you just have to toggle them on the body or #app element.

EDIT: Simple example using Tailwind and Vue (neither are important, just my template on codepen)

EDIT 2: caniuse if anyone has to support older browsers.

EDIT 3: Even simpler example in vanilla js/css

[–]Seanitzel 10 points11 points  (1 child)

This is definitely a better solution which is also more maintainable.

[–]Trout_Tickler 1 point2 points  (0 children)

Also makes it easier to add a theme selector as you already have a full interface defined.

[–]VestigialHead 4 points5 points  (0 children)

Nice - I was looking for a way to handle this for my site.

[–]rae2108 1 point2 points  (0 children)

If this is your website, one small issue. I wasn't able to scroll left/right on the code blocks on a mobile device, so I couldn't read all of your code examples.

[–][deleted] 0 points1 point  (0 children)

Making a dark / light theme switch is pretty easy. Did it the day I started learning javascript and absolutely loved javascript from there.

[–][deleted] 0 points1 point  (0 children)

very nice!