-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
118 lines (99 loc) · 3.1 KB
/
Copy pathscript.js
File metadata and controls
118 lines (99 loc) · 3.1 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
(() => {
"use strict";
const sidebar = document.getElementById("sidebar");
const toggle = document.getElementById("sidebarToggle");
const navLinks = document.querySelectorAll(".nav-link");
const sections = document.querySelectorAll("main .section");
const emailLink = document.getElementById("emailLink");
const closeSidebar = () => {
sidebar.classList.remove("is-open");
toggle.setAttribute("aria-expanded", "false");
document.body.classList.remove("no-scroll");
};
const openSidebar = () => {
sidebar.classList.add("is-open");
toggle.setAttribute("aria-expanded", "true");
document.body.classList.add("no-scroll");
};
toggle.addEventListener("click", () => {
if (sidebar.classList.contains("is-open")) {
closeSidebar();
return;
}
openSidebar();
});
navLinks.forEach((link) => {
link.addEventListener("click", () => {
if (sidebar.classList.contains("is-open")) {
closeSidebar();
}
});
});
document.addEventListener("keydown", (event) => {
if (event.key === "Escape" && sidebar.classList.contains("is-open")) {
closeSidebar();
toggle.focus();
}
});
if (emailLink && navigator.clipboard?.writeText) {
const label = emailLink.querySelector(".link-label");
const originalText = label?.textContent ?? "email";
let revertTimer;
emailLink.addEventListener("click", (event) => {
event.preventDefault();
navigator.clipboard.writeText(emailLink.dataset.email).then(
() => {
if (!label) {
return;
}
clearTimeout(revertTimer);
label.textContent = "copied!";
emailLink.classList.add("is-copied");
revertTimer = setTimeout(() => {
label.textContent = originalText;
emailLink.classList.remove("is-copied");
}, 1500);
},
() => {
window.location.href = emailLink.href;
}
);
});
}
if (!("IntersectionObserver" in window) || !sections.length) {
return;
}
const setActive = (id) => {
navLinks.forEach((link) => {
link.classList.toggle("is-active", link.dataset.section === id);
});
};
const syncBottomSection = () => {
const scrollBottom = window.scrollY + window.innerHeight;
const pageBottom = document.documentElement.scrollHeight;
if (pageBottom - scrollBottom <= 4) {
const lastSection = sections[sections.length - 1];
if (lastSection) {
setActive(lastSection.id);
}
}
};
const observer = new IntersectionObserver(
(entries) => {
const visible = entries
.filter((entry) => entry.isIntersecting)
.sort((a, b) => b.intersectionRatio - a.intersectionRatio);
if (visible.length) {
setActive(visible[0].target.id);
}
},
{
rootMargin: "-40% 0px -55% 0px",
threshold: [0, 0.25, 0.5, 1]
}
);
sections.forEach((section) => observer.observe(section));
window.addEventListener("scroll", syncBottomSection, { passive: true });
window.addEventListener("resize", syncBottomSection);
syncBottomSection();
})();