Toolbar 是 WordPress 组件库中的一个组件,用于分组相关选项,特别是图标按钮,以增强用户界面的组织性和交互性。它遵循设计指南,支持多种状态和开发配置。
import { Toolbar, ToolbarButton } from '@wordpress/components';
import { formatBold, formatItalic, link } from '@wordpress/icons';
function MyToolbar() {
return (
<Toolbar label="Options">
<ToolbarButton icon={ formatBold } label="Bold" />
<ToolbarButton icon={ formatItalic } label="Italic" />
<ToolbarButton icon={ link } label="Link" />
</Toolbar>
);
}Toolbar can be used to group related options. To emphasize groups of related icon buttons, a toolbar should share a common container.

Toolbars should:

A toolbar’s state makes it clear which icon button is active. Hover and focus states express the available selection options for icon buttons in a toolbar.

Toolbars that cannot be selected can either be given a disabled state, or be hidden.
import { Toolbar, ToolbarButton } from '@wordpress/components';
import { formatBold, formatItalic, link } from '@wordpress/icons';
function MyToolbar() {
return (
<Toolbar label="Options">
<ToolbarButton icon={ formatBold } label="Bold" />
<ToolbarButton icon={ formatItalic } label="Italic" />
<ToolbarButton icon={ link } label="Link" />
</Toolbar>
);
}
Toolbar will pass all HTML props to the underlying element. Additionally, you can pass the custom props specified below.
className: stringClass to set on the container div.
label: stringAn accessible label for the toolbar.
variant: 'unstyled' | undefinedSpecifies the toolbar’s style.
Leave undefined for the default style. Or 'unstyled' which removes the border from the toolbar, but keeps the default popover style.
undefinedorientation: 'horizontal' | 'vertical'Specifies the toolbar’s orientation.
Leave undefined or ‘horizontal’ for horizontal orientation keyboard interactions, choose ‘vertical’ for the alternative.
horizontal