1159cl/js/theme-toggle.js

23 lines
811 B
JavaScript
Raw Normal View History

2024-12-06 23:38:18 +00:00
document.addEventListener('DOMContentLoaded', () => {
const themeToggle = document.getElementById('theme-toggle');
const themeIcon = document.getElementById('theme-icon');
function applyTheme(theme) {
document.documentElement.setAttribute('data-theme', theme);
themeIcon.src = theme === 'dark' ? 'icons/pixelarticons/svg/moon.svg' : 'icons/pixelarticons/svg/sun-alt.svg';
themeToggle.checked = theme === 'dark';
}
const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
applyTheme(systemPrefersDark ? 'dark' : 'light');
themeToggle.addEventListener('change', function() {
if (this.checked) {
applyTheme('dark');
} else {
applyTheme('light');
}
});
});