-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
63 lines (54 loc) · 1.64 KB
/
script.js
File metadata and controls
63 lines (54 loc) · 1.64 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
const THEME = {
STORAGE_KEY: "theme",
LIGHT: "light",
DARK: "dark",
};
const themeToggleBtn = document.getElementById("theme-toggle-btn");
const themeToggleIcon = document.getElementById("theme-toggle");
const themeElements = document.getElementsByClassName("theme");
const getTheme = () => window.localStorage.getItem(THEME.STORAGE_KEY);
function changeToDarkTheme() {
for (let index = 0; index < themeElements.length; index++) {
const element = themeElements[index];
element.classList.remove("light-theme");
element.classList.add("dark-theme");
}
window.localStorage.setItem(THEME.STORAGE_KEY, THEME.DARK);
}
function changeToLightTheme() {
for (let index = 0; index < themeElements.length; index++) {
const element = themeElements[index];
element.classList.remove("dark-theme");
element.classList.add("light-theme");
}
window.localStorage.setItem(THEME.STORAGE_KEY, THEME.LIGHT);
}
themeToggleBtn.addEventListener("click", () => {
if (getTheme() === THEME.LIGHT) {
changeToDarkTheme();
} else {
changeToLightTheme();
}
});
function setPrefferedTheme() {
const theme = getTheme();
if (theme) {
switch (theme) {
case THEME.LIGHT:
changeToLightTheme();
break;
case THEME.DARK:
changeToDarkTheme();
break;
}
} else {
if (window.matchMedia("(prefers-color-scheme: light)").matches) {
changeToLightTheme();
} else {
changeToDarkTheme();
}
}
}
window.onload = () => {
setPrefferedTheme();
};