Skip to content
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
37 changes: 34 additions & 3 deletions packages/block-library/src/buttons/edit.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import clsx from 'clsx';
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';
import {
BlockControls,
Inserter,
useBlockProps,
useInnerBlocksProps,
} from '@wordpress/block-editor';
import { ToolbarButton, ToolbarGroup } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

const DEFAULT_BLOCK = {
name: 'core/button',
};

function ButtonsEdit( { attributes, className } ) {
function ButtonsEdit( { attributes, className, clientId } ) {
const { fontSize, layout, style } = attributes;
const blockProps = useBlockProps( {
className: clsx( className, {
Expand All @@ -16,9 +23,33 @@ function ButtonsEdit( { attributes, className } ) {
const innerBlocksProps = useInnerBlocksProps( blockProps, {
defaultBlock: DEFAULT_BLOCK,
orientation: layout?.orientation ?? 'horizontal',

// No on-canvas appender: adding a button goes through the toolbar.
renderAppender: false,
} );

return <div { ...innerBlocksProps } />;
return (
<>
<BlockControls>
<ToolbarGroup>
{ /* `core/button` is the only allowed child, so Inserter
renders a one-click button and names it from the block
title rather than a hardcoded string. */ }
<Inserter
rootClientId={ clientId }
isAppender
toggleProps={ {
as: ToolbarButton,
name: 'add-button',
icon: undefined,
children: __( 'Add button' ),
} }
/>
</ToolbarGroup>
</BlockControls>
<div { ...innerBlocksProps } />
</>
);
}

export default ButtonsEdit;
9 changes: 5 additions & 4 deletions test/e2e/specs/editor/blocks/buttons.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ test.describe( 'Buttons', () => {
);
} );

test( 'copies attributes when inserting a sibling via the appender', async ( {
test( 'copies attributes when inserting a sibling via the toolbar', async ( {
editor,
page,
} ) => {
Expand Down Expand Up @@ -479,13 +479,14 @@ test.describe( 'Buttons', () => {
.click();
await page.getByRole( 'option', { name: 'Vivid red' } ).click();

// Select the parent Buttons block so the appender is visible.
// Select the parent Buttons block so its toolbar is shown.
await editor.selectBlocks(
editor.canvas.getByRole( 'document', { name: 'Block: Buttons' } )
);

// Insert a sibling via the appender; should auto-insert a button.
await editor.canvas
// Insert a sibling from the toolbar; should auto-insert a button.
await page
.getByRole( 'toolbar', { name: 'Block tools' } )
.getByRole( 'button', { name: 'Add button' } )
.click();

Expand Down
Loading