Components

工具栏

¥Toolbar

用于对一组控件(例如按钮、切换组或下拉菜单)进行分组的容器。

import * as React from "react";
import { Toolbar } from "radix-ui";
import { StrikethroughIcon, TextAlignLeftIcon, TextAlignCenterIcon, TextAlignRightIcon, FontBoldIcon, FontItalicIcon, } from "@radix-ui/react-icons";
import "./styles.css";
const ToolbarDemo = () => (
<Toolbar.Root className="ToolbarRoot" aria-label="Formatting options">
<Toolbar.ToggleGroup type="multiple" aria-label="Text formatting">
<Toolbar.ToggleItem className="ToolbarToggleItem" value="bold" aria-label="Bold" >
<FontBoldIcon />
</Toolbar.ToggleItem>
<Toolbar.ToggleItem className="ToolbarToggleItem" value="italic" aria-label="Italic" >
<FontItalicIcon />
</Toolbar.ToggleItem>
<Toolbar.ToggleItem className="ToolbarToggleItem" value="strikethrough" aria-label="Strike through" >
<StrikethroughIcon />
</Toolbar.ToggleItem>
</Toolbar.ToggleGroup>
<Toolbar.Separator className="ToolbarSeparator" />
<Toolbar.ToggleGroup type="single" defaultValue="center" aria-label="Text alignment" >
<Toolbar.ToggleItem className="ToolbarToggleItem" value="left" aria-label="Left aligned" >
<TextAlignLeftIcon />
</Toolbar.ToggleItem>
<Toolbar.ToggleItem className="ToolbarToggleItem" value="center" aria-label="Center aligned" >
<TextAlignCenterIcon />
</Toolbar.ToggleItem>
<Toolbar.ToggleItem className="ToolbarToggleItem" value="right" aria-label="Right aligned" >
<TextAlignRightIcon />
</Toolbar.ToggleItem>
</Toolbar.ToggleGroup>
<Toolbar.Separator className="ToolbarSeparator" />
<Toolbar.Link className="ToolbarLink" href="#" target="_blank" style={{ marginRight: 10 }} >
Edited 2 hours ago
</Toolbar.Link>
<Toolbar.Button className="ToolbarButton" style={{ marginLeft: "auto" }}>
Share
</Toolbar.Button>
</Toolbar.Root>
);
export default ToolbarDemo;

Features

    Full keyboard navigation.

安装

¥Installation

从命令行安装组件。

¥Install the component from your command line.

npm install @radix-ui/react-toolbar

结构

¥Anatomy

导入组件。

¥Import the component.

import { Toolbar } from "radix-ui";
export default () => (
<Toolbar.Root>
<Toolbar.Button />
<Toolbar.Separator />
<Toolbar.Link />
<Toolbar.ToggleGroup>
<Toolbar.ToggleItem />
</Toolbar.ToggleGroup>
</Toolbar.Root>
);

API 参考

¥API Reference

¥Root

包含所有工具栏的组件。

¥Contains all the toolbar component parts.

PropTypeDefault
asChild
boolean
false
orientation
enum
"horizontal"
dir
enum
No default value
loop
boolean
true
Data attributeValues
[data-orientation]"vertical" | "horizontal"

按钮

¥Button

一个按钮项。

¥A button item.

PropTypeDefault
asChild
boolean
false
Data attributeValues
[data-orientation]"vertical" | "horizontal"

链接

¥Link

一个链接项。

¥A link item.

PropTypeDefault
asChild
boolean
false

ToggleGroup

一组可切换为开启或关闭的双状态按钮。

¥A set of two-state buttons that can be toggled on or off.

PropTypeDefault
asChild
boolean
false
type*
enum
No default value
value
string
No default value
defaultValue
string
No default value
onValueChange
function
No default value
value
string[]
[]
defaultValue
string[]
[]
onValueChange
function
No default value
disabled
boolean
false
Data attributeValues
[data-orientation]"vertical" | "horizontal"

ToggleItem

组中的项目。

¥An item in the group.

PropTypeDefault
asChild
boolean
false
value*
string
No default value
disabled
boolean
No default value
Data attributeValues
[data-state]"on" | "off"
[data-disabled]

Present when disabled

[data-orientation]"vertical" | "horizontal"

分隔符

¥Separator

用于在工具栏中以视觉方式分隔项目。

¥Used to visually separate items in the toolbar.

PropTypeDefault
asChild
boolean
false
Data attributeValues
[data-orientation]"vertical" | "horizontal"

示例

¥Examples

与其他基础组件一起使用

¥Use with other primitives

所有暴露 Trigger 部分的基础组件,例如 DialogAlertDialogPopoverDropdownMenu,都可以使用 asChild 属性 在工具栏中组合。

¥All our primitives which expose a Trigger part, such as Dialog, AlertDialog, Popover, DropdownMenu can be composed within a toolbar by using the asChild prop.

以下是使用我们的 DropdownMenu 基础组件的示例。

¥Here is an example using our DropdownMenu primitive.

import { Toolbar, DropdownMenu } from "radix-ui";
export default () => (
<Toolbar.Root>
<Toolbar.Button>Action 1</Toolbar.Button>
<Toolbar.Separator />
<DropdownMenu.Root>
<Toolbar.Button asChild>
<DropdownMenu.Trigger>Trigger</DropdownMenu.Trigger>
</Toolbar.Button>
<DropdownMenu.Content></DropdownMenu.Content>
</DropdownMenu.Root>
</Toolbar.Root>
);

可访问性

¥Accessibility

使用 移动 tabindex 管理项目之间的焦点移动。

¥Uses roving tabindex to manage focus movement among items.

键盘交互

¥Keyboard Interactions

KeyDescription
Tab
Moves focus to the first item in the group.
Space
Activates/deactivates the item.
Enter
Activates/deactivates the item.
ArrowDown
Moves focus to the next item depending on orientation.
ArrowRight
Moves focus to the next item depending on orientation.
ArrowUp
Moves focus to the previous item depending on orientation.
ArrowLeft
Moves focus to the previous item depending on orientation.
Home
Moves focus to the first item.
End
Moves focus to the last item.