theme/persist
This commit is contained in:
parent
b32a5b260e
commit
2e7410265f
1 changed files with 25 additions and 7 deletions
|
@ -1,17 +1,35 @@
|
||||||
const button = document.getElementById("theme");
|
const button = document.getElementById("theme");
|
||||||
const classes = document.documentElement.classList;
|
const classes = document.documentElement.classList;
|
||||||
|
|
||||||
function refresh() {
|
function button_refresh() {
|
||||||
button.innerHTML = theme();
|
button.innerHTML = theme_get();
|
||||||
}
|
}
|
||||||
|
function theme_get() {
|
||||||
function theme() {
|
|
||||||
return classes.contains("dark")?"dark":"light";
|
return classes.contains("dark")?"dark":"light";
|
||||||
}
|
}
|
||||||
|
function theme_read() {
|
||||||
|
const theme = localStorage.getItem("theme");
|
||||||
|
if (theme) {
|
||||||
|
theme_set(theme);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function theme_set(theme) {
|
||||||
|
if (theme == "dark") {
|
||||||
|
classes.add("dark");
|
||||||
|
}
|
||||||
|
button_refresh();
|
||||||
|
}
|
||||||
|
function theme_toggle() {
|
||||||
|
classes.toggle("dark");
|
||||||
|
button_refresh();
|
||||||
|
theme_write();
|
||||||
|
}
|
||||||
|
function theme_write() {
|
||||||
|
localStorage.setItem("theme", theme_get());
|
||||||
|
}
|
||||||
|
|
||||||
button.addEventListener("click", () => {
|
button.addEventListener("click", () => {
|
||||||
classes.toggle("dark");
|
theme_toggle();
|
||||||
refresh();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
refresh();
|
theme_read();
|
||||||
|
|
Loading…
Add table
Reference in a new issue