homepage/assets/scripts/themeToggle.js

18 lines
772 B
JavaScript
Raw Normal View History

2024-09-28 13:45:52 +02:00
const toggleSwitch = document.querySelector('input[type="checkbox"].theme-switch');
if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme:dark)').matches)) {
document.documentElement.classList.add('dark');
toggleSwitch.checked = true;
} else {
document.documentElement.classList.remove('dark')
}
function switchTheme(e) {
if (e.target.checked) {
localStorage.setItem('theme', 'dark');
document.documentElement.classList.add('dark');
} else {
localStorage.setItem('theme', 'light');
document.documentElement.classList.remove('dark')
}
}
toggleSwitch.addEventListener('change', switchTheme, false);