mirror of
https://codeberg.org/Kankys/doxa.git
synced 2026-11-11 06:44:36 +01:00
new-doxa-monorepo
This commit is contained in:
1 parent
b4401527a3
commit
5eacbb0182
162 files changed
+21820
-2
No files matched your search
@@ -0,0 +1,85 @@
|
||||
(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");
|
||||
});
|
||||
})();
|
||||
Reference in new issue
Block a user