-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
37 lines (32 loc) · 968 Bytes
/
script.js
File metadata and controls
37 lines (32 loc) · 968 Bytes
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
/* =========================================================
THEME MANAGEMENT
- Default dark mode
- Persist user preference
- No SEO logic in JS (correct practice)
========================================================= */
const root = document.documentElement;
const toggleButton = document.getElementById("theme-toggle");
const STORAGE_KEY = "preferred-theme";
/**
* Apply theme and persist choice
*/
function applyTheme(theme) {
root.setAttribute("data-theme", theme);
localStorage.setItem(STORAGE_KEY, theme);
}
/**
* Initialize theme on first load
* Default: DARK
*/
function initTheme() {
const savedTheme = localStorage.getItem(STORAGE_KEY);
applyTheme(savedTheme || "dark");
}
/**
* Toggle theme on click
*/
toggleButton.addEventListener("click", () => {
const currentTheme = root.getAttribute("data-theme");
applyTheme(currentTheme === "dark" ? "light" : "dark");
});
initTheme();