Skip to content

Commit

Permalink
Allow passing popover props for palette popovers
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Apr 21, 2023
1 parent a1456eb commit d1bcaa6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
39 changes: 36 additions & 3 deletions packages/components/src/palette-edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import { paramCase as kebabCase } from 'change-case';
/**
* WordPress dependencies
*/
import { useState, useRef, useEffect, useCallback } from '@wordpress/element';
import {
useState,
useRef,
useEffect,
useCallback,
useMemo,
} from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { lineSolid, moreVertical, plus } from '@wordpress/icons';
import {
Expand Down Expand Up @@ -106,13 +112,20 @@ function ColorPickerPopover< T extends Color | Gradient >( {
isGradient,
element,
onChange,
popoverProps: receivedPopoverProps,
onClose = () => {},
}: ColorPickerPopoverProps< T > ) {
const popoverProps: ColorPickerPopoverProps< T >[ 'popoverProps' ] = {
shift: true,
offset: 20,
placement: 'left-start',
...receivedPopoverProps,
};

return (
<Popover
placement="left-start"
offset={ 20 }
className="components-palette-edit__popover"
{ ...popoverProps }
onClose={ onClose }
>
{ ! isGradient && (
Expand Down Expand Up @@ -154,17 +167,31 @@ function Option< T extends Color | Gradient >( {
onStartEditing,
onRemove,
onStopEditing,
popoverProps: receivedPopoverProps,
slugPrefix,
isGradient,
}: OptionProps< T > ) {
const focusOutsideProps = useFocusOutside( onStopEditing );
const value = isGradient ? element.gradient : element.color;

// Use internal state instead of a ref to make sure that the component
// re-renders when the popover's anchor updates.
const [ popoverAnchor, setPopoverAnchor ] = useState( null );
const popoverProps = useMemo(
() => ( {
...receivedPopoverProps,
// Use the custom palette color item as the popover anchor.
anchor: popoverAnchor,
} ),
[ popoverAnchor, receivedPopoverProps ]
);

return (
<PaletteItem
className={ isEditing ? 'is-selected' : undefined }
as="div"
onClick={ onStartEditing }
ref={ setPopoverAnchor }
{ ...( isEditing
? { ...focusOutsideProps }
: {
Expand Down Expand Up @@ -218,6 +245,7 @@ function Option< T extends Color | Gradient >( {
isGradient={ isGradient }
onChange={ onChange }
element={ element }
popoverProps={ popoverProps }
/>
) }
</PaletteItem>
Expand All @@ -244,6 +272,7 @@ function PaletteEditListView< T extends Color | Gradient >( {
canOnlyChangeValues,
slugPrefix,
isGradient,
popoverProps,
}: PaletteEditListViewProps< T > ) {
// When unmounting the component if there are empty elements (the user did not complete the insertion) clean them.
const elementsReference = useRef< typeof elements >();
Expand Down Expand Up @@ -317,6 +346,7 @@ function PaletteEditListView< T extends Color | Gradient >( {
}
} }
slugPrefix={ slugPrefix }
popoverProps={ popoverProps }
/>
) ) }
</ItemGroup>
Expand Down Expand Up @@ -356,6 +386,7 @@ export function PaletteEdit( {
canOnlyChangeValues,
canReset,
slugPrefix = '',
popoverProps,
}: PaletteEditProps ) {
const isGradient = !! gradients;
const elements = isGradient ? gradients : colors;
Expand Down Expand Up @@ -541,6 +572,7 @@ export function PaletteEdit( {
setEditingElement={ setEditingElement }
slugPrefix={ slugPrefix }
isGradient={ isGradient }
popoverProps={ popoverProps }
/>
) }
{ ! isEditing && editingElement !== null && (
Expand Down Expand Up @@ -568,6 +600,7 @@ export function PaletteEdit( {
);
} }
element={ elements[ editingElement ?? -1 ] }
popoverProps={ popoverProps }
/>
) }
{ ! isEditing &&
Expand Down
8 changes: 7 additions & 1 deletion packages/components/src/palette-edit/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { Key, MouseEventHandler } from 'react';
* Internal dependencies
*/
import type { HeadingSize } from '../heading/types';
import type { PopoverProps } from '../popover/types';

export type Color = {
color: string;
Expand Down Expand Up @@ -85,7 +86,9 @@ type PaletteEditGradients = {
};

export type PaletteEditProps = BasePaletteEdit &
( PaletteEditColors | PaletteEditGradients );
( PaletteEditColors | PaletteEditGradients ) & {
popoverProps?: Omit< PopoverProps, 'children' >;
};

type EditingElement = number | null;

Expand All @@ -94,6 +97,7 @@ export type ColorPickerPopoverProps< T extends Color | Gradient > = {
onChange: ( newElement: T ) => void;
isGradient?: T extends Gradient ? true : false;
onClose?: () => void;
popoverProps?: Omit< PopoverProps, 'children' >;
};

export type NameInputProps = {
Expand All @@ -112,6 +116,7 @@ export type OptionProps< T extends Color | Gradient > = {
onRemove: MouseEventHandler< HTMLButtonElement >;
onStartEditing: () => void;
onStopEditing: () => void;
popoverProps?: Omit< PopoverProps, 'children' >;
slugPrefix: string;
};

Expand All @@ -121,6 +126,7 @@ export type PaletteEditListViewProps< T extends Color | Gradient > = {
isGradient: T extends Gradient ? true : false;
canOnlyChangeValues: PaletteEditProps[ 'canOnlyChangeValues' ];
editingElement?: EditingElement;
popoverProps?: Omit< PopoverProps, 'children' >;
setEditingElement: ( newEditingElement?: EditingElement ) => void;
slugPrefix: string;
};

0 comments on commit d1bcaa6

Please sign in to comment.