Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
79 lines (71 loc) · 2.11 KB
/
Copy pathscript.js
File metadata and controls
79 lines (71 loc) · 2.11 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
document.addEventListener("DOMContentLoaded", () => {
const themeToggles = [
document.getElementById("theme-toggle"),
document.getElementById("theme-toggle-mobile"),
];
const themeIcons = {
light: [
document.getElementById("theme-icon-light"),
document.getElementById("theme-icon-light-mobile"),
],
dark: [
document.getElementById("theme-icon-dark"),
document.getElementById("theme-icon-dark-mobile"),
],
system: [
document.getElementById("theme-icon-system"),
document.getElementById("theme-icon-system-mobile"),
],
};
const mobileMenuButton = document.getElementById("mobile-menu-button");
const mobileMenu = document.getElementById("mobile-menu");
let currentTheme = localStorage.getItem("theme") || "system";
const applyTheme = (theme) => {
if (
theme === "dark" ||
(theme === "system" &&
window.matchMedia("(prefers-color-scheme: dark)").matches)
) {
document.documentElement.classList.add("dark");
} else {
document.documentElement.classList.remove("dark");
}
updateIcons(theme);
};
const updateIcons = (theme) => {
for (const key in themeIcons) {
themeIcons[key].forEach((icon) => icon.classList.add("hidden"));
}
themeIcons[theme].forEach((icon) => icon.classList.remove("hidden"));
};
const cycleTheme = () => {
const themes = ["light", "dark", "system"];
const currentIndex = themes.indexOf(currentTheme);
const nextIndex = (currentIndex + 1) % themes.length;
currentTheme = themes[nextIndex];
localStorage.setItem("theme", currentTheme);
applyTheme(currentTheme);
};
themeToggles.forEach((toggle) => {
if (toggle) {
toggle.addEventListener("click", cycleTheme);
}
});
// System theme change listener
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", (e) => {
if (currentTheme === "system") {
applyTheme("system");
}
});
// Mobile menu toggle
if (mobileMenuButton && mobileMenu) {
mobileMenuButton.addEventListener("click", () => {
mobileMenu.classList.toggle("hidden");
});
}
// Initial setup
applyTheme(currentTheme);
document.documentElement.classList.add("scroll-smooth");
});