hugo-theme-hello-friend/source/js/theme.js

19 lines
528 B
JavaScript
Raw Permalink Normal View History

2018-07-20 19:14:22 +02:00
// Toggle theme
2018-12-21 19:07:37 +01:00
const getTheme = window.localStorage && window.localStorage.getItem("theme");
const themeToggle = document.querySelector(".theme-toggle");
const isDark = getTheme === "dark";
2018-07-20 19:14:22 +02:00
2018-12-21 19:07:37 +01:00
if (getTheme !== null) {
document.body.classList.toggle("dark-theme", isDark);
}
2018-07-20 19:14:22 +02:00
2018-12-21 19:07:37 +01:00
themeToggle.addEventListener("click", () => {
document.body.classList.toggle("dark-theme");
window.localStorage &&
window.localStorage.setItem(
2018-12-21 19:07:37 +01:00
"theme",
document.body.classList.contains("dark-theme") ? "dark" : "light",
);
});