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

Allow block-specific settings controls #22672

Closed
wants to merge 2 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 @@ -7,13 +7,18 @@ 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 BlockSettingsMenuControlsSlot = ( { fillProps, clientIds = null } ) => {
const { Fill, Slot } = createSlotFill( 'BlockSettingsMenuControls' );

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

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

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

export const BlockSettingsMenuControls = ( {
forAllBlocks = false,
...fillProps
} ) => {
const context = useContext( BlockListBlockContext );
if ( ! forAllBlocks ) {
// Let's use non-existent ID in case the context is missing
const clientId = context?.clientId || 'non-such-id';
Comment on lines +62 to +63
Copy link
Contributor Author

@adamziel adamziel May 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any assertion mechanism available? Using an non-existent ID is an okayish workaround but if it's used in a block-specific mode outside of block context I'd really prefer to provide a clear error message in the console (if not blow up entirely).

fillProps.name = getSlotName( clientId );
}

return <Fill { ...fillProps } />;
};

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 @@ -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