Repository navigation
Expand file tree
/
Copy pathpage-scripts.js
More file actions
117 lines (102 loc) · 4.1 KB
/
Copy pathpage-scripts.js
File metadata and controls
117 lines (102 loc) · 4.1 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
// ===== Shared scripts for all pages =====
(function() {
'use strict';
// ===== Theme Management =====
const THEME_KEY = 'bouldering-lingo-theme';
const html = document.documentElement;
function initTheme() {
const savedTheme = localStorage.getItem(THEME_KEY);
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const theme = savedTheme || (prefersDark ? 'dark' : 'light');
setTheme(theme, false);
}
function setTheme(theme, save = true) {
html.setAttribute('data-theme', theme);
if (save) {
localStorage.setItem(THEME_KEY, theme);
}
// Update meta theme-color for mobile browsers
const metaThemes = document.querySelectorAll('meta[name="theme-color"]');
metaThemes.forEach(meta => {
if (meta.getAttribute('media')?.includes('dark') && theme === 'dark') {
meta.setAttribute('content', '#0a0a0b');
} else if (meta.getAttribute('media')?.includes('light') && theme === 'light') {
meta.setAttribute('content', '#f5f5f7');
} else if (!meta.getAttribute('media')) {
meta.setAttribute('content', theme === 'dark' ? '#0a0a0b' : '#f5f5f7');
}
});
}
function toggleTheme() {
const current = html.getAttribute('data-theme');
const newTheme = current === 'dark' ? 'light' : 'dark';
setTheme(newTheme);
}
// ===== Navigation Toggle =====
function initNavToggle() {
const navToggle = document.getElementById('navToggle');
const navLinks = document.getElementById('navLinks');
if (navToggle && navLinks) {
navToggle.addEventListener('click', () => {
const expanded = navToggle.getAttribute('aria-expanded') === 'true';
navToggle.setAttribute('aria-expanded', !expanded);
navToggle.classList.toggle('open');
navLinks.classList.toggle('open');
});
// Close menu when clicking a link
navLinks.querySelectorAll('a').forEach(link => {
link.addEventListener('click', () => {
navToggle.classList.remove('open');
navLinks.classList.remove('open');
navToggle.setAttribute('aria-expanded', 'false');
});
});
// Close menu when clicking outside
document.addEventListener('click', (e) => {
if (!navToggle.contains(e.target) && !navLinks.contains(e.target)) {
navToggle.classList.remove('open');
navLinks.classList.remove('open');
navToggle.setAttribute('aria-expanded', 'false');
}
});
}
}
// ===== Theme Toggle Button =====
function initThemeToggle() {
const themeToggle = document.getElementById('themeToggle');
if (themeToggle) {
themeToggle.addEventListener('click', toggleTheme);
}
}
// ===== Smooth Scroll for Anchor Links =====
function initSmoothScroll() {
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
const target = document.querySelector(this.getAttribute('href'));
if (target) {
e.preventDefault();
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
});
});
}
// ===== Initialize =====
function init() {
initTheme();
initNavToggle();
initThemeToggle();
initSmoothScroll();
}
// Run on DOM ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
// Listen for system theme changes
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
if (!localStorage.getItem(THEME_KEY)) {
setTheme(e.matches ? 'dark' : 'light', false);
}
});
})();