Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (53 loc) · 1.79 KB
/
Copy pathscript.js
File metadata and controls
66 lines (53 loc) · 1.79 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
document.documentElement.classList.add('js-enabled');
function initMobileMenu() {
const toggle = document.querySelector('[data-mobile-menu-toggle]');
const menu = document.querySelector('[data-mobile-menu]');
if (!toggle || !menu) {
return;
}
const setMenuState = (isOpen, { restoreFocus = false } = {}) => {
menu.classList.toggle('is-open', isOpen);
toggle.setAttribute('aria-expanded', String(isOpen));
if (!isOpen && restoreFocus) {
toggle.focus();
}
};
toggle.addEventListener('click', () => {
const isOpen = toggle.getAttribute('aria-expanded') === 'true';
setMenuState(!isOpen);
});
menu.querySelectorAll('a').forEach((link) => {
link.addEventListener('click', () => {
setMenuState(false);
});
});
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && toggle.getAttribute('aria-expanded') === 'true') {
setMenuState(false, { restoreFocus: true });
}
});
}
function initScrollReveal() {
const revealItems = [...document.querySelectorAll('[data-reveal]')];
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
const showAll = () => {
revealItems.forEach((item) => item.classList.add('is-visible'));
};
if (!revealItems.length || reduceMotion || !('IntersectionObserver' in window)) {
showAll();
return;
}
const observer = new IntersectionObserver((entries, currentObserver) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add('is-visible');
currentObserver.unobserve(entry.target);
}
});
}, { threshold: 0.12 });
revealItems.forEach((item) => observer.observe(item));
}
document.addEventListener('DOMContentLoaded', () => {
initMobileMenu();
initScrollReveal();
});