-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ColorPalette, BorderControl, GradientPicker: refine types and logic a…
…round single vs multiple palettes (#47384) * Move ColorPalette utils to separate file * Rewrite multiple palette object/array check * Remove unnecessary check * Tweak type to more correct version * Reuse multiple origin check in BorderControl * Apply similar logic for GradientPicker * CHANGELOG * Initialize colord plugins in utils file
- Loading branch information
Showing
8 changed files
with
138 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { RefObject } from 'react'; | ||
import { colord, extend } from 'colord'; | ||
import namesPlugin from 'colord/plugins/names'; | ||
import a11yPlugin from 'colord/plugins/a11y'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { ColorObject, ColorPaletteProps, PaletteObject } from './types'; | ||
|
||
extend( [ namesPlugin, a11yPlugin ] ); | ||
|
||
export const extractColorNameFromCurrentValue = ( | ||
currentValue?: ColorPaletteProps[ 'value' ], | ||
colors: ColorPaletteProps[ 'colors' ] = [], | ||
showMultiplePalettes: boolean = false | ||
) => { | ||
if ( ! currentValue ) { | ||
return ''; | ||
} | ||
|
||
const currentValueIsCssVariable = /^var\(/.test( currentValue ); | ||
const normalizedCurrentValue = currentValueIsCssVariable | ||
? currentValue | ||
: colord( currentValue ).toHex(); | ||
|
||
// Normalize format of `colors` to simplify the following loop | ||
type normalizedPaletteObject = { colors: ColorObject[] }; | ||
const colorPalettes: normalizedPaletteObject[] = showMultiplePalettes | ||
? ( colors as PaletteObject[] ) | ||
: [ { 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(); | ||
|
||
if ( normalizedCurrentValue === normalizedColorValue ) { | ||
return colorName; | ||
} | ||
} | ||
} | ||
|
||
// translators: shown when the user has picked a custom color (i.e not in the palette of colors). | ||
return __( 'Custom' ); | ||
}; | ||
|
||
export const showTransparentBackground = ( currentValue?: string ) => { | ||
if ( typeof currentValue === 'undefined' ) { | ||
return true; | ||
} | ||
return colord( currentValue ).alpha() === 0; | ||
}; | ||
|
||
// The PaletteObject type has a `colors` property (an array of ColorObject), | ||
// while the ColorObject type has a `color` property (the CSS color value). | ||
export const isMultiplePaletteObject = ( | ||
obj: PaletteObject | ColorObject | ||
): obj is PaletteObject => | ||
Array.isArray( ( obj as PaletteObject ).colors ) && ! ( 'color' in obj ); | ||
|
||
export const isMultiplePaletteArray = ( | ||
arr: ( PaletteObject | ColorObject )[] | ||
): arr is PaletteObject[] => { | ||
return ( | ||
arr.length > 0 && | ||
arr.every( ( colorObj ) => isMultiplePaletteObject( colorObj ) ) | ||
); | ||
}; | ||
|
||
export const normalizeColorValue = ( | ||
value: string | undefined, | ||
ref: RefObject< HTMLElement > | null | ||
) => { | ||
const currentValueIsCssVariable = /^var\(/.test( value ?? '' ); | ||
|
||
if ( ! currentValueIsCssVariable || ! ref?.current ) { | ||
return value; | ||
} | ||
|
||
const { ownerDocument } = ref.current; | ||
const { defaultView } = ownerDocument; | ||
const computedBackgroundColor = defaultView?.getComputedStyle( | ||
ref.current | ||
).backgroundColor; | ||
|
||
return computedBackgroundColor | ||
? colord( computedBackgroundColor ).toHex() | ||
: value; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ac4d1db
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected in ac4d1db.
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/4005295256
📝 Reported issues:
specs/editor/various/block-editor-keyboard-shortcuts.test.js