-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathbutton.js
More file actions
33 lines (29 loc) · 931 Bytes
/
button.js
File metadata and controls
33 lines (29 loc) · 931 Bytes
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
let firstLoad = true;
window.addEventListener("pageshow", () => {
if (!firstLoad) {
localStorage.getItem(key) ? applyTheme() : matchSystem();
return
}
firstLoad = false;
});
function changeTheme() {
const theme = localStorage.getItem(key);
if (!theme) {
matchSystem();
}
switch (localStorage.getItem(key)) {
case "light":
localStorage.setItem(key, "dark");
break;
case "dark":
localStorage.setItem(key, "light");
}
const transition = document.createElement("style");
transition.textContent = "body { transition: all .25s ease-in-out; }";
document.head.appendChild(transition);
applyTheme();
setTimeout(() => {document.head.removeChild(transition)}, 250);
}
const button = document.querySelector("button");
button.style.visibility = "visible";
button.addEventListener("click", () => changeTheme());