-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy paththeme.js
More file actions
159 lines (138 loc) · 4.55 KB
/
Copy paththeme.js
File metadata and controls
159 lines (138 loc) · 4.55 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
(function initSharedTheme() {
const STORAGE_KEY = "theme";
const THEMES = new Set(["dark", "light", "sepia", "cyberpunk", "nord"]);
const root = document.documentElement;
let transitionTimer = null;
let initialized = false;
const safeStorage = {
get() {
try {
return localStorage.getItem(STORAGE_KEY);
} catch (_) {
return null;
}
},
set(theme) {
try {
localStorage.setItem(STORAGE_KEY, theme);
} catch (_) {
// Private browsing and embedded contexts can block storage.
}
},
};
const normalizeTheme = (theme) => (THEMES.has(theme) ? theme : "dark");
const getSystemTheme = () =>
window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
const getStoredTheme = () => {
const saved = safeStorage.get();
return saved ? normalizeTheme(saved) : getSystemTheme();
};
const syncBodyClass = (theme) => {
if (!document.body) return;
document.body.classList.toggle("light-mode", theme === "light");
};
const syncToggleIcons = (theme) => {
// Update active state in dropdown menus
document.querySelectorAll(".theme-dropdown-container .dropdown-item").forEach(item => {
if (item.dataset.themeValue === theme) {
item.classList.add("active");
} else {
item.classList.remove("active");
}
});
const themeSymbols = {
light: "☀",
dark: "☾",
sepia: "☕",
cyberpunk: "⚡",
nord: "❄"
};
const currentSymbol = themeSymbols[theme] || "◌";
document.querySelectorAll("#themeToggleNav span[aria-hidden='true']").forEach((icon) => {
icon.textContent = currentSymbol;
});
};
const applyTheme = (theme, options = {}) => {
const nextTheme = normalizeTheme(theme);
root.setAttribute("data-theme", nextTheme);
root.style.colorScheme = nextTheme;
syncBodyClass(nextTheme);
syncToggleIcons(nextTheme);
if (options.persist !== false) {
safeStorage.set(nextTheme);
}
window.dispatchEvent(new CustomEvent("themechange", { detail: { theme: nextTheme } }));
return nextTheme;
};
const withTransitionGuard = () => {
root.setAttribute("data-theme-transitioning", "true");
document.body?.classList.add("theme-transitioning");
if (transitionTimer) clearTimeout(transitionTimer);
transitionTimer = setTimeout(() => {
root.removeAttribute("data-theme-transitioning");
document.body?.classList.remove("theme-transitioning");
}, 250);
};
const toggleTheme = () => {
const currentTheme = normalizeTheme(root.getAttribute("data-theme"));
const themeArray = ["light", "dark", "sepia", "cyberpunk", "nord"];
const currentIndex = themeArray.indexOf(currentTheme);
const nextIndex = (currentIndex + 1) % themeArray.length;
const nextTheme = themeArray[nextIndex];
applyTheme(nextTheme);
withTransitionGuard();
};
const setTheme = (themeName) => {
applyTheme(themeName);
withTransitionGuard();
};
const init = () => {
if (!root.getAttribute("data-theme")) {
applyTheme(getStoredTheme(), { persist: false });
} else {
applyTheme(root.getAttribute("data-theme"), { persist: false });
}
if (initialized) return;
initialized = true;
// Cross-tab theme sync
window.addEventListener('storage', (e) => {
if (e.key === 'theme' && e.newValue) {
applyTheme(e.newValue, { persist: false });
}
});
// Respect system theme changes (only if user hasn't manually set a theme)
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
if (!safeStorage.get()) {
applyTheme(e.matches ? 'dark' : 'light', { persist: false });
}
});
document.addEventListener("click", (event) => {
const toggle = event.target.closest("#themeToggle");
if (toggle) {
event.preventDefault();
toggleTheme();
return;
}
const dropdownItem = event.target.closest(".theme-dropdown-container .dropdown-item");
if (dropdownItem) {
event.preventDefault();
const themeValue = dropdownItem.dataset.themeValue;
if (themeValue) {
setTheme(themeValue);
}
}
});
document.addEventListener("DOMContentLoaded", () => {
applyTheme(root.getAttribute("data-theme") || getStoredTheme(), { persist: false });
});
};
window.ThemeManager = {
applyTheme,
currentTheme: () => normalizeTheme(root.getAttribute("data-theme") || getStoredTheme()),
init,
toggleTheme,
setTheme,
};
applyTheme(getStoredTheme(), { persist: false });
init();
})();