hatecomputers.club/static/js/components/themeSwitcher.js

28 lines
676 B
JavaScript
Raw Normal View History

2024-03-26 18:00:05 -04:00
const THEMES = {
DARK: "DARK",
LIGHT: "LIGHT",
};
const flipFlopTheme = (theme) =>
THEMES[theme] === THEMES.DARK ? THEMES.LIGHT : THEMES.DARK;
const themePickerText = {
DARK: "light mode.",
LIGHT: "dark mode.",
};
const themeSwitcher = document.getElementById("theme-switcher");
const setTheme = (theme) => {
themeSwitcher.textContent = `${themePickerText[theme]}`;
document.documentElement.setAttribute("data-theme", theme);
localStorage.setItem("theme", theme);
};
themeSwitcher.addEventListener("click", () =>
setTheme(flipFlopTheme(document.documentElement.getAttribute("data-theme"))),
);
2024-03-27 21:53:04 -04:00
setTheme(localStorage.getItem("theme") || THEMES.LIGHT);