gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Duplicate colors in palette causes issues

Open timelsass opened this issue 8 years ago • 17 comments

In the customizer we have created a palette control system, so users have a place to manage their palette globally for our themes. After working on integration with Gutenburg, I've ran into an issue where users are selecting the same color for more than one of the colors in their palette. In the color settings two colors are selected:

image

When entering into the editor, the following error:

Warning: Encountered two children with the same key, rgb(29, 16, 22). Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.

Since our themes let users change the palette in the customizer, and the color classes are already applied to several elements, it's easy for a user to select the same color twice.

This can be reproduced more easily by adding the same color key via the add_theme_support method:

add_theme_support( 'editor-color-palette', array(
    array(
        'name' => __( 'Dark Background', 'themeLangDomain' ),
        'slug' => 'dark-background',
        'color' => 'rgb(0, 0, 0)',
    ),
    array(
        'name' => __( 'Dark Text', 'themeLangDomain' ),
        'slug' => 'dark-text',
        'color' => 'rgb(0, 0, 0)',
    ),
) );

I think that the same color should be allowed to exist in the palette for the use case of allowing a user to easily change their entire palette for a theme via the global controls (ie the customizer), and that the color selected in the editor should be based on the slug key opposed to using the color key as the slug is meant to be unique, whereas a color doesn't necessarily need to be.

On an instance by instance basis editing content, it might not make much sense to allow multiple colors, so one way to solve this issue would be to remove duplicate colors and prevent the errors from occurring. On the other hand as an example of where this is desirable: We have a theme author create a theme with our system and they assign a slot in the palette for the header background color, which is white, and they also assign another slot to the site content's background color, which is also white. An end user who has the theme and wishes to customize it might want to change the color of their header to something more contrasting with the site content's background, and they can easily do this. The same palette also impacts all of their site content, so it provides a way for a theme author supplying starter content in their themes a way to declare a relationship of how each slot in a palette effects elements on the site instead of tying the color directly to all elements just because it's the same, and ultimately gives the end users less flexibility.

The same issue would be evident when tackling #6941 as it would be easy for a user to select the same color twice as opposed to using a color they have already added in their palette.

Tested on Gutenberg v3.6.2 & WordPress v4.9.8

timelsass avatar Aug 26 '18 00:08 timelsass

I feel I mistakenly labeled this as a help request while it may make more sense as a feature request or enhancement and am correcting that now.

designsimply avatar Jan 29 '19 00:01 designsimply

I have the same problem with the theme I am developing. I also included a dark mode so now the problem is, that if there is the same color two times, always the first one is selected, although that one is thought for a totally different purpose, that way it can happen that the webpage looks not good anymore in one of the modes.

DesignerThan95 avatar Nov 10 '20 20:11 DesignerThan95

+1 here. Same issue with the same reasoning behind having duplicate colors.

JiveDig avatar Sep 09 '21 14:09 JiveDig

@timelsass Did you find a workaround? I'm curious about slightly altering the syntax rgb(0, 0, 0) vs rgb(0,0,0) vs rgb(0,0, 0) each time it's duplicated. Feels super hacky but the still curious ;P

JiveDig avatar Sep 09 '21 15:09 JiveDig

+1 this is a very big problem for the theme I’m creating also. I very much want users to be able to reuse the same colors with different names (slugs). I want them to be able to select header, title, footer, primary, etc. colors that the theme uses. Users will definitely want the ability to specify the same color # for more than one of those.

Gutenberg already places the slug into the source HTML instead of the color, but it just chooses the first slug with the selected color. We would just need it to return the slug of the color that the user selected Is a color picker Instead.

timnicholson avatar Apr 05 '22 15:04 timnicholson

I am still facing the same issue in 2023, I still don't understand why WordPress uses color as the unique factor. Where the color can be same from time to time. Using slug would be have been the best approach. This is a really big problem.

My current hacky workaround is to change the last digit or alphabet of the hex eg: Change this from #251046 -> #251045 , So one slug will have : #251046 and the other one will have #251045 , So that WordPress thinks that its a different color, Hence as per WordPress a different slug. And as per user experience are concerned the difference in the color is not noticeable at all . Hope this helps others as a hacky workaround for the time being, Till WordPress fixes this issue.

Before :

Animation22

After :

Animation22

nathanrodrigues2111 avatar May 05 '23 08:05 nathanrodrigues2111

