块编辑器开发文档

💡 云策文档标注

概述

ToolbarItem 是一个通用的无头组件,用于将自定义组件或 HTML 元素转换为工具栏项。它通常放置在 Toolbar 或 ToolbarGroup 中,用于构建通用界面;在自定义块中添加控件时,建议使用 BlockControls。

关键要点

  • ToolbarItem 是一个通用组件,可将任何自定义组件或 HTML 元素作为工具栏项使用。
  • 必须放置在 Toolbar 或 ToolbarGroup 内,以确保正确集成到界面中。
  • 在自定义块开发中,优先使用 BlockControls 来添加块工具栏控件,可结合 ToolbarGroup 包装。
  • 提供了 as prop 和 render prop 两种使用方式,支持灵活配置组件或传递属性。
  • 对于简单的工具栏按钮,推荐使用 ToolbarButton 组件替代 ToolbarItem。

代码示例

import { Toolbar, ToolbarItem, Button } from '@wordpress/components';

function MyToolbar() {
    return (
        <Toolbar label="Options">
            <ToolbarItem as={ Button }>I am a toolbar button</ToolbarItem>
            <ToolbarItem as="button">I am another toolbar button</ToolbarItem>
        </Toolbar>
    );
}
import { Toolbar, ToolbarItem, DropdownMenu } from '@wordpress/components';
import { table } from '@wordpress/icons';

function MyToolbar() {
    return (
        <Toolbar label="Options">
            <ToolbarItem>
                { ( toolbarItemHTMLProps ) => (
                    <DropdownMenu
                        icon={ table }
                        toggleProps={ toolbarItemHTMLProps }
                        label={ 'Edit table' }
                        controls={ [] }
                    />
                ) }
            </ToolbarItem>
        </Toolbar>
    );
}
import { BlockControls } from '@wordpress/block-editor';
import { ToolbarGroup, ToolbarItem, Button } from '@wordpress/components';

function Edit() {
    return (
        <BlockControls>
            <ToolbarGroup>
                <ToolbarItem as={ Button }>I am a toolbar button</ToolbarItem>
            </ToolbarGroup>
        </BlockControls>
    );
}

注意事项

  • ToolbarItem 应始终包裹在 Toolbar 或 ToolbarGroup 组件内,以确保功能正常。
  • 在自定义块开发中,避免直接使用 ToolbarItem 添加控件,而应使用 BlockControls 以符合块编辑器规范。
  • 如果仅需简单按钮功能,考虑使用 ToolbarButton 组件,它更简洁且专为按钮场景设计。

📄 原文内容

A ToolbarItem is a generic headless component that can be used to make any custom component a Toolbar item. It should be 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.

Usage

as prop

You can use the as prop with a custom component or any HTML element.

import { Toolbar, ToolbarItem, Button } from '@wordpress/components';

function MyToolbar() {
    return (
        <Toolbar label="Options">
            <ToolbarItem as={ Button }>I am a toolbar button</ToolbarItem>
            <ToolbarItem as="button">I am another toolbar button</ToolbarItem>
        </Toolbar>
    );
}

render prop

You can pass children as function to get the ToolbarItem props and pass them to another component.

import { Toolbar, ToolbarItem, DropdownMenu } from '@wordpress/components';
import { table } from '@wordpress/icons';

function MyToolbar() {
    return (
        <Toolbar label="Options">
            <ToolbarItem>
                { ( toolbarItemHTMLProps ) => (
                    <DropdownMenu
                        icon={ table }
                        toggleProps={ toolbarItemHTMLProps }
                        label={ 'Edit table' }
                        controls={ [] }
                    />
                ) }
            </ToolbarItem>
        </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, ToolbarItem, Button } from '@wordpress/components';

function Edit() {
    return (
        <BlockControls>
            <ToolbarGroup>
                <ToolbarItem as={ Button }>I am a toolbar button</ToolbarItem>
            </ToolbarGroup>
        </BlockControls>
    );
}

Related components