Skip to content

Commit

Permalink
Ensure blocks locked against removal cause the Group button not to re…
Browse files Browse the repository at this point in the history
…nder
  • Loading branch information
andrewserong committed Mar 24, 2022
1 parent 8cb4b16 commit d29a703
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useDispatch } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';
import { switchToBlockType } from '@wordpress/blocks';
import { ToolbarButton, ToolbarGroup } from '@wordpress/components';
import { group } from '@wordpress/icons';
Expand All @@ -22,6 +22,16 @@ function BlockGroupToolbar( { label = __( 'Group' ) } ) {
} = useConvertToGroupButtonProps();
const { replaceBlocks } = useDispatch( blockEditorStore );

const { canRemove } = useSelect(
( select ) => {
const { canRemoveBlocks } = select( blockEditorStore );
return {
canRemove: canRemoveBlocks( clientIds ),
};
},
[ clientIds ]
);

const onConvertToGroup = () => {
const newBlocks = switchToBlockType(
blocksSelection,
Expand All @@ -35,7 +45,8 @@ function BlockGroupToolbar( { label = __( 'Group' ) } ) {
// Don't render the button if the current selection cannot be grouped.
// A good example is selecting multiple button blocks within a Buttons block:
// The group block is not a valid child of Buttons, so we should not show the button.
if ( ! isGroupable ) {
// Any blocks that are locked against removal also cannot be grouped.
if ( ! isGroupable || ! canRemove ) {
return null;
}

Expand Down

0 comments on commit d29a703

Please sign in to comment.