Dark theme toggle with cookie?

3 replies · opened Oct 17, 2021

Rrezn8dOct 17, 2021

Is there a way to create and recall a site preference cookie to remember a user's dark or light theme?

I am using a "Turn the Lights Off" button on the navigation bar to toggle a users dark or light theme settings and would like to set a cookie to remember:


- 
    [](#)

Here is the jQuery toggle:


// DARK & LIGHT BULB SITE SETTINGS

// GET DEFAULT THEME FROM ADAPTIVE-COLOR-SCHEME
var toggle = $('#theme-setting');
if ($('body').hasClass('dark')) {
  toggle.addClass('bg-dark text-white dark-theme').attr('title','Change to Light Theme');
  $('').appendTo(toggle);
} else {
  toggle.addClass('bg-light text-dark light-theme').attr('title','Change to Dark Theme');
  $('').appendTo(toggle);
}

// OVERRIDE ADAPTIVE-COLOR-SCHEME ON CLICK
toggle.click(function(e) {
  e.preventDefault();
  if ($(this).hasClass('light-theme')) {
    $(this).removeClass('bg-light text-dark light-theme').addClass('bg-dark text-white dark-theme').attr('title','Change to Light Theme').html('');
    $('body').addClass('dark');
  } else {
    $(this).removeClass('bg-dark text-white dark-theme').addClass('bg-light text-dark light-theme').attr('title','Change to Dark Theme').html('');
    $('body').removeClass('dark');
  }
});

How can I store and recall a user's theme choice and override "adaptive-color-scheme" using a cookie?

SSemicolon WebSTAFFOct 18, 2021

Hello,

This is Definitely Possible but requires Customization. You can consider setting a Cookie for the User after he clicks the "Switch to Dark Scheme" Button, using the following JS Code:
[ch_pre type="js"]Cookies.set('canvas-color-scheme', 'dark');[/ch_pre]

and then add the following code to check the Color Scheme and set it accordingly:
[ch_pre type="js"]if( Cookies.get('canvas-color-scheme') == 'dark' ) {
jQuery('body').addClass( 'dark' );
}[/ch_pre]

This will definitely work fine. Hope this Helps!

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

Rrezn8dOct 19, 2021

THANK YOU!

I actually added this to the GDPR lightbox instead:


- 
    [Preferences](#gdpr-preferences)

- 
	
		
			
				
					
						
						
					
					
						Dark Mode (Theme Setting)
					
				
				
					Do you want to turn out the lights? Click the switch to permanently set the site to our dark theme! When you visit our website a cookie (called "canvas-color-scheme") may be placed in your browser. This cookie is used only to determine your theme preference.
				
			
		
		
			
				
				
			
		
	

if( Cookies.get('canvas-color-scheme') == '1' ) {
  $('body').addClass( 'dark' );
}
SSemicolon WebSTAFFOct 21, 2021

Hello,

Glad your issue was resolved.

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

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