33 lines
1.0 KiB
JavaScript
33 lines
1.0 KiB
JavaScript
/* Function for setting the theme */
|
|
(function(){
|
|
const root = document.documentElement;
|
|
const storageKey = "theme";
|
|
const themes = ["light", "dark", "dark-academia"];
|
|
const labels = {
|
|
"light": "Light",
|
|
"dark": "Dark",
|
|
"dark-academia": "Academia"
|
|
};
|
|
const saved = localStorage.getItem(storageKey);
|
|
if (themes.includes(saved)) {
|
|
root.setAttribute("data-theme", saved);
|
|
}
|
|
const btn = document.getElementById("theme-toggle");
|
|
if (!btn) return;
|
|
const updateButton = () => {
|
|
const current = root.getAttribute("data-theme");
|
|
const label = labels[current] || "Auto";
|
|
btn.textContent = `Theme: ${label}`;
|
|
btn.setAttribute("aria-label", `Current theme: ${label}. Switch theme.`);
|
|
};
|
|
updateButton();
|
|
btn.addEventListener("click", () => {
|
|
const current = root.getAttribute("data-theme");
|
|
const index = themes.indexOf(current);
|
|
const target = themes[index === -1 ? 1 : (index + 1) % themes.length];
|
|
root.setAttribute("data-theme", target);
|
|
localStorage.setItem(storageKey, target);
|
|
updateButton();
|
|
});
|
|
})();
|