welpo af80aef00c
feat: change default theme to OS default
Update the theme switcher code to set the default theme to the OS
default, rather than the "light" theme. This provides a more seamless
user experience for users whose OS is set to a dark mode. The current
theme setting in local storage will still be respected if present. This
change was made to improve the accessibility and usability.

Fixes #38
2023-02-15 20:36:26 +01:00

29 lines
962 B
JavaScript

var themeSwitcher = document.querySelector('.theme-switcher input');
var currentTheme = localStorage.getItem('theme');
// detect the user's preferred color scheme and activate it
if (currentTheme) {
document.documentElement.setAttribute('data-theme', currentTheme);
if (currentTheme === 'dark') {
themeSwitcher.checked = true;
}
} else if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.setAttribute('data-theme', 'dark');
themeSwitcher.checked = true;
}
// switch between themes
function switchTheme(e) {
if (e.target.checked) {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage.setItem('theme', 'dark');
}
else {
document.documentElement.setAttribute('data-theme', 'light');
localStorage.setItem('theme', 'light');
}
}
// event listener on checkbox change
themeSwitcher.addEventListener('change', switchTheme, false);