-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BlockToolbar: Show Group button in toolbar when multiple blocks are s…
…elected (#39710) * BlockToolbar: Show Group button in toolbar when multiple blocks are selected Co-authored-by: Jitesh Dhamaniya <[email protected]> * Ensure blocks locked against removal cause the Group button not to render * Add translator context for Group as a verb Co-authored-by: Jitesh Dhamaniya <[email protected]>
- Loading branch information
1 parent
6b6b798
commit dba91fb
Showing
3 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/block-editor/src/components/convert-to-group-buttons/toolbar.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { switchToBlockType } from '@wordpress/blocks'; | ||
import { ToolbarButton, ToolbarGroup } from '@wordpress/components'; | ||
import { group } from '@wordpress/icons'; | ||
import { _x } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useConvertToGroupButtonProps } from '../convert-to-group-buttons'; | ||
import { store as blockEditorStore } from '../../store'; | ||
|
||
function BlockGroupToolbar( { label = _x( 'Group', 'verb' ) } ) { | ||
const { | ||
blocksSelection, | ||
clientIds, | ||
groupingBlockName, | ||
isGroupable, | ||
} = useConvertToGroupButtonProps(); | ||
const { replaceBlocks } = useDispatch( blockEditorStore ); | ||
|
||
const { canRemove } = useSelect( | ||
( select ) => { | ||
const { canRemoveBlocks } = select( blockEditorStore ); | ||
return { | ||
canRemove: canRemoveBlocks( clientIds ), | ||
}; | ||
}, | ||
[ clientIds ] | ||
); | ||
|
||
const onConvertToGroup = () => { | ||
const newBlocks = switchToBlockType( | ||
blocksSelection, | ||
groupingBlockName | ||
); | ||
if ( newBlocks ) { | ||
replaceBlocks( clientIds, newBlocks ); | ||
} | ||
}; | ||
|
||
// 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. | ||
// Any blocks that are locked against removal also cannot be grouped. | ||
if ( ! isGroupable || ! canRemove ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<ToolbarGroup> | ||
<ToolbarButton | ||
icon={ group } | ||
label={ label } | ||
onClick={ onConvertToGroup } | ||
/> | ||
</ToolbarGroup> | ||
); | ||
} | ||
|
||
export default BlockGroupToolbar; |