-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
212 lines (180 loc) · 7.16 KB
/
Copy pathscript.js
File metadata and controls
212 lines (180 loc) · 7.16 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
210
211
212
"use strict";
document.documentElement.classList.add("js");
const header = document.querySelector("[data-header]");
const navToggle = document.querySelector(".nav-toggle");
const navigation = document.querySelector(".site-nav");
const navLinks = [...document.querySelectorAll('.site-nav a[href^="#"]')];
const prefersReducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
// Mobile navigation
function closeNavigation() {
if (!navigation || !navToggle) return;
navigation.classList.remove("open");
navToggle.setAttribute("aria-expanded", "false");
navToggle.setAttribute("aria-label", "Open navigation menu");
}
navToggle?.addEventListener("click", () => {
const willOpen = !navigation.classList.contains("open");
navigation.classList.toggle("open", willOpen);
navToggle.setAttribute("aria-expanded", String(willOpen));
navToggle.setAttribute("aria-label", willOpen ? "Close navigation menu" : "Open navigation menu");
});
navLinks.forEach((link) => link.addEventListener("click", closeNavigation));
document.addEventListener("click", (event) => {
if (!navigation?.classList.contains("open")) return;
if (!header?.contains(event.target)) closeNavigation();
});
window.addEventListener("resize", () => {
if (window.innerWidth > 1020) closeNavigation();
});
// Smooth scrolling with sticky-header offset
document.querySelectorAll('a[href^="#"]').forEach((link) => {
link.addEventListener("click", (event) => {
const targetId = link.getAttribute("href");
if (!targetId || targetId === "#") return;
const target = document.querySelector(targetId);
if (!target) return;
event.preventDefault();
const headerHeight = header?.offsetHeight ?? 0;
const targetTop = target.getBoundingClientRect().top + window.scrollY - headerHeight;
window.scrollTo({
top: targetTop,
behavior: prefersReducedMotion.matches ? "auto" : "smooth",
});
history.replaceState(null, "", targetId);
});
});
// Active-section navigation
const navigationSections = navLinks
.map((link) => document.querySelector(link.getAttribute("href")))
.filter(Boolean);
function updateActiveNavigation() {
const offset = (header?.offsetHeight ?? 0) + 90;
let activeSection = navigationSections[0];
navigationSections.forEach((section) => {
if (section.getBoundingClientRect().top <= offset) activeSection = section;
});
navLinks.forEach((link) => {
const isActive = link.getAttribute("href") === `#${activeSection?.id}`;
link.classList.toggle("active", isActive);
if (isActive) link.setAttribute("aria-current", "location");
else link.removeAttribute("aria-current");
});
}
let scrollFrame;
window.addEventListener(
"scroll",
() => {
if (scrollFrame) return;
scrollFrame = requestAnimationFrame(() => {
updateActiveNavigation();
scrollFrame = null;
});
},
{ passive: true }
);
updateActiveNavigation();
// Subtle scroll reveal
const revealItems = document.querySelectorAll(".reveal");
if (prefersReducedMotion.matches || !("IntersectionObserver" in window)) {
revealItems.forEach((item) => item.classList.add("visible"));
} else {
const revealObserver = new IntersectionObserver(
(entries, observer) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
entry.target.classList.add("visible");
observer.unobserve(entry.target);
});
},
{ threshold: 0.12, rootMargin: "0px 0px -35px" }
);
revealItems.forEach((item) => revealObserver.observe(item));
}
// Missing-image fallbacks
function showImageFallback(image) {
image.classList.add("image-missing");
const fallback = image.nextElementSibling;
if (fallback?.classList.contains("image-fallback")) fallback.classList.add("visible");
}
document.querySelectorAll("[data-fallback-image]").forEach((image) => {
image.addEventListener("error", () => showImageFallback(image), { once: true });
if (image.complete && image.naturalWidth === 0) showImageFallback(image);
});
// Accessible project modal
const modal = document.querySelector("[data-modal]");
const modalDialog = modal?.querySelector(".modal-dialog");
const modalContent = modal?.querySelector("[data-modal-content]");
const modalOpeners = document.querySelectorAll("[data-modal-open]");
const modalClosers = modal?.querySelectorAll("[data-modal-close]") ?? [];
let returnFocus = null;
function getFocusableElements() {
if (!modalDialog) return [];
return [...modalDialog.querySelectorAll(
'a[href], button:not([disabled]), input:not([disabled]), textarea:not([disabled]), select:not([disabled]), [tabindex]:not([tabindex="-1"])'
)].filter((element) => !element.hasAttribute("hidden"));
}
function openModal(templateId, trigger) {
const template = document.getElementById(templateId);
if (!modal || !modalDialog || !modalContent || !(template instanceof HTMLTemplateElement)) return;
returnFocus = trigger;
modalContent.replaceChildren(template.content.cloneNode(true));
modal.hidden = false;
document.body.classList.add("modal-open");
requestAnimationFrame(() => modalDialog.focus());
}
function closeModal() {
if (!modal || modal.hidden) return;
modal.hidden = true;
document.body.classList.remove("modal-open");
modalContent?.replaceChildren();
returnFocus?.focus();
returnFocus = null;
}
modalOpeners.forEach((button) => {
button.addEventListener("click", () => openModal(button.dataset.modalOpen, button));
});
modalClosers.forEach((button) => button.addEventListener("click", closeModal));
document.addEventListener("keydown", (event) => {
if (!modal || modal.hidden) return;
if (event.key === "Escape") {
event.preventDefault();
closeModal();
return;
}
if (event.key !== "Tab") return;
const focusable = getFocusableElements();
if (!focusable.length) {
event.preventDefault();
modalDialog?.focus();
return;
}
const first = focusable[0];
const last = focusable[focusable.length - 1];
const focusIsInsideDialog = modalDialog?.contains(document.activeElement);
if (!focusIsInsideDialog) {
event.preventDefault();
first.focus();
} else if (event.shiftKey && (document.activeElement === first || document.activeElement === modalDialog)) {
event.preventDefault();
last.focus();
} else if (!event.shiftKey && document.activeElement === last) {
event.preventDefault();
first.focus();
}
});
// Mailto contact form; no data is sent to a backend.
const contactForm = document.querySelector("[data-contact-form]");
contactForm?.addEventListener("submit", (event) => {
event.preventDefault();
if (!contactForm.reportValidity()) return;
const formData = new FormData(contactForm);
const name = String(formData.get("name") ?? "").trim();
const email = String(formData.get("email") ?? "").trim();
const subject = String(formData.get("subject") ?? "").trim();
const message = String(formData.get("message") ?? "").trim();
const body = `${message}\n\nFrom: ${name}\nEmail: ${email}`;
window.location.href = `mailto:noahchen482@gmail.com?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
});
// Automatic footer year
const yearElement = document.querySelector("[data-current-year]");
if (yearElement) yearElement.textContent = String(new Date().getFullYear());