35 lines
952 B
JavaScript
35 lines
952 B
JavaScript
(function () {
|
|
"use strict";
|
|
|
|
const STORAGE_KEY = "ui_theme";
|
|
const root = document.documentElement;
|
|
const button = document.getElementById("theme-toggle");
|
|
|
|
function setTheme(theme) {
|
|
root.setAttribute("data-theme", theme);
|
|
try {
|
|
localStorage.setItem(STORAGE_KEY, theme);
|
|
} catch (_) {}
|
|
if (button) {
|
|
button.textContent = theme === "dark" ? "Light Mode" : "Dark Mode";
|
|
}
|
|
}
|
|
|
|
function getInitialTheme() {
|
|
try {
|
|
const stored = localStorage.getItem(STORAGE_KEY);
|
|
if (stored === "dark" || stored === "light") return stored;
|
|
} catch (_) {}
|
|
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
}
|
|
|
|
setTheme(getInitialTheme());
|
|
|
|
if (button) {
|
|
button.addEventListener("click", function () {
|
|
const current = root.getAttribute("data-theme") === "dark" ? "dark" : "light";
|
|
setTheme(current === "dark" ? "light" : "dark");
|
|
});
|
|
}
|
|
})();
|