-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.js
More file actions
61 lines (51 loc) · 1.69 KB
/
theme.js
File metadata and controls
61 lines (51 loc) · 1.69 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
const STORAGE_KEY = "portfolio-theme";
function getPreferredTheme() {
try {
const stored = localStorage.getItem(STORAGE_KEY);
if (stored === "light" || stored === "dark") return stored;
} catch {}
const prefersLight =
typeof window !== "undefined" &&
typeof window.matchMedia === "function" &&
window.matchMedia("(prefers-color-scheme: light)").matches;
return prefersLight ? "light" : "dark";
}
function applyTheme(theme) {
const t = theme === "light" ? "light" : "dark";
document.documentElement.dataset.theme = t;
const logoSrc = t === "light" ? "./assets/logo.png" : "./assets/inverse-logo.png";
for (const img of Array.from(document.querySelectorAll("img.brand-mark"))) {
img.setAttribute("src", logoSrc);
}
const btn = document.getElementById("themeToggle");
if (btn) {
const isDark = t === "dark";
btn.setAttribute("aria-checked", isDark ? "true" : "false");
const label = btn.querySelector(".theme-toggle-label");
if (label) label.textContent = isDark ? "Dark" : "Light";
}
}
function setTheme(theme) {
const t = theme === "light" ? "light" : "dark";
applyTheme(t);
try {
localStorage.setItem(STORAGE_KEY, t);
} catch {}
}
function toggleTheme() {
const current = document.documentElement.dataset.theme === "light" ? "light" : "dark";
setTheme(current === "light" ? "dark" : "light");
}
export function initTheme() {
applyTheme(getPreferredTheme());
const btn = document.getElementById("themeToggle");
if (btn) {
btn.addEventListener("click", toggleTheme);
btn.addEventListener("keydown", (e) => {
if (e.key === " " || e.key === "Enter") {
e.preventDefault();
toggleTheme();
}
});
}
}