-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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, BorderControl, GradientPicker: refine types and logic around single vs multiple palettes #47384
ColorPalette, BorderControl, GradientPicker: refine types and logic around single vs multiple palettes #47384
Conversation
import type { DropdownProps as DropdownComponentProps } from '../../dropdown/types'; | ||
import type { ColorProps, DropdownProps } from '../types'; | ||
|
||
const getColorObject = ( | ||
colorValue: CSSProperties[ 'borderColor' ], | ||
colors: ColorProps[ 'colors' ] | undefined | ||
) => { | ||
if ( ! colorValue || ! colors || colors.length === 0 ) { |
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.
This check is now embedded in isMultiplePaletteArray
let matchedColor; | ||
|
||
( colors as PaletteObject[] ).some( ( origin ) => |
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.
Since isMultiplePaletteArray
acts as a type guard, the cast to PaletteObject[]
and to ColorObject[]
isn't necessary anymore
// Make sure that the `colors` array has a valid format. | ||
if ( | ||
colors.length > 0 && | ||
hasMultipleColorOrigins !== areColorsMultiplePalette( colors ) | ||
) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
'wp.components.ColorPalette: please specify a valid format for the `colors` prop. ' | ||
); | ||
return null; | ||
} | ||
|
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.
This check isn't necessary anymore, because its goal was to check that the colors
array and the __experimentalHasMultipleOrigins
were in sync. Now that the __experimentalHasMultipleOrigins
has been removed, we can safely delete the check
@@ -55,7 +55,7 @@ export type ColorPaletteProps = Pick< PaletteProps, 'onChange' > & { | |||
* | |||
* @default [] | |||
*/ | |||
colors?: ( PaletteObject | ColorObject )[]; | |||
colors?: PaletteObject[] | ColorObject[]; |
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.
This change is to reflect that the colors
array cannot contain mixed PaletteObject
and ColorObject
items
// 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 ) ) | ||
); | ||
}; |
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.
This is the only part that changes in this file. The rest of the lines are a simple copy/paste from index.tsx
const Component = | ||
gradients?.length && gradients[ 0 ].gradients | ||
? MultipleOrigin | ||
: SingleOrigin; | ||
const Component = isMultipleOriginArray( gradients ) | ||
? MultipleOrigin | ||
: SingleOrigin; |
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.
Applied the same logic used for ColorPalette
also to GradientPicker
, with the only difference that instead of checking for colors
and color
properties, we check for gradients
and gradient
*/ | ||
import type { ColorObject, ColorPaletteProps, PaletteObject } from './types'; | ||
|
||
extend( [ namesPlugin, a11yPlugin ] ); |
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.
I had to add colord
extensions here as well, since without it the unit tests would fail. I don't think that this should be problematic, but better to double-check too
Size Change: -22 B (0%) Total Size: 1.33 MB
ℹ️ View Unchanged
|
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.
Great work @ciampo! ✨
These changes make sense and clean things up nicely. Thanks for all the inline comments providing context, they make reviewing a pleasure.
✅ Didn't spot any issues smoke testing various controls in both editors
✅ ColorPalette, GradientPicker & BorderControl components function as per trunk in Storybook
✅ All available unit tests for impacted components pass
✅ No typing errors
LGTM 🚢
…gin-types-and-utils
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.
Great work @ciampo! Self-review has been super helpful 🚀
What?
As a follow-up to #46315, this PR further cleans up some of the logic around single vs multiple origin color palettes.
Why?
Code quality (re-use same checks in different components, add comments, tighten up types)
How?
Here's the main set of changes applies in this PR:
ColorPalette
utilities to a separateutils
filelenght
check)ColorPalette
that isn't necessary anymore since the removal of the__experimentalHasMultipleOrigins
prop in Remove: __experimentalHasMultipleOrigins prop from colors and gradients #46315ColorPalette
logic inBorderControl
instead of custom check (which allows to simplify some types and checks too!)GradientPicker
tooYou can review this PR commit-by-commit too
Testing Instructions
In Storybook, check that
ColorPalette
,GradientPicker
andBorderControl
components behave like ontrunk
. Pay particular attention to testing these components with single vs multiple origin colors/gradients.Smoke test the block editor and make sure that the above components do not show any regressions.