组件 · 输入
ToggleGroup
A compact row of toggle buttons in a gray track, each one on or off on its own, or one of them or none; a pressed button is a yellow sticker.
也叫 Toggle buttons, Toolbar toggles
import { ToggleGroup } from "@botiverse/raft-ui";A toggle group is a row of buttons in a gray track that people press on and off: Bold, Italic and Underline, or a row of filters, each selectable on its own (Built from existing parts). With multiple, any number can be on; without it, one or none. A pressed button is a yellow sticker.
When to use
| Situation | Use |
|---|---|
| Several options in a compact row, each on or off on its own | ToggleGroup with multiple |
| One option of a few, or none | ToggleGroup |
| One of 2 to 4 views, and one is always selected | SegmentedControl |
| Options that are saved with a form | Checkbox |
| One setting, applied at once | Switch |
| Filtering a list by one of its values | A filter (PickerTriggerButton) |
The full table is in Choosing a component.
Anatomy
ToggleGroup: the track, infill-muted. Itsvalueis the list of pressed items. Required.ToggleGroupItem: one 32px button, identified by itsvalue. Required, at least 2.- An icon: optional, 16px. It can stand alone (
aria-labelrequired, see Accessibility). ToggleGroupLabel: the visible label. It truncates when the row is too narrow.
- An icon: optional, 16px. It can stand alone (
Variants
| Variant | Use for |
|---|---|
multiple | Options that combine: Bold and Italic can both be on |
Without multiple | Options that exclude each other, where none is also an answer: an alignment that can be cleared |
orientation="vertical" | A stack of toggles in a narrow column |
States
| State | Look |
|---|---|
| Not pressed | foreground-muted text on the track; the medium tier on hover and press |
| Pressed | A yellow sticker with a bold label (Color). It doesn't change on hover; a press lays sticker-medium (Hover and press) |
| Disabled item, disabled group | foreground-disabled text, no reaction to hover or press (States) |
Behavior
- A horizontal group wraps its items onto a new row when they don't fit, and never grows wider than its container.
- Pressing a pressed item turns it off, in both variants.
- Items sit 3px inside the track and 3px apart, like the segments of a SegmentedControl.
- Items are 32px high and don't extend their hit area to 44px. On touch screens that is below the hit target.
Content
- Label each item with what it turns on, in Title Case: Bold, Numbered List (Writing).
Accessibility
ToggleGroupwraps the Base UI ToggleGroup and eachToggleGroupItemthe Base UI Toggle: every item is a button witharia-pressed, and the arrow keys move focus between items in the direction oforientation, wrapping at the ends (loopFocus).- Name the group with
aria-labelafter what it controls: "Text formatting", "Text alignment". - Give every icon-only item an
aria-label("Align left"), and hide its icon (aria-hidden).
Rules
- Don't use a toggle group to switch views or sections: one view is always shown, and a toggle group can be left with none. Use a SegmentedControl or Tabs.
API
ToggleGroup
| Prop | 类型 | 默认值 |
|---|---|---|
| The pressed state of the toggle group represented by an array of the values of all pressed toggle buttons. This is the uncontrolled counterpart of `value`. | readonly T[] | |
| Whether the toggle group should ignore user interaction. | boolean | false |
| Whether to loop keyboard focus back to the first item when the end of the list is reached while using the arrow keys. | boolean | true |
| When `false` only one item in the group can be pressed. If any item in the group becomes pressed, the others will become unpressed. When `true` multiple items can be pressed. | boolean | false |
| Callback fired when the pressed states of the toggle group changes. | (groupValue: T[], eventDetails: BaseToggleGroup.ChangeEventDetails) => void | |
| Orientation | 'horizontal' | |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) | |
| The pressed state of the toggle group represented by an array of the values of all pressed toggle buttons. This is the controlled counterpart of `defaultValue`. | readonly T[] |
Also takes the attributes of its element.
ToggleGroupItem
| Prop | 类型 | 默认值 |
|---|---|---|
| A unique string that identifies the toggle when used inside a toggle group. | T | |
| Whether the component should ignore user interaction. | boolean | false |
| Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`). | boolean | true |
| Callback fired when the pressed state is changed. | (pressed: boolean, eventDetails: BaseToggle.ChangeEventDetails) => void | |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
ToggleGroupLabel
接受其元素的全部属性。
相关组件
改动
- 0.3.0 · AddedGraduated
ProfilePanelandToggleGroupto the stable package entry point.