Toolbar buttons and sidebar settings for blocks: BlockControls and InspectorControls
A custom block puts its settings in two places: quick, frequent actions in the block toolbar above the block, everything else in the settings sidebar. The @wordpress/block-editor package provides slots for both, BlockControls and InspectorControls, and the same slots can be used to add a button or a panel to core blocks with a filter, without forking them.
Problem: A plugin’s “Notice” block has its style choice (info, warning, success) hidden in the sidebar, so editors never find it. The team also wants a “copy anchor link” button on every core Heading block.
Solution: In the block’s edit function, render a ToolbarDropdownMenu inside BlockControls for the notice type and a PanelBody inside InspectorControls for the rarer settings. For core Heading blocks, wrap their edit component with the editor.BlockEdit filter and createHigherOrderComponent, and add a ToolbarButton there.
The block’s own controls
// src/notice/edit.js
import { __ } from '@wordpress/i18n';
import { BlockControls, InspectorControls, RichText, useBlockProps } from '@wordpress/block-editor';
import { ToolbarDropdownMenu, PanelBody, ToggleControl } from '@wordpress/components';
const TYPES = [
{ value: 'info', title: __( 'Info', 'textdomain' ), icon: 'info' },
{ value: 'warning', title: __( 'Warning', 'textdomain' ), icon: 'warning' },
{ value: 'success', title: __( 'Success', 'textdomain' ), icon: 'yes' },
];
export default function Edit( { attributes, setAttributes } ) {
const { type, text, dismissible } = attributes;
const current = TYPES.find( ( t ) => t.value === type ) ?? TYPES[ 0 ];
return (
<>
<BlockControls group="block">
<ToolbarDropdownMenu
icon={ current.icon }
label={ __( 'Notice type', 'textdomain' ) }
controls={ TYPES.map( ( t ) => ( {
title: t.title,
icon: t.icon,
isActive: t.value === type,
onClick: () => setAttributes( { type: t.value } ),
} ) ) }
/>
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Behavior', 'textdomain' ) }>
<ToggleControl
label={ __( 'Dismissible', 'textdomain' ) }
checked={ dismissible }
onChange={ ( value ) => setAttributes( { dismissible: value } ) }
__nextHasNoMarginBottom
/>
</PanelBody>
</InspectorControls>
<RichText
{ ...useBlockProps( { className: `is-type-${ type }` } ) }
tagName="p"
value={ text }
onChange={ ( value ) => setAttributes( { text: value } ) }
/>
</>
);
}
BlockControls renders into the toolbar of the selected block; group="block" places the control in the block-specific group next to alignment. InspectorControls renders into the sidebar’s Settings tab. Both only show while the block is selected, and they need no registration: the components portal their content into the right place.
A button on core Heading blocks
// src/heading-anchor.js
import { __ } from '@wordpress/i18n';
import { addFilter } from '@wordpress/hooks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { BlockControls } from '@wordpress/block-editor';
import { ToolbarButton } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
const withAnchorCopy = createHigherOrderComponent( ( BlockEdit ) => ( props ) => {
const permalink = useSelect( ( select ) => select( 'core/editor' )?.getPermalink(), [] );
if ( props.name !== 'core/heading' || ! props.attributes.anchor || ! permalink ) {
return <BlockEdit { ...props } />;
}
return (
<>
<BlockEdit { ...props } />
<BlockControls group="other">
<ToolbarButton
icon="admin-links"
label={ __( 'Copy link to this heading', 'textdomain' ) }
onClick={ () => navigator.clipboard.writeText( `${ permalink }#${ props.attributes.anchor }` ) }
/>
</BlockControls>
</>
);
}, 'withAnchorCopy' );
addFilter( 'editor.BlockEdit', 'ha/heading-anchor-copy', withAnchorCopy );
editor.BlockEdit wraps the edit component of every block; the check on props.name limits the button to headings that have an HTML anchor set (Advanced → HTML anchor). Nothing is saved; the button only uses existing attributes. The script is enqueued with enqueue_block_editor_assets and its dependencies from the generated .asset.php file.
Build
Both files contain JSX and are compiled with @wordpress/scripts (wp-scripts build), which also generates the dependency list. Block scaffolding, block.json and server-side rendering: a custom block with dynamic rendering.
NOTE: Prefer the toolbar for the one or two actions editors use on almost every block, and the sidebar for everything else; a crowded toolbar is as unusable as a hidden setting. Settings that should be available for many blocks at once, like spacing or colors, belong in supports in block.json, which gives the standard controls for free: block.json explained.