From 8577539576e2e1430e049e26f6dcbe2998f87dfb Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Wed, 5 Apr 2023 13:32:07 +0200 Subject: [PATCH] show palettes by group --- packages/block-editor/src/hooks/duotone.js | 23 ++-- .../src/duotone-picker/duotone-picker.tsx | 114 ++++++++++++++---- .../components/src/duotone-picker/types.ts | 40 ++++-- 3 files changed, 140 insertions(+), 37 deletions(-) diff --git a/packages/block-editor/src/hooks/duotone.js b/packages/block-editor/src/hooks/duotone.js index 934f1c9dfb7a59..22c0414b9ac61d 100644 --- a/packages/block-editor/src/hooks/duotone.js +++ b/packages/block-editor/src/hooks/duotone.js @@ -93,11 +93,20 @@ function useGroupedPresets( { presetSetting, defaultSetting } ) { useSetting( `${ presetSetting }.default` ) || EMPTY_ARRAY; return useMemo( () => { - return { - user: userPresets, - theme: themePresets, - default: disableDefault ? EMPTY_ARRAY : defaultPresets, - }; + return [ + { + name: 'User', + palettes: userPresets, + }, + { + name: 'Theme', + palettes: themePresets, + }, + { + name: 'Default', + palettes: disableDefault ? EMPTY_ARRAY : defaultPresets, + }, + ]; }, [ disableDefault, userPresets, themePresets, defaultPresets ] ); } @@ -162,7 +171,7 @@ function DuotonePanel( { attributes, setAttributes } ) { __experimentalShareWithChildBlocks > { +function paletteOptions( { palette, value, onChange }: paletteOptionsProps ) { + return palette.map( ( { colors, slug, name } ) => { const style = { background: getGradientFromCSSColors( colors, '135deg' ), color: 'transparent', @@ -71,6 +63,27 @@ function SinglePalette( { /> ); } ); +} + +function SinglePalette( { + defaultDark, + defaultLight, + clearable, + unsetable, + colorPalette, + duotonePalette, + disableCustomColors, + disableCustomDuotone, + value, + unsetOption, + onChange, +}: SinglePaletteProps ) { + const colorValue = value && value !== 'unset' ? value : undefined; + const options = paletteOptions( { + palette: duotonePalette, + value, + onChange, + } ); return ( + { duotonePaletteByOrigin + .filter( ( { palettes } ) => palettes.length > 0 ) + .map( ( { name, palettes }, index, array ) => { + if ( index > 0 ) { + paletteProps.unsetable = false; + } + if ( index === array.length - 1 ) { + paletteProps.clearable = true; + } + return ( + + { name } + + + ); + } ) } + + ); +} + /** * ```jsx * import { DuotonePicker, DuotoneSwatch } from '@wordpress/components'; @@ -191,10 +258,8 @@ function DuotonePicker( { const paletteCommonProps = { defaultDark, defaultLight, - clearable, unsetable, colorPalette, - duotonePalette, disableCustomColors, disableCustomDuotone, value, @@ -205,13 +270,16 @@ function DuotonePicker( { return ( <> { duotonePaletteByOrigin ? ( - + <> + + ) : ( ) } diff --git a/packages/components/src/duotone-picker/types.ts b/packages/components/src/duotone-picker/types.ts index 57d2878b124ce8..18bb5607a9bcf4 100644 --- a/packages/components/src/duotone-picker/types.ts +++ b/packages/components/src/duotone-picker/types.ts @@ -51,11 +51,10 @@ type Color = { slug: string; }; -type duotonePaletteByOrigin = { - theme: DuotoneColor[]; - user: DuotoneColor[]; - default: DuotoneColor[]; -}; +type duotonePaletteByOrigin = Array< { + name: string; + palettes: DuotoneColor[]; +} >; type DuotoneColor = { colors: string[]; @@ -70,8 +69,35 @@ export type DuotoneSwatchProps = { values?: string[] | null; }; -export type SinglePaletteProps = DuotonePickerProps & { +export type SinglePaletteProps = { + defaultDark: string; + defaultLight: string; + unsetable?: boolean; + colorPalette: Color[]; + disableCustomColors?: boolean; + disableCustomDuotone?: boolean; + value?: string[] | 'unset'; + unsetOption: React.ReactElement; + onChange: ( value: DuotonePickerProps[ 'value' ] | undefined ) => void; + clearable?: boolean; + duotonePalette: DuotoneColor[]; +}; + +export type MultiplePalettesProps = { defaultDark: string; defaultLight: string; - unsetOption: any; + unsetable?: boolean; + colorPalette: Color[]; + disableCustomColors?: boolean; + disableCustomDuotone?: boolean; + value?: string[] | 'unset'; + unsetOption: React.ReactElement; + onChange: ( value: DuotonePickerProps[ 'value' ] | undefined ) => void; + duotonePaletteByOrigin: duotonePaletteByOrigin; +}; + +export type paletteOptionsProps = { + palette: DuotoneColor[]; + value?: string[] | 'unset'; + onChange: ( value: DuotonePickerProps[ 'value' ] | undefined ) => void; };