-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
177 lines (151 loc) · 5.02 KB
/
script.js
File metadata and controls
177 lines (151 loc) · 5.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
const Utils = {
getElement: (selector) => document.querySelector(selector),
getElements: (selector) => document.querySelectorAll(selector),
};
// Theme Management
const ThemeManager = {
init() {
const themeToggle = Utils.getElement("#themeToggle");
if (!themeToggle) return;
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)");
const setTheme = (theme) => {
const isDark = theme === "dark";
document.documentElement.setAttribute("data-theme", theme);
localStorage.setItem("theme", theme);
themeToggle.textContent = isDark ? "🌞" : "🌛";
themeToggle.setAttribute(
"aria-label",
`Switch to ${isDark ? "light" : "dark"} theme`
);
};
// Initialize theme
const savedTheme =
localStorage.getItem("theme") || (prefersDark.matches ? "dark" : "light");
setTheme(savedTheme);
// Theme toggle handler
themeToggle.addEventListener("click", () => {
const currentTheme = document.documentElement.getAttribute("data-theme");
setTheme(currentTheme === "dark" ? "light" : "dark");
});
// Listen for system theme changes
prefersDark.addEventListener("change", (e) => {
if (!localStorage.getItem("theme")) {
setTheme(e.matches ? "dark" : "light");
}
});
},
};
// UI Management
const UIManager = {
init() {
this.initMobileMenu();
this.initSmoothScroll();
this.initAnimations();
},
initMobileMenu() {
const btn = Utils.getElement(".mobile-menu-btn");
const menu = Utils.getElement(".nav-menu");
if (!btn || !menu) return;
btn.addEventListener("click", () => {
const isActive = menu.classList.toggle("active");
btn.setAttribute("aria-expanded", isActive);
});
// Close menu when clicking outside
document.addEventListener("click", (e) => {
if (menu.classList.contains("active") && !e.target.closest(".navbar")) {
menu.classList.remove("active");
btn.setAttribute("aria-expanded", "false");
}
});
document.addEventListener("keydown", (e) => {
if (e.key === "Escape" && menu.classList.contains("active")) {
menu.classList.remove("active");
btn.setAttribute("aria-expanded", "false");
btn.focus();
}
});
},
initSmoothScroll() {
const navLinks = Utils.getElements('.nav-link[href^="#"]');
if (!navLinks.length) return;
navLinks.forEach((link) => {
link.addEventListener("click", (e) => {
e.preventDefault();
const targetId = link.getAttribute("href");
const targetSection = document.querySelector(targetId);
if (targetSection) {
// Close mobile menu if open
const navMenu = Utils.getElement(".nav-menu");
if (navMenu) navMenu.classList.remove("active");
const mobileBtn = Utils.getElement(".mobile-menu-btn");
if (mobileBtn) mobileBtn.setAttribute("aria-expanded", "false");
const navHeight = Utils.getElement(".navbar")?.offsetHeight || 0;
const targetPosition = targetSection.offsetTop - navHeight;
window.scrollTo({
top: targetPosition,
behavior: "smooth",
});
// Update active state
navLinks.forEach((navLink) => navLink.classList.remove("active"));
link.classList.add("active");
}
});
});
},
initAnimations() {
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add("animate");
observer.unobserve(entry.target);
}
});
},
{
rootMargin: "50px 0px",
threshold: 0.1,
}
);
// Pre-animate the first few items immediately for faster initial load
const elements = Utils.getElements(".animated");
if (!elements.length) return;
const challengesContainer =
Utils.getElement(".challenges-grid")?.parentElement;
if (challengesContainer) {
challengesContainer.classList.add("challenges-container", "loading");
// Use requestAnimationFrame for better performance
requestAnimationFrame(() => {
// Wait for next paint cycle
requestAnimationFrame(() => {
challengesContainer.classList.remove("loading");
});
});
}
// Immediately show the first few visible challenges
elements.forEach((el, index) => {
if (index < 4 || el.getBoundingClientRect().top < window.innerHeight) {
requestAnimationFrame(() => {
setTimeout(() => {
el.classList.add("animate");
}, index * 50);
});
} else {
observer.observe(el);
}
});
// Clean up observer on page navigation
window.addEventListener("unload", () => {
observer.disconnect();
});
},
};
// Initialize on DOM ready
document.addEventListener("DOMContentLoaded", () => {
try {
ThemeManager.init();
UIManager.init();
} catch (error) {
console.error("Initialization error:", error);
}
});