-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
48 lines (42 loc) · 1.18 KB
/
common.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
{{define "common-js"}}
const cssMappings = {
dark: {
"--link-text-color": "white",
"--link-text-hover-color": "teal",
"--background-color": "#02071f",
},
light: {
"--link-text-color": "#02071f",
"--link-text-hover-color": "#db4e4e",
"--background-color": "white",
},
};
const root = document.querySelector(":root");
function updateColors() {
if (JSON.parse(window.localStorage.getItem("dark-mode-enabled"))) {
Object.entries(cssMappings["dark"]).forEach(([key, value]) => {
document.body.style.setProperty(key, value);
});
return;
}
Object.entries(cssMappings["light"]).forEach(([key, value]) => {
document.body.style.setProperty(key, value);
});
}
function toggleDarkMode(event) {
const darkModeEnabled = window.localStorage.getItem("dark-mode-enabled");
if (darkModeEnabled === "null") {
window.localStorage.setItem("dark-mode-enabled", "true");
return;
}
window.localStorage.setItem(
"dark-mode-enabled",
!JSON.parse(window.localStorage.getItem("dark-mode-enabled")),
);
updateColors();
}
document
.getElementById("dark-mode-toggle-button")
.addEventListener("click", toggleDarkMode);
updateColors();
{{end}}