mirror of
https://github.com/jbowdre/runtimeterror.git
synced 2024-11-24 16:02:18 +00:00
42 lines
No EOL
1.2 KiB
JavaScript
42 lines
No EOL
1.2 KiB
JavaScript
const toggleButton = document.getElementById('themeToggle');
|
|
const htmlElement = document.documentElement;
|
|
|
|
function setTheme(theme) {
|
|
htmlElement.setAttribute('data-theme', theme);
|
|
localStorage.setItem('theme', theme);
|
|
updateToggleButton(theme);
|
|
}
|
|
|
|
function updateToggleButton(theme) {
|
|
toggleButton.setAttribute('aria-checked', theme === 'dark');
|
|
}
|
|
|
|
function getPreferredTheme() {
|
|
const storedTheme = localStorage.getItem('theme');
|
|
if (storedTheme) {
|
|
return storedTheme;
|
|
}
|
|
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
|
}
|
|
|
|
// Ensure the toggle button state is correct on load
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
updateToggleButton(getPreferredTheme());
|
|
});
|
|
|
|
// Listen for toggle button clicks
|
|
toggleButton.addEventListener('click', () => {
|
|
const currentTheme = htmlElement.getAttribute('data-theme') || getPreferredTheme();
|
|
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
|
|
setTheme(newTheme);
|
|
});
|
|
|
|
// Listen for system preference changes
|
|
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
|
|
if (!localStorage.getItem('theme')) {
|
|
const newTheme = e.matches ? 'dark' : 'light';
|
|
setTheme(newTheme);
|
|
}
|
|
});
|
|
|
|
setTheme(getPreferredTheme()); |