Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
899 B
1
Indexable
Never
const root = document.documentElement;
const themeBtn = document.getElementById("themeBtn");
const darkModeBtn = document.querySelector(".fa-moon");
const lightModeBtn = document.querySelector(".fa-sun");
let themeMode = localStorage.getItem("data-theme");

const enableDarkMode = () => {
  root.classList.add("dark");
  darkModeBtn.classList.remove("hidden");
  lightModeBtn.classList.add("hidden");
  localStorage.setItem("data-theme", "dark");
};

const disableDarkMode = () => {
  root.classList.remove("dark");
  darkModeBtn.classList.add("hidden");
  lightModeBtn.classList.remove("hidden");
  localStorage.setItem("data-theme", "");
};

if (themeMode === "dark") enableDarkMode();

themeBtn.addEventListener("click", (e) => {
  e.preventDefault();
  themeMode = localStorage.getItem("data-theme");
  if (themeMode === "dark") {
    enableDarkMode();
  } else {
    disableDarkMode();
  }
});