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 7 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 @@ -11,6 +11,7 @@
- `Dropdown`: deprecate `position` prop, use `popoverProps` instead ([46865](https://github.com/WordPress/gutenberg/pull/46865)).
- `Button`: improve padding for buttons with icon and text. ([46764](https://github.com/WordPress/gutenberg/pull/46764)).
- `ColorPalette`: Use computed color when css variable is passed to `ColorPicker` ([47181](https://github.com/WordPress/gutenberg/pull/47181)).
- `ColorPalette`: ensure text label contrast checking works with CSS variables ([#47373](https://github.com/WordPress/gutenberg/pull/47373)).

### Internal

Expand Down
42 changes: 34 additions & 8 deletions packages/components/src/color-palette/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import type { ForwardedRef } from 'react';
import { colord, extend } from 'colord';
import namesPlugin from 'colord/plugins/names';
import a11yPlugin from 'colord/plugins/a11y';
import mixPlugin from 'colord/plugins/mix';

/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { useCallback, useRef, useMemo, forwardRef } from '@wordpress/element';
import {
useCallback,
useMemo,
useState,
useRef,
forwardRef,
} from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -35,12 +42,13 @@ import type { WordPressComponentProps } from '../ui/context';
import type { DropdownProps } from '../dropdown/types';
import {
extractColorNameFromCurrentValue,
getCompositeBackgroundColor,
isMultiplePaletteArray,
normalizeColorValue,
showTransparentBackground,
} from './utils';

extend( [ namesPlugin, a11yPlugin ] );
extend( [ namesPlugin, a11yPlugin, mixPlugin ] );

function SinglePalette( {
className,
Expand Down Expand Up @@ -185,8 +193,21 @@ function UnforwardedColorPalette(
__experimentalIsRenderedInSidebar = false,
...otherProps
} = props;
const [ normalizedColorValue, setNormalizedColorValue ] = useState( value );
const clearColor = useCallback( () => onChange( undefined ), [ onChange ] );

const customColorPaletteCallbackRef = useCallback(
( ref: HTMLElement | null ) => {
if ( ref ) {
customColorPaletteRef.current = ref;
setNormalizedColorValue(
normalizeColorValue( value, customColorPaletteRef )
);
}
},
[ value ]
);

const hasMultipleColorOrigins = isMultiplePaletteArray( colors );
const buttonLabelName = useMemo(
() =>
Expand All @@ -201,15 +222,18 @@ 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 compositeColor = getCompositeBackgroundColor(
colord( '#fff' ),
colordColor
);
const valueWithoutLeadingHash = value?.startsWith( '#' )
? value.substring( 1 )
: value ?? '';
Expand Down Expand Up @@ -246,7 +270,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 All @@ -260,8 +284,10 @@ function UnforwardedColorPalette(
: {
background: value,
color:
colordColor.contrast() >
colordColor.contrast( '#000' )
compositeColor.contrast() >
compositeColor.contrast(
'#000'
)
? '#fff'
: '#000',
}
Expand Down
15 changes: 14 additions & 1 deletion packages/components/src/color-palette/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
* External dependencies
*/
import type { RefObject } from 'react';
import type { Colord } from 'colord';
import { colord, extend } from 'colord';
import namesPlugin from 'colord/plugins/names';
import a11yPlugin from 'colord/plugins/a11y';
import mixPlugin from 'colord/plugins/mix';

/**
* WordPress dependencies
Expand All @@ -16,7 +18,7 @@ import { __ } from '@wordpress/i18n';
*/
import type { ColorObject, ColorPaletteProps, PaletteObject } from './types';

extend( [ namesPlugin, a11yPlugin ] );
extend( [ namesPlugin, a11yPlugin, mixPlugin ] );

export const extractColorNameFromCurrentValue = (
currentValue?: ColorPaletteProps[ 'value' ],
Expand Down Expand Up @@ -96,3 +98,14 @@ export const normalizeColorValue = (
? colord( computedBackgroundColor ).toHex()
: value;
};

// Composite with background color behind when the front background color is transparent
// to get the actual background color.
export const getCompositeBackgroundColor = (
behindColor: Colord,
frontColor: Colord
) => {
return frontColor.alpha() < 1
? behindColor.mix( frontColor.alpha( 1 ), frontColor.alpha() )
: frontColor;
};