Files
nouri-App/nouri/static/js/theme.js
T
2026-04-12 10:36:13 +02:00

33 lines
1.1 KiB
JavaScript

(() => {
const root = document.documentElement;
const storageKey = "nouri-theme";
const toggle = () => document.querySelector("[data-theme-toggle]");
const applyTheme = (theme) => {
const resolved = theme || localStorage.getItem(storageKey) || "auto";
const finalTheme =
resolved === "auto"
? (window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light")
: resolved;
root.dataset.theme = finalTheme;
const button = toggle();
if (button) {
button.textContent = finalTheme === "dark" ? "Hell" : "Dunkel";
}
};
document.addEventListener("DOMContentLoaded", () => {
applyTheme();
const button = toggle();
if (!button) return;
button.addEventListener("click", () => {
const current = root.dataset.theme === "dark" ? "dark" : "light";
const next = current === "dark" ? "light" : "dark";
localStorage.setItem(storageKey, next);
applyTheme(next);
});
});
})();