This has caused some issues for us that were very difficult to track down and identify and really should be improved or fixed.

We are upgrading from WP 5.4 to WP 5.7. Sometime between 5.4 and 5.7 two new default colors were added by WP / Gutenberg (White and Black with #ffffff and #000000)

Our plugin was adding new color themes to the default list of colors and we have a color system that treats the colors as a "theme" and then applies that theme styling to the block if that has-color-<theme> class is applied. There are several colors palettes such as Primary, Secondary so classes are added as has-color-primary but we also had a light and dark option which the hex codes for the colors were set as #ffffff and #000000.

In 5.7 we were finding that when the user applied Light color the class has-color-white would be applied instead of has-color-light and so the styling for the "Light" theme was lost and not applied.

If instead of relying on the color value to apply the corresponding class name for the color it relied on the slug of the registered color it seems like this issue might go away.

Our current work around is as others have said to go through and change our Light and Dark registered colors to be a slightly different hex code. However that is going to require updating not just our plugin but a good number of themes that can use a filter to modify the registered colors.

If this can't be fixed, at a minimum some documentation should be noted so that people avoid registering colors that have the same color value as any other colors that are registered. It'd be nice to see a warning in the console or editor if 2 colors with the same color value are registered and are going to result in this issue happening and the wrong class name getting applied.

acketon avatar Jul 26 '23 15:07 acketon

How is this still not fixed? It destroys the very purpose of tokenizing themes.

metasaman avatar Apr 11 '24 20:04 metasaman

Reproduction Report

Description

āœ… This report validates that the issue can be reproduced.

Environment

  • WordPress: 6.9-alpha-60093-src
  • PHP: 8.2.29
  • Server: Apache/2.4.62 (Debian)
  • Database: mysqli (Server: 11.8.2-MariaDB-ubu2404 / Client: mysqlnd 8.2.29)
  • Browser: Chrome 138.0.0.0
  • OS: Windows 10/11
  • Theme: Twenty Twenty-Five 1.3
  • MU Plugins: None activated
  • Plugins:
    • Gutenberg 21.3.0-rc.1
    • Test Reports 1.2.0

Testing Instructions

  1. Add some extra colours to the theme.json settings > color > palette like this:
{
	"slug": "color-one",
	"name": "Color One",
	"color": "#ff0000"
},
{
	"slug": "color-two",
	"name": "Color Two",
	"color": "#ff0000"
},
  1. Add to blocks in the editor, for example two paragraphs
  2. Select any of these two colours for background, for example
  3. šŸž Both colours are selected at the same time

Actual Results

  1. āœ… Error condition occurs (reproduced).

Additional Notes

  • As many have pointed out, this is still live nowadays. It appears that the palette is prioritizing by the colour and not by the slug, which technically should be the unique ID for anything on WordPress.
  • For this reason, I think that this report is ready for patch and I believe it should not be very complex, so I think it is a good Good First Bug. If anyone is willing to take it, please ping me and I will be doing the follow-up testing.

Supplemental Artifacts

Screencast showcase: https://f003.backblazeb2.com/file/wordpress-videos/wp-videos/2025/07/gb9357.mp4

SirLouen avatar Jul 24 '25 22:07 SirLouen

@SirLouen I'd love to take this up and submit a fix. Could you please assign this issue to me?

ritoban23 avatar Jul 27 '25 22:07 ritoban23

@ritoban23 sure. Ping me when you have something ready and I will schedule it for a testing.

By the way I invite you to join Slack @ #core-test for contribution & informative sessions.

SirLouen avatar Jul 27 '25 22:07 SirLouen

Hey @ritoban23 how is going this issue? I would rather not have it locked if you can't work on it.

SirLouen avatar Aug 01 '25 11:08 SirLouen

@SirLouen Ran into a few blockers but close to resolving it. Will push an update soon.

ritoban23 avatar Aug 02 '25 03:08 ritoban23

Hey @ritoban23, is the feature close to finished?

catthetech avatar Jan 15 '26 20:01 catthetech

@catthetech hey I was unable to complete this succesfully...please feel free to work on this

ritoban23 avatar Jan 15 '26 21:01 ritoban23

That's unfortunate, do you still have the code you wrote to date?

catthetech avatar Jan 15 '26 21:01 catthetech

#78004 did not technically close this issue — but #78048 should

ciampo avatar May 11 '26 15:05 ciampo