Dark/Light Mode Toggle

:discourse2: Summary Dark/Light Mode Toggle adds a clickable toggle color scheme button in the hamburger menu. The toggle switches between a light or dark color scheme for one theme.
:hammer_and_wrench: Repository Link https://github.com/discourse/discourse-color-scheme-toggle
:open_book: New to Discourse Themes? Beginner’s guide to using Discourse Themes

Install this theme component

This component allows a dark/light mode toggle icon on your Discourse forum. It will also automatically switch if you change your system OS to a mode other than your currently color scheme.

Thanks to @pmusaraj for the latest refactor of this component.

Options

You can choose to have the toggle display in the new Discourse Sidebar or in the header alongside other icons.

Sidebar

Kapture 2022-12-02 at 10.55.59

Header

Kapture 2022-12-02 at 10.57.54

Important Steps

For this to work properly, at least 2 color scheme choices need to be enabled in your /admin/customize/colors area. At least two colors need to have color scheme can be selected by users enabled.

A dark theme needs to be enabled at: /admin/site_settings/category/basic?filter=dark

Once this is done, users should be able to choose between two color schemes as their light and dark preferences in their user preferences interface menu.

37 Likes

Does this only work with the user input via going to their preferences and enabling it? Would be a lot better if It could be a site wide implementation rather than on a user to user base.

An admin would enable this component for the entire site.

From there, depending on what the admin who set up the component has set, the toggle will either be in the user hamburger menu, or on the header of the forum. All users can use this once it has been enabled by an admin.

2 Likes

Sorry, I don’t think I was clear with my question. By looking at your installation tutorial, After it is enabled on the admin side either in hamburger menu or on the header, it looks like each individual user has to go to their preferences and enable/add the light/dark color schemes. Otherwise it does not work for a new user / someone not logged in. So my question is:
Is it possible to implement the color scheme site wide rather than on a user-to-user basis.
Your photo is below for reference

I believe a light scheme and dark scheme will be auto assigned once an admin enables it. From there, a user can edit which dark and light color schemes are used as their personal preference.

You may have to also add a selection here /admin/site_settings/category/basic?filter=dark

I have updated the instructions to reflect this addition. Sorry for the confusion!

3 Likes

It seems doesn’t work correctly when the default color scheme is dark. How can we adapt this?

When the default color scheme is dark, toggle does not work unless a user has select light color scheme from the interface preferences.

2 Likes

Hmmm, I believe this is happening because I am checking for prefers-dark user scheme settings based on the browser and system settings.

Can you look at your inspector console and see if anything is coming up when trying to use the toggle?

I may have to look into this.

1 Like

Warnings at domain.com/logs ;

Dark-Light Toggle theme/component is throwing errors

initialize@https://develcoder.com/theme-javascripts/1d5669cf8344e0837406cef0be37bb086b5968f5.js?__ws=develcoder.com:153:73

initialize@https://develcoder.com/assets/application-01686ed6ee22a989833acbbce970ba63.js:1:11855

https://develcoder.com/assets/vendor-0cc77969943fbfe61f9ee3bae9d7291d.js:36:268273

each@https://develcoder.com/assets/vendor-0cc77969943fbfe61f9ee3bae9d7291d.js:36:429458

walk@https://develcoder.com/assets/vendor-0cc77969943fbfe61f9ee3bae9d7291d.js:36:428478

each@https://develcoder.com/assets/vendor-0cc77969943fbfe61f9ee3bae9d7291d.js:36:427828

topsort@https://develcoder.com/assets/vendor-0cc77969943fbfe61f9ee3bae9d7291d.js:36:427874

_runInitializer@https://develcoder.com/assets/vendor-0cc77969943fbfe61f9ee3bae9d7291d.js:36:268501

runInstanceInitializers@https://develcoder.com/assets/vendor-0cc77969943fbfe61f9ee3bae9d7291d.js:36:268222

_bootSync@https://develcoder.com/assets/vendor-0cc77969943fbfe61f9ee3bae9d7291d.js:36:252462

didBecomeReady@https://develcoder.com/assets/vendor-0cc77969943fbfe61f9ee3bae9d7291d.js:36:257791

invoke@https://develcoder.com/assets/vendor-0cc77969943fbfe61f9ee3bae9d7291d.js:36:418024

flush@https://develcoder.com/assets/vendor-0cc77969943fbfe61f9ee3bae9d7291d.js:36:417018

flush@https://develcoder.com/assets/vendor-0cc77969943fbfe61f9ee3bae9d7291d.js:36:418946

_end@https://develcoder.com/assets/vendor-0cc77969943fbfe61f9ee3bae9d7291d.js:36:424401

end@https://develcoder.com/assets/vendor-0cc77969943fbfe61f9ee3bae9d7291d.js:36:421081

_run@https://develcoder.com/assets/vendor-0cc77969943fbfe61f9ee3bae9d7291d.js:36:424937

