Components · Inputs
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, radios and grab handles are round; everything else is square or softly rounded, apart from the exceptions in Shape.
When to use
Which controls apply at once and which are saved is in Forms.
Anatomy
Switch: the 50 × 30px track on a phone and a touch screen, 38 × 22px on a desktop (macOS's regular switch), with the same strokes and the same 1px of track around the knob. Required. Beside a label, put it in aFieldItem, where it centers on the label's first line, or at the end of aListRow variant="plain", where it centers on the whole row, with or without a description; at its full size either way. A one-line settings row with a switch is 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
selectedsticker with an ink edge, yellow in every theme (Themes), and a white knob on the right. It looks the same in dark mode, where it gains the die-cut edge (Dark mode). - Disabled, off: the shared disabled look (States).
- Disabled, on: the on look, faded as a whole to
opacity-dimmed: the yellowselectedsticker, its ink edge (and the die-cut edge in dark mode) and the white knob, at 60% (Dimmed). Not the gray of the shared disabled look: gray on and gray off differ only by where the knob sits, which nobody reads at a glance, least of all in dark mode, where both tracks are the same dark gray. Faded yellow still says on, and the fade says it can't be changed. No reaction to hover or press.
Behavior
- Slide the knob over 220ms with
ease-slide(Motion). - Apply the change when the switch is flipped, with no Save. When it fails, flip the switch back and say what failed beside it (Forms).
- 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
Also takes the attributes of its element.
SwitchThumb
Also takes the attributes of its element.
Related
Changes
- 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.