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:
<div id="theme" class="header-misc-icon d-sm-block">
<button id="theme-switcher" type="button" class="btn btn-dark body-scheme-toggle" data-bodyclass-toggle="dark" data-add-class="btn-warning" data-remove-class="btn-dark" data-add-html="<i class='bi-brightness-high align-middle'></i><span class='visually-hidden'>Light Mode</span>" data-remove-html="<i class='bi-moon-stars align-middle'></i><span class='visually-hidden'>Dark Mode</span>"><span class="visually-hidden">Dark Toggle</span></button>
</div><!-- #top-cart end -->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 = "<i class='bi-moon-stars align-middle'></i><span class='visually-hidden'>Dark Mode</span>"
var darkText = 'Switch to Dark Theme'
var light = "<i class='bi-brightness-high align-middle'></i><span class='visually-hidden'>Light Mode</span>"
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');
}
});