组件 · 输入
Switch
A round on/off control for a setting that applies the moment it is flipped.
import { Switch } from "@botiverse/raft-ui";A switch turns a setting on or off, and the change applies at once. On, it is a yellow sticker with its knob slid to the right. Switches and radios are the only round controls in the system.
When to use
| Situation | Use |
|---|---|
| A setting row that applies when flipped | Switch |
| A yes or no that applies when the form is saved | Checkbox |
| One of several answers | RadioGroup |
Which controls apply at once and which are saved is in Forms.
Anatomy
Switch: the 50 × 30px track, the same on a phone and a desktop. Required. Beside a label, put it in aFieldItemor at the end of aListRow variant="plain": it centers on the label's or title's first line at its full 50 × 30. A one-line settings row is centered as a whole, 54px on a phone. Don't add amargin-top(Alignment).SwitchThumb: the round knob.Switchrenders it by default.
States
- Off: a
fill-mutedtrack with an ink border, and a white knob on the left. - On: a
brandsticker with an ink edge, and a white knob on the right. It looks the same in dark mode, where it gains the die-cut edge (Dark mode). - Disabled: the shared disabled look, on or off (States).
Behavior
- Slide the knob over 220ms with
ease-slide(Motion). - Put the switch at the end of a settings row (ListRow with
interactive={false}). The row itself is not a button and doesn't react to hover or press; focus goes only to the switch.
Content
- Make the description say what follows from the setting, without repeating the title: for Agents can merge to main, "Off: a person approves every merge."
Accessibility
Switch wraps Base UI Switch: an element with role="switch" whose aria-checked says whether it is on, with a hidden native input for forms.
- Name it with
aria-labelledbypointing at the row's title. - Set
disabledwhen the setting can't be changed.
Rules
- Keep the switch a pill, though everything else is square: people recognize a switch by its round shape, and a square switch isn't read as one (Shape).
- Never color a switch green or blue: green means done and online, and an on switch is yellow.
API
Switch
| Prop | 类型 | 默认值 |
|---|---|---|
| Whether the switch is currently active. | boolean | |
| Whether the switch is initially active. | boolean | false |
| Whether the component should ignore user interaction. | boolean | false |
| Identifies the form that owns the hidden input. Useful when the switch is rendered outside the form. | string | |
| The id of the hidden input element. | string | |
| A ref to access the hidden `<input>` element. | ((instance: HTMLInputElement | null) => void | (() => void | { [UNDEFINED_VOID_ONLY]: never; }) | DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES[keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES]) | RefObject<HTMLInputElement | null> | |
| Identifies the field when a form is submitted. | string | |
| Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `true` if the rendered element is a native button. | boolean | false |
| Event handler called when the switch is activated or deactivated. | (checked: boolean, eventDetails: BaseSwitch.Root.ChangeEventDetails) => void | |
| Whether the user should be unable to activate or deactivate the switch. | boolean | false |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) | |
| Whether the user must activate the switch before submitting a form. | boolean | false |
| The value submitted with the form when the switch is off. By default, unchecked switches do not submit any value, matching native checkbox behavior. | string | |
| The value submitted with the form when the switch is on. By default, switch submits the "on" value, matching native checkbox behavior. | string |
Also takes the attributes of its element.
SwitchThumb
| Prop | 类型 | 默认值 |
|---|---|---|
| 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.
相关组件
改动
- 0.6.0 · Breaking ChangesProps that no longer change anything are removed:
Badgeuppercase;Checkboxsize,variant,color;Statuspulse,attention(status never animates);Switch/SwitchThumbsize;SidebarItemvariant;SidebarItemCount/SidebarItemIndicatorvariantandappearance(the count is always the pink sticker, the indicator the yellow @);NotificationCenterActionButtonvariant;DrawerContentwidth(one-go forms use the wideDialog);AppRailItemIndicatorattention;AppRailItemAttentionchildren;Stepssize,track,renderIndicator,renderTraveler;FilePreviewBadgetype,color. Remove them at call sites.