-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
97 lines (78 loc) · 2.85 KB
/
script.js
File metadata and controls
97 lines (78 loc) · 2.85 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
const EXIT_DURATION_MS = 180;
let restoreNavIndicator = () => {};
const isSamePageNavigation = (targetUrl) => {
const current = new URL(window.location.href);
return (
targetUrl.origin === current.origin &&
targetUrl.pathname === current.pathname &&
targetUrl.search === current.search
);
};
window.addEventListener("pageshow", () => {
document.body.classList.remove("is-leaving");
restoreNavIndicator();
});
const setupNavIndicator = () => {
const nav = document.querySelector("nav");
if (!nav) return;
const links = Array.from(nav.querySelectorAll("a[href]"));
if (!links.length) return;
const indicator = document.createElement("span");
indicator.className = "nav-indicator";
indicator.setAttribute("aria-hidden", "true");
nav.appendChild(indicator);
const placeIndicator = (link) => {
const navRect = nav.getBoundingClientRect();
const linkRect = link.getBoundingClientRect();
indicator.style.width = `${linkRect.width}px`;
indicator.style.transform = `translateX(${linkRect.left - navRect.left}px)`;
indicator.style.opacity = "1";
};
const hideIndicator = () => {
indicator.style.width = "0px";
indicator.style.opacity = "0";
};
const restoreIndicator = () => {
const activeLink = nav.querySelector("a.active");
if (activeLink) {
placeIndicator(activeLink);
return;
}
hideIndicator();
};
links.forEach((link) => {
link.addEventListener("mouseenter", () => {
placeIndicator(link);
});
link.addEventListener("focus", () => {
placeIndicator(link);
});
});
nav.addEventListener("mouseleave", restoreIndicator);
nav.addEventListener("focusout", (event) => {
if (!nav.contains(event.relatedTarget)) {
restoreIndicator();
}
});
window.addEventListener("resize", restoreIndicator);
restoreNavIndicator = restoreIndicator;
restoreIndicator();
};
setupNavIndicator();
document.addEventListener("click", (event) => {
const link = event.target.closest("a[href]");
if (!link) return;
if (event.defaultPrevented || event.button !== 0) return;
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
if (link.target && link.target !== "_self") return;
if (link.hasAttribute("download")) return;
const targetUrl = new URL(link.href, window.location.href);
if (targetUrl.origin !== window.location.origin) return;
if (isSamePageNavigation(targetUrl) && targetUrl.hash) return;
event.preventDefault();
if (document.body.classList.contains("is-leaving")) return;
document.body.classList.add("is-leaving");
window.setTimeout(() => {
window.location.assign(targetUrl.href);
}, EXIT_DURATION_MS);
});