You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
955 B

// Get the theme switch checkbox
const themeSwitch = document.getElementById('theme-switch');
// Function to set the theme
const setTheme = (isDark) => {
if (isDark) {
document.body.classList.add('dark-mode');
document.body.classList.remove('light-mode');
} else {
document.body.classList.add('light-mode');
document.body.classList.remove('dark-mode');
}
};
// Retrieve the saved theme from localStorage
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
const isDark = savedTheme === 'dark';
themeSwitch.checked = !isDark; // Set the switch position
setTheme(isDark);
} else {
setTheme(true); // Default to dark mode
}
// Listen for changes on the switch
themeSwitch.addEventListener('change', (e) => {
const isDark = !e.target.checked; // Light mode when checked
setTheme(isDark);
localStorage.setItem('theme', isDark ? 'dark' : 'light'); // Save preference
});