-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdark_mode.js
55 lines (50 loc) · 1.16 KB
/
dark_mode.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
52
53
54
55
(function () {
const getCurrentMode = function () {
let mode = localStorage.getItem("mode");
if (!mode) {
mode = "light";
}
return mode;
};
const changeMode = function (toggler) {
let mode = getCurrentMode();
if (toggler) {
toggler.checked = mode === "dark";
}
if (mode === "dark") {
DarkReader.setFetchMethod(window.fetch);
DarkReader.enable({
brightness: 100,
contrast: 90,
sepia: 10,
});
} else {
DarkReader.disable();
}
};
const configure = function () {
const darkSwitch = document.getElementById("dark-switch");
if (darkSwitch) {
darkSwitch.addEventListener("change", function (e) {
if (darkSwitch.checked) {
localStorage.setItem("mode", "dark");
} else {
localStorage.setItem("mode", "light");
}
changeMode(darkSwitch);
});
}
changeMode(darkSwitch);
};
function ready(fn) {
if (document.readyState !== "loading") {
fn();
} else {
document.addEventListener("DOMContentLoaded", fn);
}
}
ready(function () {
configure();
});
changeMode();
})();