-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeme.js
51 lines (44 loc) · 1.27 KB
/
theme.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
46
47
48
49
50
51
const themeCheckboxRef = document.querySelector("#theme-switch-toggle");
const body = document.querySelector("body");
const page = document.querySelector("#page");
const Theme = {
LIGHT: "light-theme",
DARK: "dark-theme",
};
const dark = Theme.DARK;
const light = Theme.LIGHT;
const localRef = localStorage.getItem("theme");
let localStatus;
function onRef(localStatus) {
localStorage.setItem("theme", localStatus);
}
function changeTh(classAdd, classRem) {
body.classList.add(classAdd);
body.classList.remove(classRem);
}
if (localRef === null) {
localStatus = light;
onRef(localStatus);
} else if (localRef == light) {
localStatus = light;
onRef(localStatus);
changeTh(`${light}`, `${dark}`);
} else if (localRef == dark) {
themeCheckboxRef.setAttribute("checked", true);
localStatus = dark;
onRef(localStatus);
changeTh(`${dark}`, `${light}`);
}
themeCheckboxRef.addEventListener("click", function change(event) {
if (localStatus == light) {
themeCheckboxRef.setAttribute("checked", true);
localStatus = dark;
onRef(localStatus);
changeTh(`${dark}`, `${light}`);
} else if (localStatus == dark) {
themeCheckboxRef.setAttribute("checked", false);
localStatus = light;
onRef(localStatus);
changeTh(`${light}`, `${dark}`);
}
});