gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Core Block suggestion: Theme Style Toggle

Open ghost opened this issue 3 years ago • 21 comments

What problem does this address?

The site owner can't easily let visitors change the appearance of the site, which could improve accessibility. Think "high contrast" style or "dark mode". Also the theme styles are such a nice addition, but there's way more potential hidden behind a really simple block. Right now only the site owner can change the style, but the other styles stay "hidden" and inactive.

What is your proposed solution?

Add a simple toggle block which you could e.g. add to your site header, where you select one of your Theme Styles to toggle to. As soon as you click the toggle, the styles toggle to the chosen style. This way you can easily add a High Contrast style, Big Fonts style, Dark Mode style, etc. to your site, making it more accessible and/or providing a better user experience.

Can be as easy as this example.

ghost avatar Feb 20 '23 13:02 ghost

We discussed this in the accessibility bug scrub today, and think that this would be a great feature to standardize. A few questions to consider:

  1. Should this only support two design variants, or more?
  2. Should there be a mechanism in the style book for quickly comparing the variants to ensure that the design implementation is complete on both?

joedolson avatar Aug 04 '23 15:08 joedolson

I’m shocked there isn’t a suitable plugin on the market to address this, given how smoothly Style Variations operate within the Site Editor (“Browse Styles” pane) and the growing client expectations on Dark Mode.

Considerations

  • The “Browse Styles” pane might include a way to designate light and dark Variations for prefers-color-scheme
  • This proposed “Toggle Styles” block could be a checkbox list to select your Variations for front-end rendering
  • If one Variation is picked, the “Toggle Styles” block renders as a basic toggle button (as OP suggests)
  • If multiple Variations are picked, then it might convert to a dropdown feature

Related conversations: https://github.com/WordPress/gutenberg/issues/42828 https://github.com/WordPress/gutenberg/issues/24368 https://wordpress.org/support/topic/best-practice-theme-json-for-dark-mode/

spacedragn avatar Nov 28 '23 17:11 spacedragn

Good afternoon Please allow theme authors to publish their themes with theme switching blocks from dark to light (and vice versa) in the Wordpress repository. This problem has been pointed out by one of the most popular authors of free themes for vordpress, Anders Noren in this topic I am sincerely convinced that Anders's opinion is worth listening to - he personally with his free themes has added to the worldwide popularity of Wordpress at least 1 percent of those 43% of the world's sites that run on Wordpress.

egor-kugno avatar Apr 18 '24 09:04 egor-kugno

@richtabor built such a block: See Dark Mode Toggle Block

bph avatar Jul 13 '24 10:07 bph

I don't think this block would be suitable for core, as we'd need a way to effectively apply a dark mode style to any theme (not feasible).

My dark mode toggle block is is block, along with instructions for setting up CSS on your end.

I propose we close this.

richtabor avatar Jul 17 '24 19:07 richtabor

I recently came upon this variation style switcher block in this video. Looks like a pretty simple plugin that accomplishes the main feature in this suggested feature for block themes.

dhanson-wp avatar Aug 01 '24 00:08 dhanson-wp

@dhanson-wp Tried the last plugin out with my custom styles and it did not work as expected. Also it creates an annoying menu in the backend instead of hiding.

saschafoerster avatar Aug 03 '24 22:08 saschafoerster

Should this only support two design variants, or more?

I believe it should support any number and not be limited to something like dark vs. light mode.

I think the best course of action would be to integrate with the existing style variations system so that we're not introducing a new concept for themers to learn.

