haldis/app/static/js/darkmode.js

28 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-10-24 13:50:51 +02:00
{
var $darkmode = document.querySelector(".enter_darkmode");
const init = () => {
$darkmode.addEventListener("click", toggleDarkmode);
enableDarkmode();
}
const toggleDarkmode = () => {
localStorage.setItem("darkmodeChanged", 'true');
2019-10-24 13:50:51 +02:00
var newState = !(localStorage.getItem("darkmode") == 'true');
localStorage.setItem("darkmode", newState);
enableDarkmode();
}
const enableDarkmode = () => {
if (typeof(Storage) !== "undefined") {
if(localStorage.getItem("darkmode") == 'true' && localStorage.getItem("darkmodeChanged") == 'true' || (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches && localStorage.getItem("darkmodeChanged") != 'true')){
document.querySelector('body').classList.remove('lightmode');
document.querySelector('body').classList.add('darkmode');
2019-10-24 13:50:51 +02:00
} else {
document.querySelector('body').classList.remove('darkmode');
document.querySelector('body').classList.add('lightmode');
}
} else {
console.log('You browser does not support local storage, no darkmode for you!' )
}
}
init();
}