-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
54 lines (43 loc) · 1.79 KB
/
Copy pathapp.js
File metadata and controls
54 lines (43 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
document.querySelectorAll('a[href^="#"]').forEach((link) => {
link.addEventListener('click', () => link.blur());
});
const themeToggle = document.querySelector('.theme-toggle');
function currentTheme() {
return document.documentElement.dataset.theme === 'dark' ? 'dark' : 'light';
}
function updateThemeControl() {
if (!themeToggle) return;
const nextTheme = currentTheme() === 'dark' ? 'light' : 'dark';
themeToggle.setAttribute('aria-label', `Switch to ${nextTheme} mode`);
themeToggle.setAttribute('title', `Switch to ${nextTheme} mode`);
}
themeToggle?.addEventListener('click', () => {
const nextTheme = currentTheme() === 'dark' ? 'light' : 'dark';
document.documentElement.dataset.theme = nextTheme;
localStorage.setItem('theme', nextTheme);
updateThemeControl();
});
updateThemeControl();
const menuToggle = document.querySelector('.menu-toggle');
const mainNavigation = document.querySelector('#main-navigation');
function closeNavigation() {
if (!menuToggle || !mainNavigation) return;
menuToggle.setAttribute('aria-expanded', 'false');
menuToggle.setAttribute('aria-label', 'Open navigation');
mainNavigation.classList.remove('open');
}
menuToggle?.addEventListener('click', () => {
const willOpen = menuToggle.getAttribute('aria-expanded') !== 'true';
menuToggle.setAttribute('aria-expanded', String(willOpen));
menuToggle.setAttribute('aria-label', willOpen ? 'Close navigation' : 'Open navigation');
mainNavigation?.classList.toggle('open', willOpen);
});
mainNavigation?.querySelectorAll('a').forEach((link) => {
link.addEventListener('click', closeNavigation);
});
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape') closeNavigation();
});
window.addEventListener('resize', () => {
if (window.innerWidth > 700) closeNavigation();
});