color mode switcher with cookie support

1 reply · opened Apr 24, 2023

Rrezn8dApr 24, 2023

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');
    }
  });
SSemicolon WebSTAFFApr 25, 2023

Hello,

Thanks so much for your suggestions and sharing your solution. We really appreciate this a lot!

We have been already working on this and will be releasing an Update soon. We have built this on VanillaJS for more uniformity across the Template JS Codes.

Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

Have the same question, or something new?

Sign in to the Canvas dashboard to reply or open your own topic. Canvas owners get direct help from the SemiColonWeb team.

Reply on the dashboard