hugo-batsite/public/js/lightmode.js

37 lines
852 B
JavaScript

const checkbox = document.getElementById("darkmode-toggle");
const colorswitchers = document.getElementsByClassName("colorswitch");
function switchToLight() {
for (let item of colorswitchers) {
item.classList.add("light");
}
}
function switchToDark() {
for (let item of colorswitchers) {
console.log(item);
item.classList.remove("light");
}
}
export function updateMode() {
if (localStorage.getItem("light")) {
switchToLight();
} else {
switchToDark();
}
checkbox.checked = localStorage.getItem("dark");
checkbox.addEventListener("change", function () {
localStorage.setItem("light", this.checked);
if (this.checked) {
switchToLight();
} else {
localStorage.removeItem("light");
switchToDark();
}
});
if (localStorage.getItem("light")) {
checkbox.checked = true;
}
}