Under the hood, there's probably some things to work out with the way variations are stored. For example, users can't make per-variation style overrides, and style variations themselves are stored as user customizations (see: https://github.com/WordPress/gutenberg/issues/62661).

justintadlock avatar Aug 09 '24 15:08 justintadlock

Reminder: It should automatically detect site visitors' preference through an OS setting or a user agent setting, or provide a way to work with prefers-color-scheme.

daotw avatar Sep 11 '24 16:09 daotw

Throwing in my two cents, I'd love to see this become a core feature.

TheJeffr0 avatar Jan 31 '25 20:01 TheJeffr0

Definitely something we need on Wordpress

Superkikim avatar May 02 '25 14:05 Superkikim

At first, I wanted this to become a core feature. However, after reading and discussing Rich Tabor's point above (https://github.com/WordPress/gutenberg/issues/48249#issuecomment-2234149811), it's clear that this task is nearly impossible and needs to be handled on a per-site basis with a custom solution.

TheJeffr0 avatar Sep 02 '25 19:09 TheJeffr0

Not sure I understand why it can't be done. At least a theme switcher on UI.

Let's say I have Twenty-four child theme with light colors and a I make a second child theme with dark colors. If I add a button for users to click and it just switch themes, how difficult can that be?

I get that it light not be suitable if you use it with completely different theme architecture but that seems like an acceptable limitation to me.

Superkikim avatar Sep 04 '25 17:09 Superkikim

Right now, the most ideal way I can think to do it would be to move toward a modern solution using the native CSS light-dark() function. But this would require an overhaul of how colors are handled in Global Styles, providing a mechanism for users to customize the colors for both light and dark mode. That's pretty much a prerequisite before even discussing a toggle block. Such a method assumes that the theme isn't already built with a custom design token system that goes beyond the current limitations of settings.color.palette.

Building it this way assumes we're building with the awareness that such features as schemed-value() could be a thing in the future and that light-dark() is just a stepping stone: https://github.com/w3c/csswg-drafts/issues/9660

In the meantime, this is really something that theme authors can be implementing themselves in various ways. I'm pushing to get an article up on the Developer Blog in the next week that explains one method of doing it with modern CSS features.

But there are so many ways to tackle the same problem that I don't see a 100% clear path for Core. This is where exploration from theme authors is so crucial. Instead of just saying that we want something, we need to be sharing how we solved it and the feedback from real-world users on how it works.

justintadlock avatar Sep 04 '25 17:09 justintadlock

This plugin is how the Special Projects team handles light/dark toggle. https://github.com/a8cteam51/special-projects-blocks-monorepo/releases/tag/light-dark-toggle%400.2.1

dhanson-wp avatar Sep 05 '25 03:09 dhanson-wp

Check out this project.

https://github.com/mosne/mosne-dark-palette

I tried this one. But I had lots of difficulties to implement. That said, I'm developing my own theme and I suck at it. Could be might fault. But I got it work occasionally.

Superkikim avatar Sep 05 '25 20:09 Superkikim

I published a tutorial on building this on the WordPress.org Developer Blog: https://developer.wordpress.org/news/2025/09/building-a-light-dark-toggle-with-the-interactivity-api/

justintadlock avatar Sep 11 '25 16:09 justintadlock

Amazing tutorial Justin. As a theme creator, I'd use a very similar approach. I'm also inclined to use modern CSS so I'd love to be able to use light-dark() function paired with an interactive native block. But for that strategy to be user friendly on the site editor, WordPress core would need to provide a mechanism for users to customize both light and dark mode colors, as Justin pointed out. I'd love to see this implemented, I can't think of a better approach for this purpose.

nahuai avatar Sep 16 '25 17:09 nahuai

For my own theme, I'm using a similar setup to the tutorial, but exploring handling colors differently. Rough example of this:

{
	"settings": {
		"color": {
			"palette": [
				{
					"name": "Primary - 500",
					"slug": "primary-500",
					"color": "#555555"
				}
			]
		},
		"custom": {
			"color": {
				"text": {
					"example": "light-dark(var(--wp--preset--color--primary-100), var(--wp--preset--color--primary-900))"
				}
				"background": {
					"example": "light-dark(var(--wp--preset--color--primary-900), var(--wp--preset--color--primary-100))"
				}
			}
		}
	},
	"styles": {
		"color": {
			"text": "var(--wp--custom--color--text--example)",
			"background": "var(--wp--custom--color--background--example)"
		}
	}
}

This is a basic design token system. And it would allow users to customize the palette. However, it doesn't allow them to assign the light/dark colors to blocks. But it's another approach to colors themselves.

It's probably outside the scope of this specific ticket, but I'd love to see WordPress support a layered design token system (primitive -> semantic -> component). Otherwise, we end up with wildly different approaches to things like light/dark toggles, fixing one issue at the expense of another.

justintadlock avatar Sep 16 '25 20:09 justintadlock

I think we need to make a distinction between toggling different themes VS theme styles VS a dark mode/light mode toggle.

For dark mode / light mode I see it as a dropdown with system (default), dark, and light. Ultimately this would somewhere toggle class names in the body tag most likely and set prefers-color-scheme: https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme

bhubbard avatar Oct 12 '25 21:10 bhubbard