-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
183 lines (151 loc) · 6.02 KB
/
Copy pathscript.js
File metadata and controls
183 lines (151 loc) · 6.02 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* ==========================================================================
RICK SANCHEZ — site behaviour
Vanilla JS. No frameworks, no build step.
SCOPE (deliberately narrow):
This file handles the full-screen menu overlay ONLY — open/close,
aria-expanded, Escape, click-outside, focus handling.
It contains NO scroll listeners, NO requestAnimationFrame loops and NO
animation code of any kind. All page animation is owned by Motion.page
(GSAP / ScrollTrigger), which animates CSS properties on the documented
target ids/classes. Do not add scroll-driven visuals here.
========================================================================== */
(function () {
'use strict';
var FOCUSABLE = [
'a[href]',
'button:not([disabled])',
'input:not([disabled])',
'select:not([disabled])',
'textarea:not([disabled])',
'[tabindex]:not([tabindex="-1"])'
].join(',');
function init() {
var toggle = document.getElementById('menuToggle');
var overlay = document.getElementById('menuOverlay');
var closeBtn = document.getElementById('menuClose');
if (!toggle || !overlay) { return; }
var isOpen = false;
var lastFocused = null;
function focusableItems() {
return Array.prototype.filter.call(
overlay.querySelectorAll(FOCUSABLE),
function (el) {
return el.offsetWidth > 0 || el.offsetHeight > 0 || el === document.activeElement;
}
);
}
function openMenu() {
if (isOpen) { return; }
isOpen = true;
lastFocused = document.activeElement;
overlay.hidden = false;
// Force a style flush so the transition runs from the closed state.
void overlay.offsetWidth;
overlay.classList.add('is-open');
toggle.setAttribute('aria-expanded', 'true');
toggle.setAttribute('aria-label', 'Close menu');
document.body.classList.add('menu-open');
var items = focusableItems();
if (items.length) { items[0].focus(); }
}
function closeMenu(returnFocus) {
if (!isOpen) { return; }
isOpen = false;
overlay.classList.remove('is-open');
toggle.setAttribute('aria-expanded', 'false');
toggle.setAttribute('aria-label', 'Open menu');
document.body.classList.remove('menu-open');
// Hide from the a11y tree and tab order once the transition finishes.
window.setTimeout(function () {
if (!isOpen) { overlay.hidden = true; }
}, 340);
if (returnFocus !== false) {
var usable = lastFocused &&
lastFocused !== document.body &&
typeof lastFocused.focus === 'function' &&
document.contains(lastFocused);
(usable ? lastFocused : toggle).focus();
}
}
function trapFocus(event) {
var items = focusableItems();
if (!items.length) { return; }
var first = items[0];
var last = items[items.length - 1];
if (event.shiftKey && document.activeElement === first) {
event.preventDefault();
last.focus();
} else if (!event.shiftKey && document.activeElement === last) {
event.preventDefault();
first.focus();
}
}
toggle.addEventListener('click', function () {
if (isOpen) { closeMenu(); } else { openMenu(); }
});
if (closeBtn) {
closeBtn.addEventListener('click', function () { closeMenu(); });
}
// Click outside the menu panel closes it.
overlay.addEventListener('mousedown', function (event) {
if (event.target === overlay) { closeMenu(); }
});
// Following a nav link closes the overlay (links are in-page anchors).
overlay.addEventListener('click', function (event) {
var link = event.target.closest ? event.target.closest('a[href]') : null;
if (link && overlay.contains(link)) { closeMenu(false); }
});
document.addEventListener('keydown', function (event) {
if (!isOpen) { return; }
if (event.key === 'Escape' || event.key === 'Esc') {
event.preventDefault();
closeMenu();
return;
}
if (event.key === 'Tab') { trapFocus(event); }
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();
/* ==========================================================================
Theme music: play once on load. Browsers block autoplay with sound until
the visitor interacts, so if play() is rejected we retry on the first
click, key press, wheel or touch. The header button mutes / unmutes.
========================================================================== */
(function () {
var audio = document.getElementById('theme');
var btn = document.getElementById('soundToggle');
if (!audio) { return; }
var started = false;
var armEvents = ['pointerdown', 'keydown', 'touchend', 'click'];
function disarm() { armEvents.forEach(function (e) { window.removeEventListener(e, start); }); }
function arm() { armEvents.forEach(function (e) { window.addEventListener(e, start, { passive: true }); }); }
function start(ev) {
// the header button has its own handler; ignore the global unlock for it
if (ev && btn && ev.target && btn.contains(ev.target)) { return; }
if (started) { return; }
var p = audio.play();
if (p && typeof p.then === 'function') {
p.then(function () { started = true; disarm(); if (btn) { btn.setAttribute('aria-pressed', 'true'); btn.setAttribute('aria-label', 'Mute music'); } })
.catch(function () { arm(); });
} else { started = true; disarm(); }
}
audio.volume = 0.6;
start();
if (btn) {
btn.addEventListener('click', function (ev) {
ev.stopPropagation();
if (audio.paused) {
started = false; start();
} else {
audio.pause();
btn.setAttribute('aria-pressed', 'false'); btn.setAttribute('aria-label', 'Play music');
}
});
audio.addEventListener('ended', function () { btn.setAttribute('aria-pressed', 'false'); btn.setAttribute('aria-label', 'Play music'); });
}
})();