weko/assets/js/main.js

40 lines
1.0 KiB
JavaScript

import obfuscates from 'js/obfuscates';
obfuscates();
function changeTheme() {
// https://css-tricks.com/a-complete-guide-to-dark-mode-on-the-web/#using-javascript-local-storage
const btn = document.querySelector(".btn-toggle");
const prefersDarkScheme = window.matchMedia("(prefers-color-scheme: dark)");
const currentTheme = localStorage.getItem("theme");
if (currentTheme == "dark") {
document.body.classList.toggle("dark-theme");
} else if (currentTheme == "light") {
document.body.classList.toggle("light-theme");
}
btn.addEventListener("click", function () {
if (prefersDarkScheme.matches) {
document.body.classList.toggle("light-theme");
var theme = document.body.classList.contains("light-theme")
? "light"
: "dark";
} else {
document.body.classList.toggle("dark-theme");
var theme = document.body.classList.contains("dark-theme")
? "dark"
: "light";
}
localStorage.setItem("theme", theme);
});
}
function main() {
obfuscates();
changeTheme();
}
main();