-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
71 lines (61 loc) · 2.48 KB
/
Copy pathscript.js
File metadata and controls
71 lines (61 loc) · 2.48 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
const menuButton = document.querySelector(".menu-toggle");
const navigation = document.querySelector(".site-nav");
const progressBar = document.querySelector(".reading-progress span");
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
function closeMenu() {
menuButton?.setAttribute("aria-expanded", "false");
navigation?.classList.remove("is-open");
document.body.classList.remove("menu-open");
}
menuButton?.addEventListener("click", () => {
const isOpen = menuButton.getAttribute("aria-expanded") === "true";
menuButton.setAttribute("aria-expanded", String(!isOpen));
navigation?.classList.toggle("is-open", !isOpen);
document.body.classList.toggle("menu-open", !isOpen);
});
navigation?.querySelectorAll("a").forEach((link) => link.addEventListener("click", closeMenu));
function updateReadingProgress() {
const scrollable = document.documentElement.scrollHeight - window.innerHeight;
const progress = scrollable > 0 ? Math.min(window.scrollY / scrollable, 1) : 0;
if (progressBar) progressBar.style.width = `${progress * 100}%`;
}
window.addEventListener("scroll", updateReadingProgress, { passive: true });
window.addEventListener("resize", updateReadingProgress);
updateReadingProgress();
const revealItems = document.querySelectorAll(".reveal");
function revealHashTarget() {
const hash = window.location.hash.slice(1);
if (!hash) return;
document.getElementById(hash)?.classList.add("is-visible");
}
revealHashTarget();
window.addEventListener("hashchange", revealHashTarget);
if (reduceMotion || !("IntersectionObserver" in window)) {
revealItems.forEach((item) => item.classList.add("is-visible"));
} else {
const revealObserver = new IntersectionObserver(
(entries, observer) => {
for (const entry of entries) {
if (!entry.isIntersecting) continue;
entry.target.classList.add("is-visible");
observer.unobserve(entry.target);
}
},
{ rootMargin: "0px 0px -8%", threshold: 0.08 },
);
revealItems.forEach((item) => revealObserver.observe(item));
}
document.querySelectorAll(".copy-command").forEach((button) => {
button.addEventListener("click", async () => {
const command = button.dataset.copy;
if (!command) return;
try {
await navigator.clipboard.writeText(command);
const original = button.textContent;
button.textContent = "Copied";
window.setTimeout(() => { button.textContent = original; }, 1400);
} catch {
button.textContent = command;
}
});
});