Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ColorPalette utils: do not normalize undefined color values #64969

Merged
merged 4 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
### Bug Fixes

- `TimePicker`: use ToggleGroupControl for AM/PM toggle ([#64800](https://github.com/WordPress/gutenberg/pull/64800)).
- `ColorPalette` utils: do not normalize undefined color values ([#64969](https://github.com/WordPress/gutenberg/pull/64969)).

### Internal

Expand Down
23 changes: 21 additions & 2 deletions packages/components/src/color-palette/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,37 @@ describe( 'ColorPalette: Utils', () => {
} );

describe( 'normalizeColorValue', () => {
test( 'should return the value as is if the color value is not a CSS variable', () => {
test( 'should return the value if the value argument is not a CSS variable', () => {
const element = document.createElement( 'div' );
expect( normalizeColorValue( '#ff0000', element ) ).toBe(
'#ff0000'
);
} );
test( 'should return the background color computed from a element if the color value is a CSS variable', () => {
test( 'should return the background color computed from an element if the value argument is a CSS variable', () => {
const element = document.createElement( 'div' );
element.style.backgroundColor = '#ff0000';
expect( normalizeColorValue( 'var(--red)', element ) ).toBe(
'#ff0000'
);
} );
test( 'should return the background color computed from an element if the value argument is a color mix', () => {
const element = document.createElement( 'div' );
element.style.backgroundColor = '#ff0000';
expect(
normalizeColorValue(
'color-mix(in oklab, #a71e14, white)',
element
)
).toBe( '#ff0000' );
} );
test( 'should return the value if the value argument is undefined', () => {
const element = document.createElement( 'div' );
expect( normalizeColorValue( undefined, element ) ).toBe(
undefined
);
} );
test( 'should return the value if the element argument is null', () => {
expect( normalizeColorValue( '#ff0000', null ) ).toBe( '#ff0000' );
} );
} );
} );
4 changes: 1 addition & 3 deletions packages/components/src/color-palette/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ export const normalizeColorValue = (
value: string | undefined,
element: HTMLElement | null
) => {
const valueIsSimpleColor = value ? isSimpleCSSColor( value ) : false;

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

Expand Down
Loading