From 1ddeda84101f277f3671762bd29c10ee3ced71a6 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Fri, 30 Aug 2024 11:11:36 +0200 Subject: [PATCH] DropdownMenuV2: add GroupLabel subcomponent (#64854) * DropdownMenuV2: add GroupLabel subcomponent * Use in Storybook examples * Use Text component * Use the first-party group label in the block bindings dropdown menu * Apply design feedback * Remove unneeded block-editor-bindings__popover classname * Add dedicated DropdownMenuGroupLabelProps type * Fix README * CHANGELOG --- Co-authored-by: ciampo Co-authored-by: tyxla Co-authored-by: jameskoster Co-authored-by: cbravobernal --- .../block-editor/src/hooks/block-bindings.js | 10 +---- .../src/hooks/block-bindings.scss | 8 ---- packages/components/CHANGELOG.md | 1 + .../components/src/dropdown-menu-v2/README.md | 18 +++++++- .../src/dropdown-menu-v2/group-label.tsx | 37 +++++++++++++++ .../components/src/dropdown-menu-v2/index.tsx | 4 ++ .../dropdown-menu-v2/stories/index.story.tsx | 45 ++++++++++++++----- .../components/src/dropdown-menu-v2/styles.ts | 9 ++++ .../components/src/dropdown-menu-v2/types.ts | 7 +++ 9 files changed, 109 insertions(+), 30 deletions(-) create mode 100644 packages/components/src/dropdown-menu-v2/group-label.tsx diff --git a/packages/block-editor/src/hooks/block-bindings.js b/packages/block-editor/src/hooks/block-bindings.js index 961a34f6f858fc..3b90ecc1a0c178 100644 --- a/packages/block-editor/src/hooks/block-bindings.js +++ b/packages/block-editor/src/hooks/block-bindings.js @@ -55,14 +55,9 @@ function BlockBindingsPanelDropdown( { fieldsList, attribute, binding } ) { { Object.keys( fieldsList ).length > 1 && ( - + { registeredSources[ name ].label } - + ) } { Object.entries( fields ).map( ( [ key, value ] ) => ( +>( function DropdownMenuGroup( props, ref ) { + const dropdownMenuContext = useContext( DropdownMenuContext ); + return ( + + } + { ...props } + store={ dropdownMenuContext?.store } + /> + ); +} ); diff --git a/packages/components/src/dropdown-menu-v2/index.tsx b/packages/components/src/dropdown-menu-v2/index.tsx index 96d335f7b38bb9..34805d2d3e2687 100644 --- a/packages/components/src/dropdown-menu-v2/index.tsx +++ b/packages/components/src/dropdown-menu-v2/index.tsx @@ -32,6 +32,7 @@ import { DropdownMenuItem } from './item'; import { DropdownMenuCheckboxItem } from './checkbox-item'; import { DropdownMenuRadioItem } from './radio-item'; import { DropdownMenuGroup } from './group'; +import { DropdownMenuGroupLabel } from './group-label'; import { DropdownMenuSeparator } from './separator'; import { DropdownMenuItemLabel } from './item-label'; import { DropdownMenuItemHelpText } from './item-help-text'; @@ -215,6 +216,9 @@ export const DropdownMenuV2 = Object.assign( Group: Object.assign( DropdownMenuGroup, { displayName: 'DropdownMenuV2.Group', } ), + GroupLabel: Object.assign( DropdownMenuGroupLabel, { + displayName: 'DropdownMenuV2.GroupLabel', + } ), Separator: Object.assign( DropdownMenuSeparator, { displayName: 'DropdownMenuV2.Separator', } ), diff --git a/packages/components/src/dropdown-menu-v2/stories/index.story.tsx b/packages/components/src/dropdown-menu-v2/stories/index.story.tsx index fd14bb68b1d6ad..90d15ca2ea6c18 100644 --- a/packages/components/src/dropdown-menu-v2/stories/index.story.tsx +++ b/packages/components/src/dropdown-menu-v2/stories/index.story.tsx @@ -32,6 +32,8 @@ const meta: Meta< typeof DropdownMenuV2 > = { // @ts-expect-error - See https://github.com/storybookjs/storybook/issues/23170 Group: DropdownMenuV2.Group, // @ts-expect-error - See https://github.com/storybookjs/storybook/issues/23170 + GroupLabel: DropdownMenuV2.GroupLabel, + // @ts-expect-error - See https://github.com/storybookjs/storybook/issues/23170 Separator: DropdownMenuV2.Separator, // @ts-expect-error - See https://github.com/storybookjs/storybook/issues/23170 Context: DropdownMenuV2.Context, @@ -83,6 +85,7 @@ export const Default: StoryFn< typeof DropdownMenuV2 > = ( props ) => ( Disabled item + Group label } > @@ -182,6 +185,9 @@ export const WithCheckboxes: StoryFn< typeof DropdownMenuV2 > = ( props ) => { return ( + + Single selection, uncontrolled + = ( props ) => { Checkbox item A - Uncontrolled + Initially unchecked = ( props ) => { Checkbox item B - Uncontrolled, initially checked + Initially checked + + Single selection, controlled + = ( props ) => { Checkbox item A - Controlled + Initially unchecked = ( props ) => { Checkbox item B - Controlled, initially checked + Initially checked + + Multiple selection, uncontrolled + = ( props ) => { Checkbox item A - Uncontrolled, multiple selection + Initially unchecked = ( props ) => { Checkbox item B - Uncontrolled, multiple selection, initially checked + Initially checked + + Multiple selection, controlled + = ( props ) => { Checkbox item A - Controlled, multiple selection + Initially unchecked = ( props ) => { Checkbox item B - Controlled, multiple selection, initially checked + Initially checked @@ -307,12 +322,15 @@ export const WithRadios: StoryFn< typeof DropdownMenuV2 > = ( props ) => { return ( + + Uncontrolled + Radio item 1 - Uncontrolled + Initially unchecked = ( props ) => { Radio item 2 - Uncontrolled, initially checked + Initially checked + + Controlled + = ( props ) => { Radio item 1 - Controlled + Initially unchecked = ( props ) => { Radio item 2 - Controlled, initially checked + Initially checked diff --git a/packages/components/src/dropdown-menu-v2/styles.ts b/packages/components/src/dropdown-menu-v2/styles.ts index a39ef83326c3aa..9bb2f07b282622 100644 --- a/packages/components/src/dropdown-menu-v2/styles.ts +++ b/packages/components/src/dropdown-menu-v2/styles.ts @@ -311,6 +311,15 @@ export const DropdownMenuGroup = styled( Ariakit.MenuGroup )` display: contents; `; +export const DropdownMenuGroupLabel = styled( Ariakit.MenuGroupLabel )` + /* Occupy the width of all grid columns (ie. full width) */ + grid-column: 1 / -1; + + padding-block-start: ${ space( 3 ) }; + padding-block-end: ${ space( 2 ) }; + padding-inline: ${ ITEM_PADDING_INLINE }; +`; + export const DropdownMenuSeparator = styled( Ariakit.MenuSeparator )< Pick< DropdownMenuContext, 'variant' > >` diff --git a/packages/components/src/dropdown-menu-v2/types.ts b/packages/components/src/dropdown-menu-v2/types.ts index f49f026b787933..795cd9ac76ff58 100644 --- a/packages/components/src/dropdown-menu-v2/types.ts +++ b/packages/components/src/dropdown-menu-v2/types.ts @@ -87,6 +87,13 @@ export interface DropdownMenuGroupProps { children: React.ReactNode; } +export interface DropdownMenuGroupLabelProps { + /** + * The contents of the dropdown menu group. + */ + children: React.ReactNode; +} + export interface DropdownMenuItemProps { /** * The contents of the menu item.