hugo-battheme/static/js/darkmode.js

30 lines
673 B
JavaScript
Raw Normal View History

2022-04-09 20:23:18 +00:00
const checkbox = document.getElementById('darkmode-toggle');
const body = document.querySelector("body");
2022-04-09 19:42:12 +00:00
2022-04-09 20:23:18 +00:00
if (localStorage.getItem('dark')) {
switchToDark()
} else {
switchToLight()
2022-04-09 19:42:12 +00:00
}
2022-04-09 20:23:18 +00:00
checkbox.checked = localStorage.getItem('dark')
checkbox.addEventListener("click", function () {
localStorage.setItem("dark", this.checked)
if (this.checked) {
switchToDark()
} else {
switchToLight()
2022-04-09 19:42:12 +00:00
}
2022-04-09 20:23:18 +00:00
})
2022-04-09 19:42:12 +00:00
function switchToLight() {
2022-04-09 20:23:18 +00:00
// for some reason this needs to be set to null
// i am losing my mind
localStorage.removeItem("dark")
body.classList.add('light')
2022-04-09 19:42:12 +00:00
}
function switchToDark() {
2022-04-09 20:23:18 +00:00
body.classList.remove('light')
2022-04-09 19:42:12 +00:00
}