块编辑器开发文档

💡 云策文档标注

概述

ToolbarButton 是用于在工具栏中添加操作的组件,适用于通用界面或自定义块中。它继承 Button 组件的特性,确保工具栏中的按钮样式和键盘交互符合 WAI-ARIA 工具栏模式。

关键要点

  • ToolbarButton 用于在 Toolbar 或 ToolbarGroup 中添加按钮,适用于通用界面;在自定义块中,建议使用 BlockControls。
  • 组件接受 Button 的 API,并额外支持 containerClassName 和 subscript 属性。
  • 可与 Toolbar、ToolbarGroup、Dropdown 等组件结合使用,实现复杂交互。

代码示例

import { Toolbar, ToolbarButton } from '@wordpress/components';
import { pencil } from '@wordpress/icons';

function MyToolbar() {
    return (
        <Toolbar label="Options">
            <ToolbarButton
                icon={ pencil }
                label="Edit"
                onClick={ () => alert( 'Editing' ) }
            />
        </Toolbar>
    );
}
import { BlockControls } from '@wordpress/block-editor';
import { ToolbarGroup, ToolbarButton } from '@wordpress/components';
import { pencil } from '@wordpress/icons';

function Edit() {
    return (
        <BlockControls>
            <ToolbarGroup>
                <ToolbarButton
                    icon={ pencil }
                    label="Edit"
                    onClick={ () => alert( 'Editing' ) }
                />
            </ToolbarGroup>
        </BlockControls>
    );
}

📄 原文内容

ToolbarButton can be used to add actions to a toolbar, usually inside a Toolbar or ToolbarGroup when used to create general interfaces. If you’re using it to add controls to your custom block, you should consider using BlockControls.

It has similar features to the Button component. Using ToolbarButton will ensure the correct styling for a button in a toolbar, and also that keyboard interactions in a toolbar are consistent with the WAI-ARIA toolbar pattern.

Usage

To create general interfaces, you’ll want to render ToolbarButton in a Toolbar component.

import { Toolbar, ToolbarButton } from '@wordpress/components';
import { pencil } from '@wordpress/icons';

function MyToolbar() {
    return (
        <Toolbar label="Options">
            <ToolbarButton
                icon={ pencil }
                label="Edit"
                onClick={ () => alert( 'Editing' ) }
            />
        </Toolbar>
    );
}

Inside BlockControls

If you’re working on a custom block and you want to add controls to the block toolbar, you should use BlockControls instead. Optionally wrapping it with ToolbarGroup.

import { BlockControls } from '@wordpress/block-editor';
import { ToolbarGroup, ToolbarButton } from '@wordpress/components';
import { pencil } from '@wordpress/icons';

function Edit() {
    return (
        <BlockControls>
            <ToolbarGroup>
                <ToolbarButton
                    icon={ pencil }
                    label="Edit"
                    onClick={ () => alert( 'Editing' ) }
                />
            </ToolbarGroup>
        </BlockControls>
    );
}

Props

This component accepts the same API of the Button component in addition to:

containerClassName: string

An optional additional class name to apply to the button container.

  • Required: No

subscript: string

An optional subscript for the button.

  • Required: No

Related components

  • If you wish to implement a control to select options grouped as icon buttons you can use the Toolbar component, which already handles this strategy.
  • The ToolbarButton may be used with other elements such as Dropdown to display options in a popover.