Conversation
| colorValue: CSSProperties[ 'borderColor' ], | ||
| colors: ColorProps[ 'colors' ] | undefined | ||
| ) => { | ||
| if ( ! colorValue || ! colors || colors.length === 0 ) { |
There was a problem hiding this comment.
This check is now embedded in isMultiplePaletteArray
| // Multiple origins | ||
| let matchedColor; | ||
|
|
||
| ( colors as PaletteObject[] ).some( ( origin ) => |
There was a problem hiding this comment.
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.
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
| * @default [] | ||
| */ | ||
| colors?: ( PaletteObject | ColorObject )[]; | ||
| colors?: PaletteObject[] | ColorObject[]; |
There was a problem hiding this comment.
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.
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.
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.
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
|
aaronrobertshaw
left a comment
There was a problem hiding this comment.
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
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:
ColorPaletteutilities to a separateutilsfilelenghtcheck)ColorPalettethat isn't necessary anymore since the removal of the__experimentalHasMultipleOriginsprop in Remove: __experimentalHasMultipleOrigins prop from colors and gradients #46315ColorPalettelogic inBorderControlinstead of custom check (which allows to simplify some types and checks too!)GradientPickertooYou can review this PR commit-by-commit too
Testing Instructions
In Storybook, check that
ColorPalette,GradientPickerandBorderControlcomponents 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.