JSFiddle - React, Tailwind, and code Playground
HTML
<script>
const body = document.querySelector('body');
function toggleDark() {
if (document.documentElement.classList.contains('dark')) {
document.documentElement.classList.remove('dark');
localStorage.setItem("theme", "light");
} else {
document.documentElement.classList.add('dark');
localStorage.setItem("theme", "dark");
}
}
if (localStorage.getItem("theme") === "dark") {
document.documentElement.classList.add('dark');
}
</script>
<button class="dark-mode" id="btn-id" onclick="toggleDark()">DARK</button>
CSS
html body {background-color: #ffffff}
html.dark body {background-color: #000000; color: #ffffff}