Skip to content

Commit

Permalink
More explicit Single vs Multiple conditional
Browse files Browse the repository at this point in the history
  • Loading branch information
chad1008 committed Feb 22, 2023
1 parent 29687ab commit c43ea5c
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions packages/components/src/gradient-picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ import type {
// For Multiple Origin Gradients, the `gradients` property is an array of
// objects, each with a `name` property and an internal `gradients` property,
// which is the array of Gradient objects.
const isMultipleOrigin = ( arr: GradientsProp ) => {
// NTS: We can't use `Array.every` here because of a TS bug https://github.com/microsoft/TypeScript/issues/44373
let allElementsAreOrigins;
for ( const gradientObj of arr ) {
allElementsAreOrigins = 'gradients' in gradientObj;
if ( ! allElementsAreOrigins ) break;
}
return arr !== undefined && arr.length > 0 && allElementsAreOrigins;
const isMultipleOrigin = ( arr: GradientsProp ): arr is OriginObject[] => {
return ( arr as any[] ).every( ( obj ) => 'gradients' in obj );
};

function SingleOrigin( {
Expand Down Expand Up @@ -116,6 +110,13 @@ function MultipleOrigin( {
);
}

function Component( props: PickerProps< any > ) {
if ( isMultipleOrigin( props.gradients ) ) {
return <MultipleOrigin { ...props } />;
}
return <SingleOrigin { ...props } />;
}

export default function GradientPicker( {
/** Start opting into the new margin-free styles that will become the default in a future version. */
__nextHasNoMargin = false,
Expand All @@ -132,9 +133,9 @@ export default function GradientPicker( {
() => onChange( undefined ),
[ onChange ]
);
const Component = isMultipleOrigin( gradients )
? MultipleOrigin
: SingleOrigin;
// const Component = isMultipleOrigin( gradients )
// ? MultipleOrigin
// : SingleOrigin;

if ( ! __nextHasNoMargin ) {
deprecated( 'Outer margin styles for wp.components.GradientPicker', {
Expand Down

0 comments on commit c43ea5c

Please sign in to comment.