Hey! So, if you’re like me and love the whole dark mode vibe, you might have thought about adding it to your website. It’s pretty easy to set up with just a bit of CSS and JavaScript. Here’s how I did it.
First off, you need a button or a switch that users can click to toggle between light and dark modes. I went with a simple button for this example(you can use an icon if you want):
This button is going to be the trigger for switching modes.
Next, you need to define what your light mode and dark mode will look like. In your CSS, you’ll set up the default styles (which will be your light mode) and then add a dark mode class that overrides these styles.
body { background-color: white; color: black; transition: background-color 0.3s, color 0.3s; } .dark-mode { background-color: #121212; color: #ffffff; }
Here’s what’s happening:
Now comes the part where we make the button actually do something. This bit of JavaScript will toggle the .dark-mode class on the body whenever the button is clicked.
const toggleButton = document.getElementById('dark-mode-toggle'); const body = document.body; toggleButton.addEventListener('click', () => { body.classList.toggle('dark-mode'); // Save the user's preference in local storage if (body.classList.contains('dark-mode')) { localStorage.setItem('theme', 'dark'); } else { localStorage.setItem('theme', 'light'); } });
Here’s a breakdown:
To make sure the site loads in the user’s preferred mode, you need to check localStorage when the page loads and set the mode accordingly.
window.addEventListener('load', () => { const savedTheme = localStorage.getItem('theme'); if (savedTheme === 'dark') { body.classList.add('dark-mode'); } });
If you’re using React, the process is pretty similar, but you’ll handle things within your components. Here’s how to do it:
Use React’s useState to manage the dark mode state, and apply the appropriate class to your root element:
import React, { useState } from 'react'; function App() { const [darkMode, setDarkMode] = useState(false); const toggleDarkMode = () => { setDarkMode(!darkMode); }; return (); } export default App;
In this example:
Add the .dark-mode class to your CSS, just like before:
body { background-color: white; color: black; transition: background-color 0.3s, color 0.3s; } .dark-mode { background-color: #121212; color: #ffffff; }
If you want the theme preference to persist, you can add this small tweak:
import React, { useState, useEffect } from 'react'; function App() { const [darkMode, setDarkMode] = useState(() => { const savedTheme = localStorage.getItem('theme'); return savedTheme === 'dark'; }); useEffect(() => { localStorage.setItem('theme', darkMode ? 'dark' : 'light'); }, [darkMode]); return (); } export default App;
Here’s what’s happening:
And that’s it! This is a simpler way to add dark mode to your React app without overcomplicating things.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3