Last active
August 2, 2022 07:11
-
-
Save thevolcanomanishere/072e126dd38352387372e9345d317653 to your computer and use it in GitHub Desktop.
Toggling darkmode the easy way (Tailwind)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ExampleComponent = () => { | |
return <h1 className="bg-white dark:bg-black">Hello</h1>; | |
}; | |
export default ExampleComponent; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @type {import('tailwindcss').Config} */ | |
module.exports = { | |
darkMode: "class", | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const toggleDarkMode = () => { | |
if (localStorage.getItem("color-theme")) { | |
if (localStorage.getItem("color-theme") === "light") { | |
document.documentElement.classList.add("dark"); | |
localStorage.setItem("color-theme", "dark"); | |
} else { | |
document.documentElement.classList.remove("dark"); | |
localStorage.setItem("color-theme", "light"); | |
} | |
// if NOT set via local storage previously | |
} else { | |
if (document.documentElement.classList.contains("dark")) { | |
document.documentElement.classList.remove("dark"); | |
localStorage.setItem("color-theme", "light"); | |
} else { | |
document.documentElement.classList.add("dark"); | |
localStorage.setItem("color-theme", "dark"); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment