Toolbar
包含命令按钮、变体尺寸、交叉轴分隔线与键盘漫游焦点的 Shadcn 工具栏组件。
Toolbar 将一组相关操作按钮和分隔线组织在一个无障碍工具栏容器中,通过单一 Tab 入口与方向键提供流畅的键盘操作。本页示例使用 Shadcn Rhea 风格。
<wc-shadcn-toolbar-root
style-profile="rhea"
a11y-label="Document actions"
>
<wc-shadcn-toolbar-button style-profile="rhea" value="undo">
Undo
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-button style-profile="rhea" value="redo">
Redo
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-separator
style-profile="rhea"
></wc-shadcn-toolbar-separator>
<wc-shadcn-toolbar-button style-profile="rhea" value="cut">
Cut
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-button style-profile="rhea" value="copy">
Copy
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-button
style-profile="rhea"
value="paste"
disabled
>
Paste
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-separator
style-profile="rhea"
></wc-shadcn-toolbar-separator>
<wc-shadcn-toolbar-button
style-profile="rhea"
value="save"
variant="default"
>
Save
</wc-shadcn-toolbar-button>
</wc-shadcn-toolbar-root>将于 0.3.0-rc.1 及后续版本中提供。可直接从 @proto.ui/prototypes-shadcn/toolbar 导入原型并适配到 Web Components、React、Vue 3 或 Vue 2:
import { toolbarRoot, toolbarButton, toolbarSeparator } from '@proto.ui/prototypes-shadcn/toolbar';import { ShadcnToolbarRoot, ShadcnToolbarButton, ShadcnToolbarSeparator,} from '../proto-ui/components/react';
export function EditorToolbar() { return ( <ShadcnToolbarRoot styleProfile="rhea" a11yLabel="Document actions"> <ShadcnToolbarButton value="undo">Undo</ShadcnToolbarButton> <ShadcnToolbarButton value="redo">Redo</ShadcnToolbarButton> <ShadcnToolbarSeparator /> <ShadcnToolbarButton value="save" variant="default"> Save </ShadcnToolbarButton> </ShadcnToolbarRoot> );}Toolbar 由 Root、Button 和 Separator 三个部件组成:
- 焦点与键盘导航:同一时刻仅有一个启用的
Button参与页面Tab序列;水平布局下使用左右方向键、垂直布局下使用上下方向键在可用命令间移动,loop={true}(默认)时会在首尾循环。 - 命令触发:激活启用状态的
Button时会发出action事件,载荷为{ value: string }。 - 分隔线:
Separator会自动根据Root的orientation切换到交叉轴方向绘制。
风格预设(styleProfile)
Section titled “风格预设(styleProfile)”Root、Button 与 Separator 均支持全部 9 种 Shadcn 风格预设(vega、nova、maia、lyra、mira、luma、sera、rhea 与 new-york-v4,new-york 作为兼容别名)。省略或设为 inherit 时优先继承最近的 StyleScope 或 Root,否则回退到 rhea。
Button 支持 ghost(默认)、outline、secondary、default 与 destructive 五种视觉变体,可在工具栏中区分普通操作、主要提交与危险操作。
<wc-shadcn-toolbar-root
style-profile="rhea"
a11y-label="Button variants"
>
<wc-shadcn-toolbar-button
style-profile="rhea"
value="ghost"
variant="ghost"
>
Ghost
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-button
style-profile="rhea"
value="outline"
variant="outline"
>
Outline
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-button
style-profile="rhea"
value="secondary"
variant="secondary"
>
Secondary
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-separator
style-profile="rhea"
></wc-shadcn-toolbar-separator>
<wc-shadcn-toolbar-button
style-profile="rhea"
value="default"
variant="default"
>
Publish
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-button
style-profile="rhea"
value="delete"
variant="destructive"
>
Delete
</wc-shadcn-toolbar-button>
</wc-shadcn-toolbar-root>尺寸与纯图标按钮
Section titled “尺寸与纯图标按钮”支持 sm、default、lg 与正方形 icon 尺寸。纯图标按钮应像下方示例一样配合 aria-hidden="true" 图标和 sr-only 可访问名称。
<wc-shadcn-toolbar-root
style-profile="rhea"
a11y-label="Compact formatting toolbar"
>
<wc-shadcn-toolbar-button
style-profile="rhea"
value="bold"
size="icon"
>
<span aria-hidden="true">B</span>
<span class="sr-only">Bold</span>
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-button
style-profile="rhea"
value="italic"
size="icon"
>
<span aria-hidden="true">I</span>
<span class="sr-only">Italic</span>
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-separator
style-profile="rhea"
></wc-shadcn-toolbar-separator>
<wc-shadcn-toolbar-button
style-profile="rhea"
value="comment"
size="sm"
>
Small
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-button
style-profile="rhea"
value="share"
size="default"
variant="outline"
>
Default
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-button
style-profile="rhea"
value="export"
size="lg"
variant="secondary"
>
Large
</wc-shadcn-toolbar-button>
</wc-shadcn-toolbar-root>将 orientation 设为 'vertical' 后,容器自动切换为纵向排列,Separator 自动转为水平分隔线,方向键切换为上下键导航。
<wc-shadcn-toolbar-root
style-profile="rhea"
orientation="vertical"
a11y-label="Canvas tools"
>
<wc-shadcn-toolbar-button style-profile="rhea" value="select">
Select
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-button style-profile="rhea" value="frame">
Frame
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-button style-profile="rhea" value="text">
Text
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-separator
style-profile="rhea"
></wc-shadcn-toolbar-separator>
<wc-shadcn-toolbar-button style-profile="rhea" value="zoom-in">
Zoom In
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-button style-profile="rhea" value="zoom-out">
Zoom Out
</wc-shadcn-toolbar-button>
</wc-shadcn-toolbar-root>禁用与边界导航
Section titled “禁用与边界导航”设置 Root 的 disabled={true} 会统一禁用整条工具栏;将 loop 设为 false 可让方向键在首尾命令处停止,并自动跳过单项禁用的按钮。
<div class="grid justify-items-center gap-4">
<wc-shadcn-toolbar-root
style-profile="rhea"
disabled
a11y-label="Read-only actions"
>
<wc-shadcn-toolbar-button style-profile="rhea" value="undo">
Undo
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-button style-profile="rhea" value="redo">
Redo
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-separator
style-profile="rhea"
></wc-shadcn-toolbar-separator>
<wc-shadcn-toolbar-button
style-profile="rhea"
value="save"
variant="default"
>
Save
</wc-shadcn-toolbar-button>
</wc-shadcn-toolbar-root>
<wc-shadcn-toolbar-root
style-profile="rhea"
loop="false"
a11y-label="Bounded actions"
>
<wc-shadcn-toolbar-button style-profile="rhea" value="back">
Back
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-button
style-profile="rhea"
value="forward"
disabled
>
Forward
</wc-shadcn-toolbar-button>
<wc-shadcn-toolbar-button style-profile="rhea" value="reload">
Reload
</wc-shadcn-toolbar-button>
</wc-shadcn-toolbar-root>
</div>| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
styleProfile | ShadcnStyleChoice | inherit → 最近 scope → rhea | 选择工具栏的 Shadcn 风格预设并向下传递作用域 |
variant | 'default' 'ghost' | 'default' | 带边框背景表面或无边框透明布局容器 |
disabled | boolean | false | 统一禁用所有工具栏命令按钮 |
orientation | 'horizontal' 'vertical' | 'horizontal' | 工具栏排列方向及方向键导航轴向 |
loop | boolean | true | 方向键导航是否在首尾命令间循环 |
a11yLabel | string | '' | 工具栏的无障碍名称(aria-label) |
Button
Section titled “Button”| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
styleProfile | ShadcnStyleChoice | inherit → Root / scope → rhea | 覆盖或继承外层风格预设 |
variant | 'ghost' 'outline' 'secondary' 'default' 'destructive' | 'ghost' | 命令按钮的视觉变体 |
size | 'default' 'sm' 'lg' 'icon' | 'default' | 按钮尺寸与内边距规格 |
value | string | '' | 激活时通过 action 事件回传的命令标识 |
disabled | boolean | false | 禁用当前命令并移出漫游焦点序列 |
Separator
Section titled “Separator”| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
styleProfile | ShadcnStyleChoice | inherit → Root / scope → rhea | 同步紧凑/常规预设下的分隔线间距 |
事件与暴露接口
Section titled “事件与暴露接口”- Button
action事件:启用状态激活时触发,载荷为{ value: string }。 - Root 暴露:
disabled状态以及focusFirst()、focusLast()、setCurrent(id)方法。 - Button 暴露:
disabled、focused、focusVisible状态以及focusSelf(options)方法。