mirror of
https://codeberg.org/Kankys/doxa.git
synced 2026-11-11 06:44:36 +01:00
85 lines
2.5 KiB
JavaScript
85 lines
2.5 KiB
JavaScript
(function () {
|
|
"use strict";
|
|
|
|
const THEME_KEY = "doxa_theme";
|
|
let systemMediaQuery = null;
|
|
let systemMediaListener = null;
|
|
|
|
function applyTheme(theme) {
|
|
try {
|
|
if (systemMediaQuery && systemMediaListener) {
|
|
try {
|
|
if (systemMediaQuery.removeEventListener)
|
|
systemMediaQuery.removeEventListener("change", systemMediaListener);
|
|
else systemMediaQuery.removeListener(systemMediaListener);
|
|
} catch (e) {}
|
|
systemMediaQuery = null;
|
|
systemMediaListener = null;
|
|
}
|
|
|
|
if (theme === "dark") {
|
|
document.body.classList.add("dark");
|
|
} else if (theme === "light") {
|
|
document.body.classList.remove("dark");
|
|
} else {
|
|
try {
|
|
const mql = window.matchMedia("(prefers-color-scheme: dark)");
|
|
if (mql.matches) document.body.classList.add("dark");
|
|
else document.body.classList.remove("dark");
|
|
systemMediaQuery = mql;
|
|
systemMediaListener = function (e) {
|
|
if (e.matches) document.body.classList.add("dark");
|
|
else document.body.classList.remove("dark");
|
|
updateThemeButton();
|
|
};
|
|
if (mql.addEventListener)
|
|
mql.addEventListener("change", systemMediaListener);
|
|
else mql.addListener(systemMediaListener);
|
|
} catch (e) {
|
|
document.body.classList.remove("dark");
|
|
}
|
|
}
|
|
} catch (e) {}
|
|
updateThemeButton();
|
|
}
|
|
|
|
function updateThemeButton() {
|
|
const btn = document.getElementById("themeToggle");
|
|
if (!btn) return;
|
|
const theme = getStoredTheme() || "system";
|
|
let icon = "🌓";
|
|
if (theme === "dark") {
|
|
icon = "🌙";
|
|
} else if (theme === "light") {
|
|
icon = "☀️";
|
|
}
|
|
btn.textContent = icon;
|
|
}
|
|
|
|
function getStoredTheme() {
|
|
try {
|
|
return localStorage.getItem(THEME_KEY);
|
|
} catch (e) { return null; }
|
|
}
|
|
function setStoredTheme(value) {
|
|
try {
|
|
localStorage.setItem(THEME_KEY, value);
|
|
} catch (e) {}
|
|
}
|
|
|
|
function toggleTheme() {
|
|
const stored = getStoredTheme() || "system";
|
|
const order = ["light", "dark", "system"];
|
|
let idx = order.indexOf(stored);
|
|
if (idx === -1) idx = 2;
|
|
const next = order[(idx + 1) % order.length];
|
|
setStoredTheme(next);
|
|
applyTheme(next);
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
const themeBtn = document.getElementById("themeToggle");
|
|
if (themeBtn) themeBtn.addEventListener("click", toggleTheme);
|
|
applyTheme(getStoredTheme() || "system");
|
|
});
|
|
})(); |