Skip to content
Merged
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
4 changes: 4 additions & 0 deletions packages/block-editor/src/components/block-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import BlockSwitcher from '../block-switcher';
import BlockControls from '../block-controls';
import BlockSettingsMenu from '../block-settings-menu';
import { BlockLockToolbar } from '../block-lock';
import { BlockGroupToolbar } from '../convert-to-group-buttons';
import { useShowMoversGestures } from './utils';
import { store as blockEditorStore } from '../../store';

Expand Down Expand Up @@ -127,6 +128,9 @@ export default function BlockToolbar( { hideDragHandle } ) {
</ToolbarGroup>
) }
</div>
{ shouldShowVisualToolbar && isMultiToolbar && (
<BlockGroupToolbar />
) }
{ shouldShowVisualToolbar && (
<>
<BlockControls.Slot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useDispatch } from '@wordpress/data';
*/
import { store as blockEditorStore } from '../../store';
import useConvertToGroupButtonProps from './use-convert-to-group-button-props';
import BlockGroupToolbar from './toolbar';

function ConvertToGroupButton( {
clientIds,
Expand Down Expand Up @@ -73,4 +74,8 @@ function ConvertToGroupButton( {
);
}

export { useConvertToGroupButtonProps, ConvertToGroupButton };
export {
BlockGroupToolbar,
ConvertToGroupButton,
useConvertToGroupButtonProps,
};
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's add the variations of Group here as well — Row, Stack.

@andrewserong andrewserong Mar 24, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes — I thought I'd look into that in a follow up PR. At the moment we have the same icon, so there's a couple ways we could do it, either adding additional icons in the row, or turn the Group button into a dropdown where you can then select which of the variations you'd like to convert to.

I'll have a play after this PR lands, and ping everyone again 😄

Edit: once Joen's neat new icons are ready, it looks like they'll be perfect for the toolbar 👍

icon={ group }
label={ label }
onClick={ onConvertToGroup }
/>
</ToolbarGroup>
);
}

export default BlockGroupToolbar;