Skip to content

Instantly share code, notes, and snippets.

@thevolcanomanishere
Last active August 2, 2022 07:11
Show Gist options
  • Save thevolcanomanishere/072e126dd38352387372e9345d317653 to your computer and use it in GitHub Desktop.
Save thevolcanomanishere/072e126dd38352387372e9345d317653 to your computer and use it in GitHub Desktop.
Toggling darkmode the easy way (Tailwind)
const ExampleComponent = () => {
return <h1 className="bg-white dark:bg-black">Hello</h1>;
};
export default ExampleComponent;
/** @type {import('tailwindcss').Config} */
module.exports = {
darkMode: "class",
}
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