Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
132 lines (124 loc) · 4.7 KB
/
Copy pathscript.js
File metadata and controls
132 lines (124 loc) · 4.7 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
/* ============================================================
Shared interactions — theme toggle, scroll reveal, year.
Hub-only: floating cursor preview on the work index.
============================================================ */
/* ---- theme toggle (persisted) ---- */
(function () {
var btn = document.getElementById("theme-toggle");
if (!btn) return;
btn.addEventListener("click", function () {
var d = document.documentElement;
var next = d.dataset.theme === "dark" ? "light" : "dark";
/* __setTheme is defined by the boot script in <head>; it writes the
cookie that carries the choice across the sibling subdomains. */
if (window.__setTheme) window.__setTheme(next);
else d.dataset.theme = next;
});
})();
/* ---- reveal on scroll ---- */
(function () {
var els = document.querySelectorAll("[data-reveal]");
if (!els.length || !("IntersectionObserver" in window)) {
els.forEach(function (el) { el.classList.add("is-in"); });
return;
}
var io = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
entry.target.classList.add("is-in");
io.unobserve(entry.target);
}
});
}, { threshold: 0.12, rootMargin: "0px 0px -8% 0px" });
els.forEach(function (el) { io.observe(el); });
})();
/* ---- footer year ---- */
(function () {
var y = document.getElementById("year");
if (y) y.textContent = new Date().getFullYear();
})();
/* ---- nav label letter-by-letter stagger ---- */
(function () {
if (matchMedia("(prefers-reduced-motion: reduce)").matches) return;
var links = document.querySelectorAll(".topnav__link");
links.forEach(function (link, li) {
var text = link.textContent;
link.textContent = "";
link.classList.add("stagger");
for (var i = 0; i < text.length; i++) {
var s = document.createElement("span");
if (text[i] === " ") { s.className = "sp"; s.innerHTML = " "; }
else s.textContent = text[i];
s.style.setProperty("--ci", li * 3 + i);
link.appendChild(s);
}
});
})();
/* ---- scrolled nav state ---- */
(function () {
function onScroll() {
document.body.classList.toggle("scrolled", window.scrollY > 40);
}
window.addEventListener("scroll", onScroll, { passive: true });
onScroll();
})();
/* ---- fullscreen hover preview for the cover menu ---- */
(function () {
var peek = document.getElementById("peek");
var media = document.getElementById("peek-media");
if (!peek || !media) return;
if (matchMedia("(hover: none)").matches) return;
document.querySelectorAll("[data-peek]").forEach(function (item) {
item.addEventListener("mouseenter", function () {
var type = item.getAttribute("data-peek");
var src = item.getAttribute("data-peek-src");
media.innerHTML = "";
if (type === "video") {
/* The peek is decoration. On save-data or a slow link, skip it
rather than spend the visitor's bytes on a hover effect. */
var c = navigator.connection;
if (c && (c.saveData || /(^|-)(2g|slow-2g)$/.test(c.effectiveType || ""))) return;
var v = document.createElement("video");
v.src = src; v.muted = true; v.loop = true; v.playsInline = true; v.autoplay = true;
v.play().catch(function () {});
media.appendChild(v);
} else {
var img = new Image();
img.src = src;
media.appendChild(img);
}
peek.classList.add("is-on");
document.body.classList.add("peek-on");
});
item.addEventListener("mouseleave", function () {
peek.classList.remove("is-on");
document.body.classList.remove("peek-on");
media.innerHTML = "";
});
});
})();
/* ---- favicon follows the theme ----
The SVG carries its own prefers-color-scheme query, which covers the OS
setting before this runs. That query cannot see the in-page toggle though,
so swap the file when data-theme changes. The <link> is replaced rather
than re-pointed: several browsers ignore an href edit on a live icon. */
(function () {
var cur = document.querySelector('link[rel="icon"]');
if (!cur) return;
var base = cur.getAttribute("href").replace(/favicon(-dark|-light)?\.svg$/, "favicon");
var shown = null;
function sync() {
var want = document.documentElement.dataset.theme === "light" ? "light" : "dark";
if (want === shown) return;
shown = want;
var next = document.createElement("link");
next.rel = "icon";
next.type = "image/svg+xml";
next.href = base + "-" + want + ".svg";
cur.parentNode.replaceChild(next, cur);
cur = next;
}
sync();
new MutationObserver(sync).observe(document.documentElement,
{ attributes: true, attributeFilter: ["data-theme"] });
})();