shapebox/js/theme-toggle.js
2022-08-29 11:37:25 +02:00

22 lines
No EOL
680 B
JavaScript

// Get current theme
var theme = localStorage.getItem( 'theme' );
// Set defaults if theme is not defined.
if ( ! theme ) {
localStorage.setItem( 'theme', 'light' );
theme = 'light';
}
// Add theme to the body.
document.body.classList.add( theme );
// Handle onClick events
document.getElementById( 'theme-toggle' ).addEventListener( 'click', () => {
// Cleanup classes from body.
document.body.classList.remove( 'light' );
document.body.classList.remove( 'dark' );
// Change the theme.
theme = ( theme === 'light' ) ? 'dark' : 'light';
// Save the theme.
localStorage.setItem( 'theme', theme );
// Apply the theme.
document.body.classList.add( theme );
});