-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
177 lines (149 loc) · 5.23 KB
/
Copy pathscript.js
File metadata and controls
177 lines (149 loc) · 5.23 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
// Theme toggle logic
const themeToggleBtn = document.getElementById("themeToggleBtn");
const htmlEl = document.documentElement;
function updateThemeToggle() {
const theme = htmlEl.getAttribute("data-theme");
if (theme === "dark") {
themeToggleBtn.textContent = "Light Mode";
themeToggleBtn.setAttribute("aria-pressed", "true");
} else {
themeToggleBtn.textContent = "Dark Mode";
themeToggleBtn.setAttribute("aria-pressed", "false");
}
}
themeToggleBtn.addEventListener("click", () => {
if (htmlEl.getAttribute("data-theme") === "dark") {
htmlEl.setAttribute("data-theme", "light");
} else {
htmlEl.setAttribute("data-theme", "dark");
}
updateThemeToggle();
});
updateThemeToggle();
// Hamburger menu toggle for small screens
const hamburgerBtn = document.getElementById("hamburgerBtn");
const navLinks = document.getElementById("primary-navigation");
hamburgerBtn.addEventListener("click", () => {
const expanded =
hamburgerBtn.getAttribute("aria-expanded") === "true" || false;
hamburgerBtn.setAttribute("aria-expanded", String(!expanded));
navLinks.classList.toggle("open");
// Manage focus trap for menu when open
if (!expanded) {
navLinks.querySelector("a").focus();
navLinks.setAttribute("tabindex", "0");
} else {
hamburgerBtn.focus();
navLinks.setAttribute("tabindex", "-1");
}
});
// Keyboard navigation for menu links
navLinks.addEventListener("keydown", (e) => {
if (e.key === "Escape") {
hamburgerBtn.click();
hamburgerBtn.focus();
}
});
// Search filtering logic
const searchInput = document.getElementById("searchInput");
const featureCards = document.querySelectorAll(".feature-card");
const filterButtons = document.querySelectorAll(".filter-btn");
let activeCategory = "all";
function filterFeatures() {
const searchTerm = searchInput.value.toLowerCase();
featureCards.forEach((card) => {
const title = card.querySelector("h3").textContent.toLowerCase();
const desc = card.querySelector("p").textContent.toLowerCase();
const category = card.getAttribute("data-category");
const matchesSearch =
title.includes(searchTerm) || desc.includes(searchTerm);
const matchesCategory =
activeCategory === "all" || category === activeCategory;
if (matchesSearch && matchesCategory) {
card.style.display = "";
card.setAttribute("aria-hidden", "false");
} else {
card.style.display = "none";
card.setAttribute("aria-hidden", "true");
}
});
}
searchInput.addEventListener("input", () => {
filterFeatures();
});
filterButtons.forEach((btn) => {
btn.addEventListener("click", () => {
filterButtons.forEach((b) => {
b.classList.remove("active");
b.setAttribute("aria-pressed", "false");
});
btn.classList.add("active");
btn.setAttribute("aria-pressed", "true");
activeCategory = btn.getAttribute("data-category");
filterFeatures();
});
// Support keyboard with Enter and Space keys
btn.addEventListener("keydown", (e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
btn.click();
}
});
});
// Newsletter form validation & submission simulation
const newsletterForm = document.getElementById("newsletterForm");
const emailInput = document.getElementById("emailInput");
const newsletterMessage = document.getElementById("newsletterMessage");
newsletterForm.addEventListener("submit", (e) => {
e.preventDefault();
newsletterMessage.textContent = "";
emailInput.setAttribute("aria-invalid", "false");
const emailValue = emailInput.value.trim();
// Simple email regex for validation
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailValue) {
newsletterMessage.textContent = "Please enter your email address.";
emailInput.setAttribute("aria-invalid", "true");
emailInput.focus();
return;
} else if (!emailRegex.test(emailValue)) {
newsletterMessage.textContent = "Please enter a valid email address.";
emailInput.setAttribute("aria-invalid", "true");
emailInput.focus();
return;
}
newsletterMessage.style.color = "var(--accent-color)";
newsletterMessage.textContent = "Thank you for subscribing!";
emailInput.value = "";
});
// Back to top button
const backToTopBtn = document.getElementById("backToTopBtn");
window.addEventListener("scroll", () => {
if (window.scrollY > 200) {
backToTopBtn.style.display = "block";
} else {
backToTopBtn.style.display = "none";
}
});
backToTopBtn.addEventListener("click", () => {
window.scrollTo({ top: 0, behavior: "smooth" });
backToTopBtn.blur();
});
// Accessibility: visually-hidden class for labels if needed
const style = document.createElement("style");
style.textContent = `
.visually-hidden {
position: absolute !important;
width: 1px !important;
height: 1px !important;
padding: 0 !important;
margin: -1px !important;
overflow: hidden !important;
clip: rect(0 0 0 0) !important;
white-space: nowrap !important;
border: 0 !important;
}
`;
document.head.appendChild(style);
// Initial filter on page load
filterFeatures();