Toggle Dark Mode in React

This page summarizes the projects mentioned and recommended in the original post on dev.to

SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.
surveyjs.io
featured
InfluxDB - Power Real-Time Data Analytics at Scale
Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
www.influxdata.com
featured
  • Portfolio2.0

  • Next, I added the toggle component to my navigation bar component. I styled the toggle following Chris Bongers’ Tutorial based on Katia De Juan’s Dribbble. Then I adjusted the size and flipped it to default to dark mode. While this toggle is so cute that you could die, this tutorial will work with any or clickable . First, I set up the basic JSX, the local state, and a variable to hold the theme we get from localStorage: import React, { useEffect, useState } from 'react'; import '../styles/toggle.css'; import { setTheme } from '../utils/themes'; function Toggle() { const [togClass, setTogClass] = useState('dark'); let theme = localStorage.getItem('theme'); return (

    ) } export default Toggle; Enter fullscreen mode Exit fullscreen mode When a user clicks the toggle, I want the theme on the page to change and the toggle to change with it. I added the imported setTheme() function and setTogClass() from the local state to a handleOnClick function. You can see where it is passed to the clickable part of the toggle in the JSX above. const handleOnClick = () => { if (localStorage.getItem('theme') === 'theme-dark') { setTheme('theme-light'); setTogClass('light') } else { setTheme('theme-dark'); setTogClass('dark') } } Enter fullscreen mode Exit fullscreen mode I used this component’s useEffect() to make sure the local state togClass always loads with the correct theme. useEffect(() => { if (localStorage.getItem('theme') === 'theme-dark') { setTogClass('dark') } else if (localStorage.getItem('theme') === 'theme-light') { setTogClass('light') } }, [theme]) Enter fullscreen mode Exit fullscreen mode Because my toggle is a checkbox, the dark theme should show the unchecked (moon) state and the light theme should show the checked (sun) state. I couldn’t get defaultChecked to work how I wanted, so I replaced the unchecked with this conditional rendering ternary operator (conditional operator): { togClass === "light" ? : } Enter fullscreen mode Exit fullscreen mode If you used a , you could easily use conditional rendering like this to change the className attribute within the tag and get the same effect. Put all together, the code for the toggle component looks like this: import React, { useEffect, useState } from 'react'; import '../styles/toggle.css'; import { setTheme } from '../utils/themes'; function Toggle() { const [togClass, setTogClass] = useState('dark'); let theme = localStorage.getItem('theme'); const handleOnClick = () => { if (localStorage.getItem('theme') === 'theme-dark') { setTheme('theme-light'); setTogClass('light') } else { setTheme('theme-dark'); setTogClass('dark') } } useEffect(() => { if (localStorage.getItem('theme') === 'theme-dark') { setTogClass('dark') } else if (localStorage.getItem('theme') === 'theme-light') { setTogClass('light') } }, [theme]) return (
    { togClass === "light" ? : }
    ) } Enter fullscreen mode Exit fullscreen mode Finally, my favorite part: the color switching SVGs! CSS variables work in SVG code too! I got my SVG code for the Github and Chrome icons from DEVICON. For the Github icon all I had to change was one fill attribute in a : fill="var(--dark-text)"> Enter fullscreen mode Exit fullscreen mode The Chrome icon had a fill attribute in a and a : fill="var(--dark-text)" cx="63.624" cy="64.474" r="22.634"> fill="var(--dark-text)" ...> Enter fullscreen mode Exit fullscreen mode The result looks like this: Conclusion I tried to include all of the relevant code, but you can also see the full code for my site in its Github repository. If you enjoyed this article or are left with questions, please leave a comment below! I would also love to see anything built following this tutorial.

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts

  • Show HN: Open-Source Video Editor Web App

    2 projects | news.ycombinator.com | 11 May 2024
  • Nematode SIM

    1 project | news.ycombinator.com | 11 May 2024
  • Mastering Real-Time Collaboration: Building Figma and Miro-Inspired Features with Supabase

    6 projects | dev.to | 11 May 2024
  • Re-inventing the wheel - Use list

    1 project | dev.to | 11 May 2024
  • 10 Ferramentas de Produtividade para Desenvolvedores Experimentarem em 2024

    3 projects | dev.to | 11 May 2024