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

Add support for context-specific BlockSettingsMenuControls #22674

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export default function BlockNavigationBlock( {
<BlockSettingsDropdown
clientIds={ [ clientId ] }
icon={ moreVertical }
contexts={ [ 'default', 'navigator' ] }
{ ...props }
/>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ import { isEmpty, map } from 'lodash';
* WordPress dependencies
*/
import { createSlotFill, MenuGroup } from '@wordpress/components';
import { useContext } from '@wordpress/element';
import { useSelect } from '@wordpress/data';

const { Fill: BlockSettingsMenuControls, Slot } = createSlotFill(
'BlockSettingsMenuControls'
);
/**
* Internal dependencies
*/
import { BlockListBlockContext } from '../block-list/block';

const { Fill, Slot } = createSlotFill( 'BlockSettingsMenuControls' );

const BlockSettingsMenuControlsSlot = ( { fillProps, clientIds = null } ) => {
const getSlotName = ( context, clientId = null ) =>
`BlockSettings-${ context }-${ clientId ? clientId : '' }`;
const BlockSettingsMenuControlsSlots = ( {
fillProps,
context = 'default',
clientIds = null,
} ) => {
const { selectedBlocks } = useSelect( ( select ) => {
const { getBlocksByClientId, getSelectedBlockClientIds } = select(
'core/block-editor'
Expand All @@ -28,16 +38,44 @@ const BlockSettingsMenuControlsSlot = ( { fillProps, clientIds = null } ) => {
};
}, [] );

const slotProps = { ...fillProps, selectedBlocks };
return (
<Slot fillProps={ { ...fillProps, selectedBlocks } }>
{ ( fills ) =>
! isEmpty( fills ) && <MenuGroup>{ fills }</MenuGroup>
}
</Slot>
<>
<BlockSettingsMenuControlsSlot
{ ...slotProps }
name={ getSlotName( context ) }
/>
{ clientIds.length === 1 && (
<BlockSettingsMenuControlsSlot
{ ...slotProps }
name={ getSlotName( context, clientIds[ 0 ] ) }
/>
) }
</>
);
};

BlockSettingsMenuControls.Slot = BlockSettingsMenuControlsSlot;
const BlockSettingsMenuControlsSlot = ( props ) => (
<Slot { ...props }>
{ ( fills ) => ! isEmpty( fills ) && <MenuGroup>{ fills }</MenuGroup> }
</Slot>
);

export const BlockSettingsMenuControls = ( {
forAllBlocks = false,
context = 'default',
...fillProps
} ) => {
const blockContext = useContext( BlockListBlockContext );
let clientId;
if ( ! forAllBlocks ) {
// Let's use non-existent ID in case the block context is missing
clientId = blockContext?.clientId || 'non-such-id';
}
return <Fill { ...fillProps } name={ getSlotName( context, clientId ) } />;
};

BlockSettingsMenuControls.Slot = BlockSettingsMenuControlsSlots;

/**
* @see https://github.com/WordPress/gutenberg/blob/master/packages/block-editor/src/components/block-settings-menu-controls/README.md
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ const POPOVER_PROPS = {
isAlternate: true,
};

export function BlockSettingsDropdown( { clientIds, ...props } ) {
export function BlockSettingsDropdown( {
clientIds,
contexts = [ 'default' ],
...props
} ) {
const blockClientIds = castArray( clientIds );
const count = blockClientIds.length;
const firstBlockClientId = blockClientIds[ 0 ];
Expand Down Expand Up @@ -145,10 +149,14 @@ export function BlockSettingsDropdown( { clientIds, ...props } ) {
/>
) }
</MenuGroup>
<BlockSettingsMenuControls.Slot
fillProps={ { onClose } }
clientIds={ clientIds }
/>
{ contexts.map( ( context ) => (
<BlockSettingsMenuControls.Slot
key={ context }
fillProps={ { onClose } }
clientIds={ clientIds }
context={ context }
/>
) ) }
<MenuGroup>
{ ! isLocked && (
<MenuItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const PluginBlockSettingsMenuItem = ( {
small,
role,
} ) => (
<BlockSettingsMenuControls>
<BlockSettingsMenuControls forAllBlocks>
{ ( { selectedBlocks, onClose } ) => {
if ( ! shouldRenderItem( selectedBlocks, allowedBlocks ) ) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function ConvertToGroupButton( {
}

return (
<BlockSettingsMenuControls>
<BlockSettingsMenuControls forAllBlocks>
{ ( { onClose } ) => (
<>
{ isGroupable && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function ReusableBlockConvertButton( {
}

return (
<BlockSettingsMenuControls>
<BlockSettingsMenuControls forAllBlocks>
{ ( { onClose } ) => (
<>
{ ! isReusable && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function ReusableBlockDeleteButton( {
}

return (
<BlockSettingsMenuControls>
<BlockSettingsMenuControls forAllBlocks>
{ ( { onClose } ) => (
<MenuItem
disabled={ isDisabled }
Expand Down