Raft 设计系统

组件 · 输入

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.

toggle-group/compositions/formatting

When to use

SituationUse
Several options in a compact row, each on or off on its ownToggleGroup with multiple
One option of a few, or noneToggleGroup
One of 2 to 4 views, and one is always selectedSegmentedControl
Options that are saved with a formCheckbox
One setting, applied at onceSwitch
Filtering a list by one of its valuesA filter (PickerTriggerButton)

The full table is in Choosing a component.

Anatomy

  • ToggleGroup: the track, in fill-muted. Its value is the list of pressed items. Required.
  • ToggleGroupItem: one 32px button, identified by its value. Required, at least 2.
    • An icon: optional, 16px. It can stand alone (aria-label required, see Accessibility).
    • ToggleGroupLabel: the visible label. It truncates when the row is too narrow.

Variants

VariantUse for
multipleOptions that combine: Bold and Italic can both be on
Without multipleOptions 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
toggle-group/compositions/alignment
toggle-group/compositions/vertical

States

StateLook
Not pressedforeground-muted text on the track; the medium tier on hover and press
PressedA yellow sticker with a bold label (Color). It doesn't change on hover; a press lays sticker-medium (Hover and press)
Disabled item, disabled groupforeground-disabled text, no reaction to hover or press (States)
toggle-group/compositions/item-disabled
toggle-group/compositions/disabled

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

  • ToggleGroup wraps the Base UI ToggleGroup and each ToggleGroupItem the Base UI Toggle: every item is a button with aria-pressed, and the arrow keys move focus between items in the direction of orientation, wrapping at the ends (loopFocus).
  • Name the group with aria-label after 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类型默认值
defaultValueThe 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[]
disabledWhether the toggle group should ignore user interaction.booleanfalse
loopFocusWhether to loop keyboard focus back to the first item when the end of the list is reached while using the arrow keys.booleantrue
multipleWhen `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.booleanfalse
onValueChangeCallback fired when the pressed states of the toggle group changes.(groupValue: T[], eventDetails: BaseToggleGroup.ChangeEventDetails) => void
orientationOrientation'horizontal'
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)
valueThe 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类型默认值
value (required)A unique string that identifies the toggle when used inside a toggle group.T
disabledWhether the component should ignore user interaction.booleanfalse
nativeButtonWhether 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>`).booleantrue
onPressedChangeCallback fired when the pressed state is changed.(pressed: boolean, eventDetails: BaseToggle.ChangeEventDetails) => void
renderAllows 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 ProfilePanel and ToggleGroup to the stable package entry point.