-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
59 lines (49 loc) · 1.76 KB
/
Copy pathscript.js
File metadata and controls
59 lines (49 loc) · 1.76 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
const toggle = document.querySelector('.menu-toggle');
const nav = document.querySelector('.nav-links');
const themeToggle = document.querySelector('.theme-toggle');
function getSavedTheme() {
try {
const theme = localStorage.getItem('theme');
return theme === 'dark' || theme === 'light' ? theme : null;
} catch {
return null;
}
}
function saveTheme(theme) {
try {
localStorage.setItem('theme', theme);
} catch {
// Theme switching still works when storage is unavailable.
}
}
const savedTheme = getSavedTheme();
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
function applyTheme(theme) {
const isDark = theme === 'dark';
document.body.classList.toggle('dark-mode', isDark);
themeToggle?.setAttribute('aria-pressed', String(isDark));
themeToggle?.setAttribute('aria-label', `Switch to ${isDark ? 'light' : 'dark'} mode`);
}
applyTheme(savedTheme || (prefersDark ? 'dark' : 'light'));
themeToggle?.addEventListener('click', () => {
const theme = document.body.classList.contains('dark-mode') ? 'light' : 'dark';
saveTheme(theme);
applyTheme(theme);
});
toggle?.addEventListener('click', () => {
const open = nav.classList.toggle('open');
toggle.setAttribute('aria-expanded', String(open));
});
document.querySelectorAll('.nav-links a').forEach((link) => link.addEventListener('click', () => {
nav.classList.remove('open');
toggle.setAttribute('aria-expanded', 'false');
}));
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add('in-view');
observer.unobserve(entry.target);
}
});
}, { threshold: 0.12 });
document.querySelectorAll('.reveal').forEach((element) => observer.observe(element));