Raft Design System

Components · Inputs

Switch

A round on/off control for a setting that applies the moment it is flipped.

Stable
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.

list-row/compositions/settings

When to use

SituationUse
A setting row that applies when flippedSwitch
A yes or no that applies when the form is savedCheckbox
A feature that can't be on until fields are filledCheckbox, with the fields and one Save
One of several answersRadioGroup

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 a FieldItem, where it centers on the label's first line, or at the end of a ListRow 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 a margin-top (Alignment).
  • SwitchThumb: the round knob. Switch renders it by default.

States

  • Off: a fill-muted track with an ink border, and a white knob on the left.
  • On: a selected sticker 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 yellow selected sticker, 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.
switch/switch

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-labelledby pointing at the row's title.
  • Set disabled when 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

PropTypeDefault
checkedWhether the switch is currently active.boolean
defaultCheckedWhether the switch is initially active.booleanfalse
disabledWhether the component should ignore user interaction.booleanfalse
formIdentifies the form that owns the hidden input. Useful when the switch is rendered outside the form.string
idThe id of the hidden input element.string
inputRefA 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>
nameIdentifies the field when a form is submitted.string
nativeButtonWhether 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.booleanfalse
onCheckedChangeEvent handler called when the switch is activated or deactivated.(checked: boolean, eventDetails: BaseSwitch.Root.ChangeEventDetails) => void
readOnlyWhether the user should be unable to activate or deactivate the switch.booleanfalse
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)
requiredWhether the user must activate the switch before submitting a form.booleanfalse
uncheckedValueThe value submitted with the form when the switch is off. By default, unchecked switches do not submit any value, matching native checkbox behavior.string
valueThe 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

PropTypeDefault
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.

Changes

  • 0.6.0 · Breaking ChangesProps that no longer change anything are removed: Badge uppercase; Checkbox size, variant, color; Status pulse, attention (status never animates); Switch / SwitchThumb size; SidebarItem variant; SidebarItemCount / SidebarItemIndicator variant and appearance (the count is always the pink sticker, the indicator the yellow @); NotificationCenterActionButton variant; DrawerContent width (one-go forms use the wide Dialog); AppRailItemIndicator attention; AppRailItemAttention children; Steps size, track, renderIndicator, renderTraveler; FilePreviewBadge type, color. Remove them at call sites.