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

Global styles: add onChange actions to color palette items #45681

Merged
merged 6 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions packages/components/src/color-palette/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function SinglePalette( {
}
style={ { backgroundColor: color, color } }
onClick={
isSelected ? clearColor : () => onChange( color )
isSelected ? clearColor : () => onChange( color, index )
}
aria-label={
name
Expand Down Expand Up @@ -118,7 +118,9 @@ function MultiplePalettes( {
<SinglePalette
clearColor={ clearColor }
colors={ colorPalette }
onChange={ onChange }
onChange={ ( newColor ) =>
onChange( newColor, index )
}
value={ value }
actions={
colors.length === index + 1 ? actions : null
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/color-palette/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe( 'ColorPalette', () => {
);

expect( onChange ).toHaveBeenCalledTimes( 1 );
expect( onChange ).toHaveBeenCalledWith( '#fff' );
expect( onChange ).toHaveBeenCalledWith( '#fff', 1 );
} );

test( 'should call onClick with undefined, when the clearButton onClick is triggered', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/color-palette/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type PaletteProps = {
/**
* Callback called when a color is selected.
*/
onChange: ( newColor?: string ) => void;
onChange: ( newColor?: string, index?: number ) => void;
value?: string;
actions?: ReactNode;
};
Expand Down
13 changes: 5 additions & 8 deletions packages/components/src/gradient-picker/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { map } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -28,7 +23,7 @@ function SingleOrigin( {
actions,
} ) {
const gradientOptions = useMemo( () => {
return map( gradients, ( { gradient, name } ) => (
return gradients.map( ( { gradient, name }, index ) => (
<CircularOptionPicker.Option
key={ gradient }
value={ gradient }
Expand All @@ -42,7 +37,7 @@ function SingleOrigin( {
onClick={
value === gradient
? clearGradient
: () => onChange( gradient )
: () => onChange( gradient, index )
}
aria-label={
name
Expand Down Expand Up @@ -80,7 +75,9 @@ function MultipleOrigin( {
<SingleOrigin
clearGradient={ clearGradient }
gradients={ gradientSet }
onChange={ onChange }
onChange={ ( gradient ) =>
onChange( gradient, index )
}
value={ value }
{ ...( gradients.length === index + 1
? { actions }
Expand Down
127 changes: 88 additions & 39 deletions packages/components/src/palette-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { kebabCase } from 'lodash';
/**
* WordPress dependencies
*/
import { useState, useRef, useEffect } from '@wordpress/element';
import { useState, useRef, useEffect, useCallback } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { lineSolid, moreVertical, plus } from '@wordpress/icons';
import {
Expand Down Expand Up @@ -89,6 +89,50 @@ export function getNameForPosition( elements, slugPrefix ) {
);
}

function ColorPickerPopover( {
isGradient,
element,
onChange,
onClose = () => {},
} ) {
return (
<Popover
placement="left-start"
offset={ 20 }
className="components-palette-edit__popover"
onClose={ onClose }
>
{ ! isGradient && (
<ColorPicker
color={ element.color }
enableAlpha
onChange={ ( newColor ) =>
onChange( {
...element,
color: newColor,
} )
}
/>
) }
{ isGradient && (
<div className="components-palette-edit__popover-gradient-picker">
<CustomGradientPicker
__nextHasNoMargin
__experimentalIsRenderedInSidebar
value={ element.gradient }
onChange={ ( newGradient ) =>
onChange( {
...element,
gradient: newGradient,
} )
}
/>
</div>
) }
</Popover>
);
}

function Option( {
canOnlyChangeValues,
element,
Expand Down Expand Up @@ -155,39 +199,11 @@ function Option( {
) }
</HStack>
{ isEditing && (
<Popover
placement="left-start"
offset={ 20 }
className="components-palette-edit__popover"
>
{ ! isGradient && (
<ColorPicker
color={ value }
enableAlpha
onChange={ ( newColor ) =>
onChange( {
...element,
color: newColor,
} )
}
/>
) }
{ isGradient && (
<div className="components-palette-edit__popover-gradient-picker">
<CustomGradientPicker
__nextHasNoMargin
__experimentalIsRenderedInSidebar
value={ value }
onChange={ ( newGradient ) =>
onChange( {
...element,
gradient: newGradient,
} )
}
/>
</div>
) }
</Popover>
<ColorPickerPopover
isGradient={ isGradient }
onChange={ onChange }
element={ element }
/>
) }
</PaletteItem>
);
Expand Down Expand Up @@ -313,6 +329,20 @@ export default function PaletteEdit( {
! elements[ editingElement ].slug;
const elementsLength = elements.length;
const hasElements = elementsLength > 0;
const debounceOnChange = useDebounce( onChange, 100 );
const onSelectPaletteItem = useCallback(
( value, newEditingElementIndex ) => {
const selectedElement = elements[ newEditingElementIndex ];
const key = isGradient ? 'gradient' : 'color';
// Ensures that the index returned matches a known element value.
if ( !! selectedElement && selectedElement[ key ] === value ) {
setEditingElement( newEditingElementIndex );
} else {
setIsEditing( true );
}
},
[ isGradient, elements ]
);

return (
<PaletteEditStyles>
Expand Down Expand Up @@ -391,9 +421,7 @@ export default function PaletteEdit( {
} }
className="components-palette-edit__menu-button"
>
{ isGradient
? __( 'Edit gradients' )
: __( 'Edit colors' ) }
{ __( 'Show details' ) }
</Button>
) }
{ ! canOnlyChangeValues && (
Expand Down Expand Up @@ -454,19 +482,40 @@ export default function PaletteEdit( {
isGradient={ isGradient }
/>
) }
{ ! isEditing && editingElement !== null && (
<ColorPickerPopover
isGradient={ isGradient }
onClose={ () => setEditingElement( null ) }
onChange={ ( newElement ) => {
debounceOnChange(
elements.map(
( currentElement, currentIndex ) => {
if (
currentIndex === editingElement
) {
return newElement;
}
return currentElement;
}
)
);
} }
element={ elements[ editingElement ] }
/>
) }
{ ! isEditing &&
( isGradient ? (
<GradientPicker
__nextHasNoMargin
gradients={ gradients }
onChange={ () => {} }
onChange={ onSelectPaletteItem }
clearable={ false }
disableCustomGradients={ true }
/>
) : (
<ColorPalette
colors={ colors }
onChange={ () => {} }
onChange={ onSelectPaletteItem }
clearable={ false }
disableCustomColors={ true }
/>
Expand Down