Repository navigation
Expand file tree
/
Copy paththeme.js
More file actions
185 lines (167 loc) · 5.18 KB
/
Copy paththeme.js
File metadata and controls
185 lines (167 loc) · 5.18 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* Dotfield site theme.
* Default: follow the OS/system color scheme until the user picks light or dark.
* Shared by home + docs.
*/
(function (root) {
"use strict";
var STORAGE_KEY = "dotfield-theme";
var PREF = { system: "system", light: "light", dark: "dark" };
function systemPrefersDark() {
try {
return (
typeof matchMedia === "function" &&
matchMedia("(prefers-color-scheme: dark)").matches
);
} catch (e) {
return false;
}
}
/** Resolved visual theme: only "light" | "dark" */
function resolveTheme(pref) {
if (pref === PREF.light || pref === PREF.dark) return pref;
return systemPrefersDark() ? PREF.dark : PREF.light;
}
/** Stored preference: "system" | "light" | "dark" (missing → system) */
function getPreference() {
try {
var stored = localStorage.getItem(STORAGE_KEY);
if (stored === PREF.light || stored === PREF.dark || stored === PREF.system) {
return stored;
}
} catch (e) {}
return PREF.system;
}
function setPreference(pref) {
var p =
pref === PREF.light || pref === PREF.dark || pref === PREF.system
? pref
: PREF.system;
try {
if (p === PREF.system) {
// No explicit choice — follow OS (and stay in sync)
localStorage.removeItem(STORAGE_KEY);
} else {
localStorage.setItem(STORAGE_KEY, p);
}
} catch (e) {}
return p;
}
function applyResolved(theme) {
var t = theme === PREF.dark ? PREF.dark : PREF.light;
var rootEl = document.documentElement;
rootEl.setAttribute("data-theme", t);
rootEl.style.colorScheme = t;
var pref = getPreference();
var buttons = document.querySelectorAll("[data-theme-toggle]");
for (var i = 0; i < buttons.length; i++) {
var btn = buttons[i];
var isDark = t === PREF.dark;
btn.setAttribute("aria-pressed", isDark ? "true" : "false");
btn.setAttribute("data-theme-pref", pref);
btn.setAttribute(
"aria-label",
isDark ? "Switch to light mode" : "Switch to dark mode"
);
var title =
pref === PREF.system
? isDark
? "Following system (dark) — click for light"
: "Following system (light) — click for dark"
: isDark
? "Light mode"
: "Dark mode";
btn.setAttribute("title", title);
var label = btn.querySelector("[data-theme-label]");
if (label) label.textContent = isDark ? "Light" : "Dark";
}
return t;
}
function getTheme() {
return resolveTheme(getPreference());
}
function emit(theme) {
try {
root.dispatchEvent(
new CustomEvent("dotfield:theme", {
detail: { theme: theme, preference: getPreference() },
})
);
} catch (e) {}
}
/** Set explicit light/dark, or "system" to follow OS again. */
function setTheme(themeOrPref) {
var pref;
if (themeOrPref === PREF.system) {
pref = setPreference(PREF.system);
} else if (themeOrPref === PREF.light || themeOrPref === PREF.dark) {
pref = setPreference(themeOrPref);
} else {
pref = setPreference(PREF.system);
}
var resolved = applyResolved(resolveTheme(pref));
emit(resolved);
return resolved;
}
function toggleTheme() {
// Explicit override opposite of what is currently shown
var next = getTheme() === PREF.dark ? PREF.light : PREF.dark;
return setTheme(next);
}
function useSystemTheme() {
return setTheme(PREF.system);
}
function bindToggles() {
var buttons = document.querySelectorAll("[data-theme-toggle]");
for (var i = 0; i < buttons.length; i++) {
(function (btn) {
if (btn.__themeBound) return;
btn.__themeBound = true;
btn.addEventListener("click", function () {
toggleTheme();
});
})(buttons[i]);
}
applyResolved(getTheme());
}
function watchSystem() {
if (typeof matchMedia !== "function") return;
try {
var mq = matchMedia("(prefers-color-scheme: dark)");
var onChange = function () {
// Only auto-update while preference is system (no stored light/dark)
if (getPreference() === PREF.system) {
var resolved = applyResolved(resolveTheme(PREF.system));
emit(resolved);
}
};
if (typeof mq.addEventListener === "function") {
mq.addEventListener("change", onChange);
} else if (typeof mq.addListener === "function") {
mq.addListener(onChange);
}
} catch (e) {}
}
// Apply immediately (default = system → OS preference)
if (typeof document !== "undefined" && document.documentElement) {
applyResolved(getTheme());
}
watchSystem();
var api = {
getTheme: getTheme,
getPreference: getPreference,
setTheme: setTheme,
toggleTheme: toggleTheme,
useSystemTheme: useSystemTheme,
bindToggles: bindToggles,
STORAGE_KEY: STORAGE_KEY,
};
root.DotfieldTheme = api;
if (typeof document !== "undefined") {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", bindToggles);
} else {
bindToggles();
}
}
})(typeof globalThis !== "undefined" ? globalThis : this);