homepage/assets/scripts/themeToggle.js
2024-09-28 13:53:15 +02:00

18 lines
No EOL
772 B
JavaScript

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);