30 lines
1.1 KiB
JavaScript
30 lines
1.1 KiB
JavaScript
(function () {
|
|
const t = localStorage.getItem("theme");
|
|
if (t) document.documentElement.style.colorScheme = t;
|
|
|
|
document.addEventListener("click", function (e) {
|
|
const btn = e.target.closest("#theme-toggle");
|
|
if (!btn) return;
|
|
const cs = getComputedStyle(document.documentElement).colorScheme;
|
|
const isDark = cs === "dark" ||
|
|
(!cs || cs === "light dark") &&
|
|
matchMedia("(prefers-color-scheme:dark)").matches;
|
|
const next = isDark ? "light" : "dark";
|
|
document.documentElement.style.colorScheme = next;
|
|
localStorage.setItem("theme", next);
|
|
btn.querySelector("span").textContent = next === "dark"
|
|
? "light_mode"
|
|
: "dark_mode";
|
|
});
|
|
|
|
document.addEventListener("DOMContentLoaded", function () {
|
|
const btn = document.getElementById("theme-toggle");
|
|
if (!btn) return;
|
|
const cs = getComputedStyle(document.documentElement).colorScheme;
|
|
const isDark = cs === "dark" ||
|
|
(!cs || cs === "light dark") &&
|
|
matchMedia("(prefers-color-scheme:dark)").matches;
|
|
btn.querySelector("span").textContent = isDark ? "light_mode" : "dark_mode";
|
|
});
|
|
})();
|