-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
38 lines (33 loc) · 1.29 KB
/
script.js
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
const toggleSwitch = document.querySelector('input[type="checkbox"]');
const nav = document.getElementById("nav");
const DARK_THEME = "dark";
const LIGHT_THEME = "light";
function toggleDarkLightMode(isDark) {
nav.style.backgroundColor = isDark ? "rgb(0 0 0 / 50%)" : "rgb(255 255 255 / 50%)";
toggleIcon.children[0].textContent = isDark ? "Dark Mode" : "Light Mode";
isDark ? toggleIcon.children[1].classList.replace("sun", "moon") :
toggleIcon.children[1].classList.replace("moon", "sun");
}
// Switch Theme Dynamically
function switchTheme(event) {
if (event.target.checked) {
document.documentElement.setAttribute("data-theme", DARK_THEME);
localStorage.setItem("theme", DARK_THEME);
toggleDarkLightMode(true);
} else {
document.documentElement.setAttribute("data-theme", LIGHT_THEME);
localStorage.setItem("theme", LIGHT_THEME);
toggleDarkLightMode(false);
}
}
// Event Listener
toggleSwitch.addEventListener("change", switchTheme);
// Check Local Storage For Theme
const currentTheme = localStorage.getItem("theme");
if (currentTheme) {
document.documentElement.setAttribute("data-theme", currentTheme);
if (currentTheme === DARK_THEME) {
toggleSwitch.checked = true;
toggleDarkLightMode(true);
}
}