DEV Community

Cover image for No Fuss Dark-Mode Toggle with React & Styled-Components! 🌞↔️🌖

No Fuss Dark-Mode Toggle with React & Styled-Components! 🌞↔️🌖

Chris Hansen on August 18, 2021

Have you noticed dark mode is pretty standard lately? Well, that's because it's badass! Let's achieve this new standard of awesome by utilizing ...
Collapse
 
hyggedev profile image
Chris Hansen • Edited

That's great advice! I actually was planning on storing a userSettings in local storage but I wanted to make it as absolute barbones as possible 👌 And that's really cool. Is that the exact snippet for any OS? In other words that would take my macs theme preference and apply it to the browser? Same for a windows user? How about if my OS preferred theme is set to detect time of day still work!? 🤯

Collapse
 
midorun profile image
Dmitriy Vorozheykin • Edited

Hey, Chris, did you know that u have no need to pass theme prop to each component?
Look for ThemeProvider component from styled-components, it gives you opportunity to wrap you whole JSX component with provider which will pass theme prop to all styleds via context, so u will need to pass your theme only to themeProvider

Collapse
 
hyggedev profile image
Chris Hansen

Absolutely! I wanted to make this as easy as possible! But you are the second person to mention this, so I think I'm just gonna have to continue this, possibly as a series! May have to give that a shot 😅 Thanks for your input, it is good advice 🤘

Collapse
 
gdenn profile image
Dennis Groß (he/him)

I just came to tell you how badass your caption image looks like. That is really amazing :)

Collapse
 
hyggedev profile image
Chris Hansen

Hahaha! Right on, thanks a lot! I'm super into the cyberpunk genre and it's neon colors, so I guess it worked out. 👌✌️

Collapse
 
gdenn profile image
Dennis Groß (he/him)

Something about this caption reminded me on "Stranger Things". And I ABSOLUTELY LOVE that series. :)

Thread Thread
 
hyggedev profile image
Chris Hansen

Couldn't agree more! The production, cast... top notch. But when the show transitions into the golden age of arcades.. all that eye candy 😎

Collapse
 
adelekand profile image
David Adelekan • Edited

Isn't it a better approach to have a theme provider at the top level component that would take the theme object as a prop based on the value of isDarkMode, instead of having to do the comparison for each of the components. This is not sustainable for a large project that have many components.

Edit:
I wrote this comment not knowing the suggestion has already been made

Collapse
 
hyggedev profile image
Chris Hansen

Well in my honest opinion you are 💯 percent right! My aim was to basically introduce styled components, and to have the least resistance for entry for beginners. Info have a goal of updating this into a series and introducing React context, dark mode hooks, or themeprovider. Thanks for dropping by!

Collapse
 
kieudac201 profile image
KieuDac201

Thank you

Collapse
 
hyggedev profile image
Chris Hansen

✌️😜✌️

Collapse
 
moghaazi profile image
Ahmad

Awesome and smooth.

Collapse
 
hyggedev profile image
Chris Hansen • Edited

Hey 👋 Glad ya dig it! Thanks for dropping by ✌️

Collapse
 
andemosa profile image
Anderson Osayerie

Thank you very much for this informative article

 
hyggedev profile image
Chris Hansen • Edited

Amazing dude 🤘 I'ma look more into that D:

Collapse
 
mateusmarquezini profile image
Mateus Marquezini

Nice post, Chris!

Collapse
 
hyggedev profile image
Chris Hansen

Hey! Thanks a lot! ✌️

Collapse
 
samuelnarciso28 profile image
Samuel Narciso

Thank you so much, I will to use in my blog 😄😄

Collapse
 
hyggedev profile image
Chris Hansen

That's awesome! ✌️🤘

Collapse
 
goodok21 profile image
Alexander Eric

Sorry, but it’s bad approach, just to pass props to each component about selected mode… at least you have to use styled theme context or css variables or whatever, but not like this

Collapse
 
hyggedev profile image
Chris Hansen

This is definitely not the end goal. For beginners who are testing the waters, this is the easiest way imo to get something up and running asap. Context and Local storage would make this much better. But as for styled-components, this totally works!

Collapse
 
goodok21 profile image
Alexander Eric

You want to provide context about dark mode? And get this context state in components and then pass as props again to styled components? ;) just use styled-components theme, and to persist state you can use local storage for sure 😉

Beginners need to learn good patterns - this is the most important thing, imho 🙂

Thread Thread
 
hyggedev profile image
Chris Hansen

Update coming soon 😉