u@https://develcoder.com/assets/vendor-0cc77969943fbfe61f9ee3bae9d7291d.js:26:26509


https://develcoder.com/assets/vendor-0cc77969943fbfe61f9ee3bae9d7291d.js:26:26807
1 Like

Same here. Dark light component throwing errors.

Hmmm… not sure where that is coming from as I have no code that should send that out. The only communication I have for an error is here:

https://github.dev/discourse/discourse-color-scheme-toggle/blob/3474a80d81d89f1264be5d55652dc65b5aea68e2/javascripts/discourse/initializers/dark-light-toggle-hamburger.js#L56-L59

Can you link to your site here if possible? Is your instance up to date?

1 Like

Hello,

I have an issue. After adding the theme, the toggle icons are not added in the hamburger menu. They dont even exist in bare code. Also you demo here the buttons dont work. meaning they dont switch between light and dark themes.

Anyone else having the same issue?

Can you check the dev tools console and see if anything shows up related to this? I am seeing the icons work fine on the demo site.

Here is the video:

My main issue is that on my forum, I don’t even see these options in hamburger menu:
image

You do not have the console tab open in your screen cast, so it is hard to see if any errors are taking place.

Have you made sure to follow each step in the original post on this topic?

at least 2 color scheme choices need to be enabled in your /admin/customize/colors area
A dark theme needs to be enabled at: /admin/site_settings/category/basic?filter=dark

And do you, as a user, have two color schemes selected, one for light, and one for dark, in your user preferences?

1 Like

Hello,

Yes this was correctly executed. Today everything is showing correctly. This was due to cookies. It was odd that on other browsers (where I never visited the forum) it was showing missing as well. Now everything is correct. Thank you and sorry for the confusion I’ve caused.

Only thing is, all users including guests can see the toggle on header. But as an admin when I am logged-in, I cannot see the toggles. Is this intentional? Could this be server cache or something?

1 Like

Hmm that’s interesting. Do you have a dark and light color scheme selected in your user preferences?

And are you using the theme that has the component installed?

2 Likes

I had to go to my profile and enable this option (Enable automatic dark mode color scheme) so it can appear on header. probably I was fidgeting around and missed that checkbox.

2 Likes

I love this component! :heart_eyes: But, I am having a little issue.

It looks like the component only works when the “Enable automatic dark mode color scheme” option is enabled. My issue occurs when the option is enabled, and when the system preference is set to dark mode (i.e., preferred color scheme: dark).

In the scenario above, when I try to switch to a light theme vie Preferences > Interface > Theme, nothing happens, which is fine with me. Presumably, since my system preference is already set to dark mode, switching to light mode in Discourse does not override it.

When I do the same with the Dark / Light Mode Toggle component, and if my theme preference in Discourse is set to Light, the toggle actually lets me switch to light mode (which is even better in my view) but the site logo on the header remains in dark mode and hard to see in light mode. When I look in the inspector, I see the element looks like this:

<div class="title"><a href="/" data-auto-route="true"><picture><source srcset="<link to image>" media="(prefers-color-scheme: dark)"><img src="<link to image>" id="site-logo" class="logo-big"></picture></a></div>

I noticed the media="(prefers-color-scheme: dark)" part although we are now in light mode set by the toggle.

I should add that the site logo ordinarily switches fine between light and dark mode when the “Enable automatic dark mode color scheme” option in Discourse is unchecked. But, this prevents the Dark / Light Mode Toggle from being displayed :man_shrugging:t2:

Also, if my theme preference in Discourse is set to Dark from Preferences > Interface > Theme, and my system preference is also dark, the toggle does not switch between light and dark—it always remains on the dark theme.

Ideally, I would prefer the Dark / Light Mode Toggle component to work the way it does now, but to make the site logo in the header follow the currently-active light/dark setting, not the user’s system preferred color scheme.

1 Like

Same here! Did you find a solution?

I’m no frontend developer but I did some debugging:
When in dark mode, like my computer is set to, the HTML for the logo looks like this:

And the logo looks fine, with dark mode fully working. When toggling to light mode, the theme goes light but the logo disappears (dark logo remains).

This is what the HTML looks like:

There is an additional line, <source srcset="https://radixtalk.com/uploads/default/original/1X/19838ecb731d3f87d9db76c927e793ab2e159f1e.png" media="(prefers-color-scheme: dark)"> which “blocks” the correct logo from loading. If I remove that line with Inspect Element, and only leave <img src="https://radixtalk.com/uploads/default/original/1X/237090c67d33a1a38f9f031336dab420c4c4a37d.png" alt="RadixTalk" id="site-logo" class="logo-small" width="36">, the correct logo shows up.

I hope this info was useful.

1 Like

I’m afraid not. I ended up changing the header background color in a way that switching the logo between light and dark mode no longer mattered :man_shrugging:t2: