-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.js
More file actions
34 lines (33 loc) · 1.36 KB
/
Copy paththeme.js
File metadata and controls
34 lines (33 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* theme.js — light/dark, loaded from <head> without defer so the theme lands
before first paint. Storage can throw (private window, blocked site data), so
every read and write is guarded and the page renders correctly without it. */
(function () {
var KEY = "mm-theme";
var root = document.documentElement;
try {
var saved = localStorage.getItem(KEY);
if (saved === "light" || saved === "dark") root.setAttribute("data-theme", saved);
} catch (e) {}
window.addEventListener("DOMContentLoaded", function () {
var btn = document.querySelector(".theme-toggle");
if (!btn) return;
function label() {
var explicit = root.getAttribute("data-theme");
var dark = explicit ? explicit === "dark"
: window.matchMedia("(prefers-color-scheme: dark)").matches;
btn.textContent = dark ? "Light" : "Dark";
btn.setAttribute("aria-label", dark ? "Switch to light theme" : "Switch to dark theme");
}
label();
btn.hidden = false;
btn.addEventListener("click", function () {
var explicit = root.getAttribute("data-theme");
var dark = explicit ? explicit === "dark"
: window.matchMedia("(prefers-color-scheme: dark)").matches;
var next = dark ? "light" : "dark";
root.setAttribute("data-theme", next);
try { localStorage.setItem(KEY, next); } catch (e) {}
label();
});
});
})();