Repository navigation
Expand file tree
/
Copy pathsite.js
More file actions
68 lines (60 loc) · 2.13 KB
/
Copy pathsite.js
File metadata and controls
68 lines (60 loc) · 2.13 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
// Movie-mode switch: dark theme = "bias lights only".
// The inline script in each <head> sets data-theme before first paint;
// this wires up the button and remembers the choice.
(function () {
var root = document.documentElement;
var btn = document.querySelector(".lights");
if (!btn) return;
function current() {
return root.dataset.theme === "dark" ? "dark" : "light";
}
function sync() {
var dark = current() === "dark";
btn.setAttribute("aria-pressed", String(dark));
btn.title = dark ? "Lights up (Daily Driver)" : "Movie mode: dim the lights";
}
btn.addEventListener("click", function () {
var next = current() === "dark" ? "light" : "dark";
root.dataset.theme = next;
try {
localStorage.setItem("theme", next);
} catch (e) {
/* private mode: the choice just won't persist */
}
sync();
document.dispatchEvent(new CustomEvent("themechange", { detail: next }));
});
sync();
})();
// Phones: the links sit behind a Menu button, so every page is one tap away.
// Without this script the nav stays a strip you can swipe sideways.
(function () {
var header = document.querySelector(".site-header");
var nav = header && header.querySelector(".site-nav");
if (!nav) return;
var btn = document.createElement("button");
btn.type = "button";
btn.className = "menu-btn";
btn.setAttribute("aria-expanded", "false");
nav.id = nav.id || "site-nav";
btn.setAttribute("aria-controls", nav.id);
btn.innerHTML = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 7h16M4 12h16M4 17h16"/></svg><span>Menu</span>';
nav.parentNode.insertBefore(btn, nav);
header.classList.add("has-menu");
function set(open) {
nav.classList.toggle("open", open);
btn.setAttribute("aria-expanded", String(open));
}
btn.addEventListener("click", function () {
set(!nav.classList.contains("open"));
});
document.addEventListener("keydown", function (e) {
if (e.key === "Escape" && nav.classList.contains("open")) {
set(false);
btn.focus();
}
});
document.addEventListener("click", function (e) {
if (!header.contains(e.target)) set(false);
});
})();