-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
70 lines (55 loc) · 1.85 KB
/
script.js
File metadata and controls
70 lines (55 loc) · 1.85 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
const storageKey = "portfolio-theme";
const body = document.body;
const themeToggle = document.querySelector("[data-theme-toggle]");
const themeLabel = document.querySelector(".theme-toggle__label");
const menuToggle = document.querySelector(".menu-toggle");
const headerPanel = document.querySelector(".header-panel");
const navLinks = document.querySelectorAll(".site-nav a");
const yearElement = document.getElementById("year");
function applyTheme(theme) {
body.dataset.theme = theme;
if (themeToggle && themeLabel) {
const nextTheme = theme === "dark" ? "light" : "dark";
themeToggle.setAttribute("aria-label", `Switch to ${nextTheme} theme`);
themeLabel.textContent = theme === "dark" ? "Dark" : "Light";
}
}
function getInitialTheme() {
const savedTheme = window.localStorage.getItem(storageKey);
if (savedTheme === "dark" || savedTheme === "light") {
return savedTheme;
}
return window.matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark";
}
function toggleTheme() {
const nextTheme = body.dataset.theme === "dark" ? "light" : "dark";
applyTheme(nextTheme);
window.localStorage.setItem(storageKey, nextTheme);
}
function closeMenu() {
if (!menuToggle || !headerPanel) {
return;
}
menuToggle.setAttribute("aria-expanded", "false");
headerPanel.classList.remove("is-open");
}
function toggleMenu() {
if (!menuToggle || !headerPanel) {
return;
}
const isOpen = headerPanel.classList.toggle("is-open");
menuToggle.setAttribute("aria-expanded", String(isOpen));
}
applyTheme(getInitialTheme());
if (themeToggle) {
themeToggle.addEventListener("click", toggleTheme);
}
if (menuToggle) {
menuToggle.addEventListener("click", toggleMenu);
}
navLinks.forEach((link) => {
link.addEventListener("click", closeMenu);
});
if (yearElement) {
yearElement.textContent = new Date().getFullYear();
}