Skip to content

Commit

Permalink
ColorPalette: partial support of color-mix() CSS colors (#64224)
Browse files Browse the repository at this point in the history
* Fix incorrect dropdown label with selected color value

* Fix normalizeColorValue function

* Add isSimpleCSSColor function

* Fix isSimpleCSSColor function, add early return

* Update CHANGELOG.md with ColorPalette: partial support of color-mix() CSS colors

* Update packages/components/CHANGELOG.md

---------

Co-authored-by: Rishit30G <[email protected]>
Co-authored-by: ciampo <[email protected]>
Co-authored-by: mirka <[email protected]>
Co-authored-by: tyxla <[email protected]>
  • Loading branch information
5 people authored Aug 20, 2024
1 parent 7f1523c commit 3ed1812
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@

### Enhancements

- `ColorPalette`: partial support of `color-mix()` CSS colors ([#64224](https://github.com/WordPress/gutenberg/pull/64224)).
- `TimeInput`: Expose as subcomponent of `TimePicker` ([#63145](https://github.com/WordPress/gutenberg/pull/63145)).
- `RadioControl`: add support for option help text ([#63751](https://github.com/WordPress/gutenberg/pull/63751)).
- `SelectControl`: Infer `value` type from `options` ([#64069](https://github.com/WordPress/gutenberg/pull/64069)).
Expand Down
33 changes: 23 additions & 10 deletions packages/components/src/color-palette/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ import type { ColorObject, ColorPaletteProps, PaletteObject } from './types';

extend( [ namesPlugin, a11yPlugin ] );

/**
* Checks if a color value is a simple CSS color.
*
* @param value The color value to check.
* @return A boolean indicating whether the color value is a simple CSS color.
*/
const isSimpleCSSColor = ( value: string ): boolean => {
const valueIsCssVariable = /var\(/.test( value ?? '' );
const valueIsColorMix = /color-mix\(/.test( value ?? '' );
return ! valueIsCssVariable && ! valueIsColorMix;
};

export const extractColorNameFromCurrentValue = (
currentValue?: ColorPaletteProps[ 'value' ],
colors: ColorPaletteProps[ 'colors' ] = [],
Expand All @@ -25,11 +37,12 @@ export const extractColorNameFromCurrentValue = (
if ( ! currentValue ) {
return '';
}

const currentValueIsCssVariable = /^var\(/.test( currentValue );
const normalizedCurrentValue = currentValueIsCssVariable
? currentValue
: colord( currentValue ).toHex();
const currentValueIsSimpleColor = currentValue
? isSimpleCSSColor( currentValue )
: false;
const normalizedCurrentValue = currentValueIsSimpleColor
? colord( currentValue ).toHex()
: currentValue;

// Normalize format of `colors` to simplify the following loop
type normalizedPaletteObject = { colors: ColorObject[] };
Expand All @@ -38,9 +51,9 @@ export const extractColorNameFromCurrentValue = (
: [ { colors: colors as ColorObject[] } ];
for ( const { colors: paletteColors } of colorPalettes ) {
for ( const { name: colorName, color: colorValue } of paletteColors ) {
const normalizedColorValue = currentValueIsCssVariable
? colorValue
: colord( colorValue ).toHex();
const normalizedColorValue = currentValueIsSimpleColor
? colord( colorValue ).toHex()
: colorValue;

if ( normalizedCurrentValue === normalizedColorValue ) {
return colorName;
Expand Down Expand Up @@ -79,9 +92,9 @@ export const normalizeColorValue = (
value: string | undefined,
element: HTMLElement | null
) => {
const currentValueIsCssVariable = /^var\(/.test( value ?? '' );
const valueIsSimpleColor = value ? isSimpleCSSColor( value ) : false;

if ( ! currentValueIsCssVariable || element === null ) {
if ( valueIsSimpleColor || element === null ) {
return value;
}

Expand Down

1 comment on commit 3ed1812

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 3ed1812.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/10469163754
📝 Reported issues:

Please sign in to comment.