-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (76 loc) · 2.64 KB
/
script.js
File metadata and controls
90 lines (76 loc) · 2.64 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
const sidebar = document.querySelector(".sidebar");
const menuToggle = document.querySelector(".menu-toggle");
const navGroups = [...document.querySelectorAll(".nav-group")];
const navLinks = [...document.querySelectorAll(".nav-group > a")];
const sections = navLinks
.map((link) => document.querySelector(link.getAttribute("href")))
.filter(Boolean);
const linkByHash = new Map(
navLinks.map((link) => [link.getAttribute("href"), link])
);
menuToggle?.addEventListener("click", () => {
const isOpen = sidebar.classList.toggle("open");
menuToggle.setAttribute("aria-expanded", String(isOpen));
});
navLinks.forEach((link) => {
link.addEventListener("click", () => {
sidebar.classList.remove("open");
menuToggle?.setAttribute("aria-expanded", "false");
});
});
// Make groups mutually exclusive: opening one collapses the rest. Optional —
// drop this block to allow several sub-menus open at once.
navGroups.forEach((group) => {
group.addEventListener("toggle", () => {
if (!group.open) return;
navGroups.forEach((other) => {
if (other !== group) other.open = false;
});
});
});
const openParentGroup = (link) => {
const parent = link.closest(".nav-group");
if (parent && !parent.open) {
parent.open = true;
navGroups.forEach((other) => {
if (other !== parent) other.open = false;
});
}
};
const activateLink = () => {
const current = sections
.filter((section) => section.getBoundingClientRect().top <= 160)
.at(-1);
if (!current) return;
const activeHash = `#${current.id}`;
navLinks.forEach((link) => {
link.classList.toggle("active", link.getAttribute("href") === activeHash);
});
const activeLink = linkByHash.get(activeHash);
if (activeLink) openParentGroup(activeLink);
};
document.addEventListener("scroll", activateLink, { passive: true });
window.addEventListener("hashchange", activateLink);
activateLink();
// On first load: if URL has a hash, expand its parent group.
if (window.location.hash) {
const link = linkByHash.get(window.location.hash);
if (link) openParentGroup(link);
}
document.querySelectorAll(".copy-button").forEach((button) => {
button.addEventListener("click", async () => {
const code = button.parentElement?.querySelector("code")?.innerText;
if (!code) return;
try {
await navigator.clipboard.writeText(code);
const original = button.textContent;
button.textContent = "Copied";
setTimeout(() => {
button.textContent = original ?? "Copy";
}, 1200);
} catch {
// Clipboard API unavailable (e.g. file://). Fail silently — the
// code block remains selectable in the page.
}
});
});