工具栏
功能
- 完整的键盘导航。
安装
从命令行安装组件。
$ npm add reka-ui结构
导入组件。
<script setup lang="ts">
import {
ToolbarButton,
ToolbarLink,
ToolbarRoot,
ToolbarSeparator,
ToolbarToggleGroup,
ToolbarToggleItem,
} from 'reka-ui'
</script>
<template>
<ToolbarRoot>
<ToolbarButton />
<ToolbarSeparator />
<ToolbarLink />
<ToolbarToggleGroup>
<ToolbarToggleItem />
</ToolbarToggleGroup>
</ToolbarRoot>
</template>API 参考
Root
包含所有工具栏组件的部分。
| Prop | Default | Type |
|---|---|---|
as | 'div' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
dir | 'ltr' | 'rtl'The reading direction of the combobox when applicable. | |
loop | booleanWhen | |
orientation | 'horizontal' | 'vertical' | 'horizontal'The orientation of the toolbar |
| Data Attribute | Value |
|---|---|
[data-orientation] | "vertical" | "horizontal" |
Button
一个按钮项。
| Prop | Default | Type |
|---|---|---|
as | 'button' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
disabled | boolean |
| Data Attribute | Value |
|---|---|
[data-orientation] | "vertical" | "horizontal" |
Link
一个链接项。
| Prop | Default | Type |
|---|---|---|
as | 'a' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
ToggleGroup
一组可以切换开或关的双状态按钮。
| Prop | Default | Type |
|---|---|---|
as | 'div' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
defaultValue | AcceptableValue | AcceptableValue[]The default active value of the item(s). Use when you do not need to control the state of the item(s). | |
dir | 'ltr' | 'rtl'The reading direction of the combobox when applicable. | |
disabled | booleanWhen | |
loop | booleanWhen | |
modelValue | AcceptableValue | AcceptableValue[]The controlled value of the active item(s). Use this when you need to control the state of the items. Can be binded with | |
name | stringThe name of the field. Submitted with its owning form as part of a name/value pair. | |
orientation | 'vertical' | 'horizontal'The orientation of the component, which determines how focus moves: | |
required | booleanWhen | |
rovingFocus | booleanWhen | |
type | 'single' | 'multiple'Determines whether a "single" or "multiple" items can be selected at a time. This prop will overwrite the inferred type from |
| Emit | Payload |
|---|---|
update:modelValue | [payload: AcceptableValue | AcceptableValue[]]Event handler called when the value of the toggle changes. |
| Data Attribute | Value |
|---|---|
[data-orientation] | "vertical" | "horizontal" |
ToggleItem
分组中的一个项。
| Prop | Default | Type |
|---|---|---|
as | 'div' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
disabled | booleanWhen | |
value* | AcceptableValueA string value for the toggle group item. All items within a toggle group should use a unique value. |
| Data Attribute | Value |
|---|---|
[data-state] | "on" | "off" |
[data-disabled] | 禁用时出现 |
[data-orientation] | "vertical" | "horizontal" |
Separator
用于在工具栏中视觉上分隔项。
| Prop | Default | Type |
|---|---|---|
as | 'div' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
| Data Attribute | Value |
|---|---|
[data-orientation] | "vertical" | "horizontal" |
示例
与其他基元一起使用
所有公开 Trigger 部分的基元,例如 Dialog、AlertDialog、Popover、DropdownMenu,都可以通过使用 asChild 属性 在工具栏内组合。
这是一个使用我们的 DropdownMenu 基元的示例。
<script setup lang="ts">
import {
DropdownMenuContent,
DropdownMenuRoot,
DropdownMenuTrigger,
ToolbarButton,
ToolbarLink,
ToolbarRoot,
ToolbarSeparator,
ToolbarToggleGroup,
ToolbarToggleItem,
} from 'reka-ui'
</script>
<template>
<ToolbarRoot>
<ToolbarButton>操作 1</ToolbarButton>
<ToolbarSeparator />
<DropdownMenuRoot>
<ToolbarButton as-child>
<DropdownMenuTrigger>触发器</DropdownMenuTrigger>
</ToolbarButton>
<DropdownMenuContent>…</DropdownMenuContent>
</DropdownMenuRoot>
</ToolbarRoot>
</template>无障碍访问
使用 roving tabindex 来管理项之间的焦点移动。
键盘交互
| Key | Description |
|---|---|
Tab | 将焦点移动到分组中的第一个项。 |
Space | 激活/停用该项。 |
Enter | 激活/停用该项。 |
ArrowDown | 根据 orientation 将焦点移动到下一个项。 |
ArrowRight | 根据 orientation 将焦点移动到下一个项。 |
ArrowUp | 根据 orientation 将焦点移动到上一个项。 |
ArrowLeft | 根据 orientation 将焦点移动到上一个项。 |
Home | 将焦点移动到第一个项。 |
End | 将焦点移动到最后一个项。 |
