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

Show "group" in multi-select toolbar #37619

Closed
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
39 changes: 37 additions & 2 deletions packages/block-editor/src/components/block-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ import classnames from 'classnames';
import { useSelect, useDispatch } from '@wordpress/data';
import { useRef } from '@wordpress/element';
import { useViewportMatch } from '@wordpress/compose';
import { getBlockType, hasBlockSupport } from '@wordpress/blocks';
import { ToolbarGroup } from '@wordpress/components';
import {
getBlockType,
hasBlockSupport,
switchToBlockType,
} from '@wordpress/blocks';
import { ToolbarGroup, Tooltip, Button } from '@wordpress/components';
import { group } from '@wordpress/icons';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
Expand All @@ -22,6 +28,7 @@ import BlockControls from '../block-controls';
import BlockSettingsMenu from '../block-settings-menu';
import { useShowMoversGestures } from './utils';
import { store as blockEditorStore } from '../../store';
import { useConvertToGroupButtonProps } from '../convert-to-group-buttons';

export default function BlockToolbar( { hideDragHandle } ) {
const {
Expand Down Expand Up @@ -64,6 +71,26 @@ export default function BlockToolbar( { hideDragHandle } ) {
};
}, [] );

// Handle grouping of blocks
const convertToGroupButtonProps = useConvertToGroupButtonProps();
const {
blocksSelection,
groupingBlockName,
clientIds,
} = convertToGroupButtonProps;
const { replaceBlocks } = useDispatch( blockEditorStore );

// Convert to group
const onConvertToGroup = () => {
const newBlocks = switchToBlockType(
blocksSelection,
groupingBlockName
);
if ( newBlocks ) {
replaceBlocks( clientIds, newBlocks );
}
};

// Handles highlighting the current block outline on hover or focus of the
// block type toolbar area.
const { toggleBlockHighlight } = useDispatch( blockEditorStore );
Expand Down Expand Up @@ -114,6 +141,14 @@ export default function BlockToolbar( { hideDragHandle } ) {
{ ( shouldShowVisualToolbar || isMultiToolbar ) && (
<ToolbarGroup className="block-editor-block-toolbar__block-controls">
<BlockSwitcher clientIds={ blockClientIds } />
{ isMultiToolbar && (
Copy link
Contributor

Choose a reason for hiding this comment

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

It could be worth moving this slightly further down into its own ToolbarGroup to the right (visually) of the BlockMover, so that it's in its own section, in case we'd like to add other similar controls in the future?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, this should be present in the actual block tools, not in the block type meta functions (transforms, movers, etc).

<Tooltip text={ __( 'Group' ) }>
<Button
icon={ group }
onClick={ onConvertToGroup }
/>
</Tooltip>
) }
<BlockMover
clientIds={ blockClientIds }
hideDragHandle={ hideDragHandle || hasReducedUI }
Expand Down