Components

切换组

¥Toggle Group

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

import * as React from "react";
import { ToggleGroup } from "radix-ui";
import { TextAlignLeftIcon, TextAlignCenterIcon, TextAlignRightIcon, } from "@radix-ui/react-icons";
import "./styles.css";
const ToggleGroupDemo = () => (
<ToggleGroup.Root className="ToggleGroup" type="single" defaultValue="center" aria-label="Text alignment" >
<ToggleGroup.Item className="ToggleGroupItem" value="left" aria-label="Left aligned" >
<TextAlignLeftIcon />
</ToggleGroup.Item>
<ToggleGroup.Item className="ToggleGroupItem" value="center" aria-label="Center aligned" >
<TextAlignCenterIcon />
</ToggleGroup.Item>
<ToggleGroup.Item className="ToggleGroupItem" value="right" aria-label="Right aligned" >
<TextAlignRightIcon />
</ToggleGroup.Item>
</ToggleGroup.Root>
);
export default ToggleGroupDemo;

Features

    Full keyboard navigation.

    Supports horizontal/vertical orientation.

    Support single and multiple pressed buttons.

    Can be controlled or uncontrolled.

安装

¥Installation

从命令行安装组件。

¥Install the component from your command line.

npm install @radix-ui/react-toggle-group

结构

¥Anatomy

导入组件。

¥Import the component.

import { ToggleGroup } from "radix-ui";
export default () => (
<ToggleGroup.Root>
<ToggleGroup.Item />
</ToggleGroup.Root>
);

API 参考

¥API Reference

¥Root

包含切换组的所有部分。

¥Contains all the parts of a toggle group.

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
rovingFocus
boolean
true
orientation
enum
undefined
dir
enum
No default value
loop
boolean
true
Data attributeValues
[data-orientation]"vertical" | "horizontal"

项目

¥Item

组中的项目。

¥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"

示例

¥Examples

确保始终存在值

¥Ensuring there is always a value

你可以控制组件以确保值。

¥You can control the component to ensure a value.

import * as React from "react";
import { ToggleGroup } from "radix-ui";
export default () => {
const [value, setValue] = React.useState("left");
return (
<ToggleGroup.Root type="single" value={value} onValueChange={(value) => { if (value) setValue(value); }} >
<ToggleGroup.Item value="left">
<TextAlignLeftIcon />
</ToggleGroup.Item>
<ToggleGroup.Item value="center">
<TextAlignCenterIcon />
</ToggleGroup.Item>
<ToggleGroup.Item value="right">
<TextAlignRightIcon />
</ToggleGroup.Item>
</ToggleGroup.Root>
);
};

可访问性

¥Accessibility

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

¥Uses roving tabindex to manage focus movement among items.

键盘交互

¥Keyboard Interactions

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