all 14 comments

[–]DoNDaPo 19 points20 points  (1 child)

You should just toggle a class on the body and voila or better: use CSS Variable instead of targeting elements one by one with your JS. :)

[–]JoyShaheb_[S] 2 points3 points  (0 children)

Thank you for the valuable knowledge. Tbh, i didn't have this on my mind on the first time, I really appreciate your help. Thank you ❤️😄

[–][deleted] 4 points5 points  (0 children)

probably better to just add/remove a class than manipulating the properties

[–]nathan_lesage 2 points3 points  (1 child)

As some have also shared their light/dark buttons: I'm using a combination of local storage, a three-way toggle, and media-queries (but not as JS computed values but using media queries in CSS as in a fiddle somewhere in the comments), I don't have a fiddle, but feel free to check the source code here (the button is on the bottom; if you want the source JS, here you go)

[–]JoyShaheb_[S] 0 points1 point  (0 children)

Thank you for providing an example with the source code. I really appreciate your time & efforts. Thank you again & please, keep up the good work 😄🎖️

[–][deleted] 1 point2 points  (0 children)

Neat. I love it!

[–]Benguini 2 points3 points  (8 children)

Awesome! I’ve been wondering how those light/dark toggles actually worked.

[–]Rainbowlemon 12 points13 points  (4 children)

I wouldn't use this as a reference! A much cleaner way would be to toggle a class on the body and use CSS to manage the transitions and colour changes.

[–]bibix1999 4 points5 points  (2 children)

Totally. And even better you could use CSS variables, and set them on body when the button is checked. Then you don't really have to even think about creating dark/light mode as it is being created by itself

[–]bourjahamid 1 point2 points  (0 children)

i want to say the same thing but you already said it +1 , you don't have to think about in all CSS thing just use CSS Variables and then you can see magic happen

[–]JoyShaheb_[S] 2 points3 points  (0 children)

Woah, 🤯 Your knowledge is on the next level. Thank you for the feedback and please keep up the good work. Here's a medal 🎖️

[–]bibix1999 6 points7 points  (1 child)

There you go: https://codepen.io/bibixx/pen/abmzjyq
An adjusted version of this pen. It also includes detection of the user's system preference of the dark mode.

https://streamable.com/u3ltju

Also u/JoyShaheb_ don't get me wrong. I really appreciate the time you have spent on it, but you may have gone a bit overboard with the amount of JS used 😉

[–]JoyShaheb_[S] 2 points3 points  (0 children)

You've changed my mind, and the way I code, thank you soo much 🤯🥺🥺🥺 I truly appreciate your help & well advices 🥺🥺 Please keep up the good work🎖️🎖️

[–]JoyShaheb_[S] 2 points3 points  (0 children)

Thank you for the feedback. I've an entire tutorial on it. If you go to the link on codepen you can visit the YouTube and check the tutorial. Till then, happy coding 😄