-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
45 lines (40 loc) · 1.45 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
39
40
41
42
43
44
45
let initialMode = "light";
let prefersColorSchemeDark = window.matchMedia("(prefers-color-scheme: dark)");
if (prefersColorSchemeDark.matches) {
initialMode = "dark";
}
if (localStorage.getItem("initialMode") == null) {
localStorage.setItem("initialMode", initialMode);
}
if (localStorage.getItem("currentMode") == null) {
localStorage.setItem("currentMode", initialMode);
} else {
let currentMode = localStorage.getItem("currentMode");
if (currentMode == "dark" && currentMode != initialMode) {
document.body.classList.add("dark-mode");
} else if (currentMode == "light" && currentMode != initialMode) {
document.body.classList.add("light-mode");
}
}
document.getElementById("toggle").addEventListener(
"click",
function () {
let initialMode = localStorage.getItem("initialMode");
let currentMode = localStorage.getItem("currentMode");
if (currentMode == "dark" && currentMode == initialMode) {
document.body.classList.add("light-mode");
localStorage.setItem("currentMode", "light");
} else if (currentMode == "light" && currentMode == initialMode) {
document.body.classList.add("dark-mode");
localStorage.setItem("currentMode", "dark");
} else if (currentMode != initialMode) {
document.body.removeAttribute("class");
if (currentMode == "dark") {
localStorage.setItem("currentMode", "light");
} else {
localStorage.setItem("currentMode", "dark");
}
}
},
false
);