-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
209 lines (172 loc) · 5.58 KB
/
Copy pathscript.js
File metadata and controls
209 lines (172 loc) · 5.58 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const themeStorageKey = "deniz-theme";
const themeMediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
const getStoredTheme = () => {
try {
const storedTheme = window.localStorage.getItem(themeStorageKey);
return storedTheme === "light" || storedTheme === "dark"
? storedTheme
: null;
} catch {
return null;
}
};
const getCurrentTheme = () => {
return (
getStoredTheme() ||
(themeMediaQuery.matches ? "dark" : "light")
);
};
const updateThemeToggle = (theme) => {
const toggle = document.querySelector("[data-theme-toggle]");
if (!toggle) {
return;
}
const nextTheme = theme === "dark" ? "light" : "dark";
toggle.setAttribute("aria-label", `Switch to ${nextTheme} mode`);
toggle.setAttribute("aria-pressed", theme === "dark" ? "true" : "false");
toggle.title = `Switch to ${nextTheme} mode`;
};
const applyTheme = (theme) => {
document.documentElement.dataset.theme = theme;
document.documentElement.style.colorScheme = theme;
updateThemeToggle(theme);
};
const persistTheme = (theme) => {
try {
window.localStorage.setItem(themeStorageKey, theme);
} catch {
return;
}
};
applyTheme(getCurrentTheme());
const headerInner = document.querySelector(".site-header-inner");
const siteNav = document.querySelector(".site-nav");
const menuToggle = document.querySelector("[data-menu-toggle]");
if (headerInner) {
const themeToggle = document.createElement("button");
themeToggle.className = "theme-toggle";
themeToggle.type = "button";
themeToggle.dataset.themeToggle = "";
themeToggle.innerHTML = `
<svg class="theme-icon-dark" viewBox="0 0 24 24" aria-hidden="true">
<path d="M12 3a6 6 0 0 0 9 7.5 9 9 0 1 1-9-7.5Z"></path>
</svg>
<svg class="theme-icon-light" viewBox="0 0 24 24" aria-hidden="true">
<circle cx="12" cy="12" r="4"></circle>
<path d="M12 2v2"></path>
<path d="M12 20v2"></path>
<path d="m4.93 4.93 1.41 1.41"></path>
<path d="m17.66 17.66 1.41 1.41"></path>
<path d="M2 12h2"></path>
<path d="M20 12h2"></path>
<path d="m6.34 17.66-1.41 1.41"></path>
<path d="m19.07 4.93-1.41 1.41"></path>
</svg>
`;
themeToggle.addEventListener("click", () => {
const nextTheme =
document.documentElement.dataset.theme === "dark" ? "light" : "dark";
persistTheme(nextTheme);
applyTheme(nextTheme);
});
headerInner.append(themeToggle);
updateThemeToggle(getCurrentTheme());
}
const closeMenu = () => {
if (!siteNav || !menuToggle) {
return;
}
siteNav.dataset.open = "false";
menuToggle.setAttribute("aria-expanded", "false");
menuToggle.setAttribute("aria-label", "Open navigation menu");
};
if (siteNav && menuToggle) {
menuToggle.addEventListener("click", () => {
const isOpen = siteNav.dataset.open === "true";
siteNav.dataset.open = isOpen ? "false" : "true";
menuToggle.setAttribute("aria-expanded", isOpen ? "false" : "true");
menuToggle.setAttribute(
"aria-label",
isOpen ? "Open navigation menu" : "Close navigation menu",
);
});
}
themeMediaQuery.addEventListener("change", (event) => {
if (!getStoredTheme()) {
applyTheme(event.matches ? "dark" : "light");
}
});
const prefersReducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
const revealItems = document.querySelectorAll(".reveal");
if (revealItems.length > 0 && !prefersReducedMotion) {
revealItems.forEach((item, index) => {
item.style.setProperty("--reveal-delay", `${Math.min(index * 45, 420)}ms`);
});
window.requestAnimationFrame(() => {
document.documentElement.classList.add("has-motion");
});
}
const sectionLinks = document.querySelectorAll("[data-section-link]");
if (sectionLinks.length > 0) {
const sections = Array.from(sectionLinks)
.map((link) => document.querySelector(link.getAttribute("href")))
.filter(Boolean);
const getScrollOffset = () => {
const header = document.querySelector(".site-header");
return (header?.offsetHeight || 0) + 18;
};
const setActiveLink = (id) => {
sectionLinks.forEach((link) => {
const target = link.getAttribute("href");
link.dataset.active = target === `#${id}` ? "true" : "false";
});
};
const scrollToSection = (section) => {
const top =
window.scrollY + section.getBoundingClientRect().top - getScrollOffset();
window.scrollTo({
top,
behavior: prefersReducedMotion ? "auto" : "smooth",
});
};
const updateActiveSection = () => {
if (sections.length === 0) {
return;
}
const scrollMarker = window.scrollY + getScrollOffset() + 24;
let activeSection = sections[0];
sections.forEach((section) => {
if (section.offsetTop <= scrollMarker) {
activeSection = section;
}
});
setActiveLink(activeSection.id);
};
sectionLinks.forEach((link) => {
link.addEventListener("click", (event) => {
const targetId = link.getAttribute("href")?.slice(1);
const targetSection = targetId
? document.getElementById(targetId)
: null;
if (!targetId || !targetSection) {
return;
}
event.preventDefault();
history.replaceState(null, "", `#${targetId}`);
setActiveLink(targetId);
closeMenu();
scrollToSection(targetSection);
});
});
window.addEventListener("scroll", updateActiveSection, { passive: true });
window.addEventListener("resize", () => {
updateActiveSection();
if (window.matchMedia("(min-width: 431px)").matches) {
closeMenu();
}
});
window.addEventListener("hashchange", updateActiveSection);
updateActiveSection();
}