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 classes = document.documentElement.classList;
|
||||
|
||||
function refresh() {
|
||||
button.innerHTML = theme();
|
||||
function button_refresh() {
|
||||
button.innerHTML = theme_get();
|
||||
}
|
||||
|
||||
function theme() {
|
||||
function theme_get() {
|
||||
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", () => {
|
||||
classes.toggle("dark");
|
||||
refresh();
|
||||
theme_toggle();
|
||||
});
|
||||
|
||||
refresh();
|
||||
theme_read();
|
||||
|
|
Loading…
Add table
Reference in a new issue