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: Ensure text label contrast checking works with CSS variables #47373

Merged
merged 16 commits into from
Feb 7, 2023
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 @@ -5,6 +5,7 @@
### Enhancements

- `ColorPalette`, `GradientPicker`, `PaletteEdit`, `ToolsPanel`: add new props to set a custom heading level ([43848](https://github.com/WordPress/gutenberg/pull/43848) and [#47788](https://github.com/WordPress/gutenberg/pull/47788)).
- `ColorPalette`: ensure text label contrast checking works with CSS variables ([#47373](https://github.com/WordPress/gutenberg/pull/47373)).

### Internal

Expand Down
17 changes: 12 additions & 5 deletions packages/components/src/color-palette/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import a11yPlugin from 'colord/plugins/a11y';
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { useCallback, useRef, useMemo, forwardRef } from '@wordpress/element';
import { useCallback, useMemo, useState, forwardRef } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -177,7 +177,6 @@ function UnforwardedColorPalette(
props: WordPressComponentProps< ColorPaletteProps, 'div' >,
forwardedRef: ForwardedRef< any >
) {
const customColorPaletteRef = useRef< HTMLElement | null >( null );
const {
clearable = true,
colors = [],
Expand All @@ -189,9 +188,17 @@ function UnforwardedColorPalette(
headingLevel = 2,
...otherProps
} = props;
const [ normalizedColorValue, setNormalizedColorValue ] = useState( value );

const clearColor = useCallback( () => onChange( undefined ), [ onChange ] );

const customColorPaletteCallbackRef = useCallback(
( node: HTMLElement | null ) => {
setNormalizedColorValue( normalizeColorValue( value, node ) );
},
[ value ]
);

const hasMultipleColorOrigins = isMultiplePaletteArray( colors );
const buttonLabelName = useMemo(
() =>
Expand All @@ -206,14 +213,14 @@ function UnforwardedColorPalette(
const renderCustomColorPicker = () => (
<DropdownContentWrapper paddingSize="none">
<ColorPicker
color={ normalizeColorValue( value, customColorPaletteRef ) }
color={ normalizedColorValue }
onChange={ ( color ) => onChange( color ) }
enableAlpha={ enableAlpha }
/>
</DropdownContentWrapper>
);

const colordColor = colord( value ?? '' );
const colordColor = colord( normalizedColorValue ?? '' );

const valueWithoutLeadingHash = value?.startsWith( '#' )
? value.substring( 1 )
Expand Down Expand Up @@ -252,7 +259,7 @@ function UnforwardedColorPalette(
renderToggle={ ( { isOpen, onToggle } ) => (
<Flex
as={ 'button' }
ref={ customColorPaletteRef }
ref={ customColorPaletteCallbackRef }
justify="space-between"
align="flex-start"
className="components-color-palette__custom-color"
Expand Down
18 changes: 17 additions & 1 deletion packages/components/src/color-palette/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import {
extractColorNameFromCurrentValue,
showTransparentBackground,
normalizeColorValue,
} from '../utils';

describe( 'ColorPalette: Utils', () => {
Expand Down Expand Up @@ -32,11 +33,26 @@ describe( 'ColorPalette: Utils', () => {
expect( showTransparentBackground( 'transparent' ) ).toBe( true );
expect( showTransparentBackground( '#75757500' ) ).toBe( true );
} );

test( 'should return false for non-transparent colors', () => {
expect( showTransparentBackground( '#FFF' ) ).toBe( false );
expect( showTransparentBackground( '#757575' ) ).toBe( false );
expect( showTransparentBackground( '#f5f5f524' ) ).toBe( false ); // 0.14 alpha.
} );
} );

describe( 'normalizeColorValue', () => {
test( 'should return the value as is if the color value 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', () => {
const element = document.createElement( 'div' );
element.style.backgroundColor = '#ff0000';
expect( normalizeColorValue( 'var(--red)', element ) ).toBe(
'#ff0000'
);
} );
} );
} );
19 changes: 12 additions & 7 deletions packages/components/src/color-palette/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import type { RefObject } from 'react';
import { colord, extend } from 'colord';
import namesPlugin from 'colord/plugins/names';
import a11yPlugin from 'colord/plugins/a11y';
Expand Down Expand Up @@ -76,21 +75,27 @@ export const isMultiplePaletteArray = (
);
};

/**
* Transform a CSS variable used as background color into the color value itself.
*
* @param value The color value that may be a CSS variable.
* @param element The element for which to get the computed style.
* @return The background color value computed from a element.
*/
export const normalizeColorValue = (
value: string | undefined,
ref: RefObject< HTMLElement > | null
element: HTMLElement | null
) => {
const currentValueIsCssVariable = /^var\(/.test( value ?? '' );

if ( ! currentValueIsCssVariable || ! ref?.current ) {
if ( ! currentValueIsCssVariable || element === null ) {
return value;
}

const { ownerDocument } = ref.current;
const { ownerDocument } = element;
const { defaultView } = ownerDocument;
const computedBackgroundColor = defaultView?.getComputedStyle(
ref.current
).backgroundColor;
const computedBackgroundColor =
defaultView?.getComputedStyle( element ).backgroundColor;

return computedBackgroundColor
? colord( computedBackgroundColor ).toHex()
Expand Down