https://canvastemplate.com/color-mode-switcher.html
I love the color switcher "Simple Toggle" however it does not use a cookie to remember the user's choice. It would be great if you could add cookie support to this feature. Here's how I did it:
Light Mode" data-remove-html="Dark Mode">Dark Toggle
This button is added in the navbar. Here's the jQuery:
// DARK & LIGHT THEME TOGGLE
// Check state on page load, set button to match:
var themeSwitcher = $('#theme-switcher');
var dark = "Dark Mode"
var darkText = 'Switch to Dark Theme'
var light = "Light Mode"
var lightText = 'Switch to Light Theme'
if( Cookies.get('cnvs-theme-dark') == '1' ) {
$('body').addClass( 'dark' );
$('html').attr('data-bs-theme','dark');
themeSwitcher.html(light);
themeSwitcher.attr('title', lightText);
themeSwitcher.removeClass('btn-dark').addClass('btn-warning body-state-toggled');
//console.log('TEST dark theme enabled');
} else {
$('body').removeClass( 'dark' );
$('html').removeAttr('data-bs-theme','dark');
themeSwitcher.html(dark);
themeSwitcher.attr('title', darkText);
themeSwitcher.removeClass('btn-warning body-state-toggled').addClass('btn-dark');
//console.log('TEST light theme enabled');
}
// set cookie on button click
themeSwitcher.click(function(e) {
if( $(this).hasClass('body-state-toggled') ) {
Cookies.set('cnvs-theme-dark','1');
//console.log('dark theme enabled');
} else {
Cookies.set('cnvs-theme-dark','0');
//console.log('light theme enabled');
}
});
