Raft 设计系统

组件 · 输入

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.

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
One of several answersRadioGroup

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 a FieldItem or at the end of a ListRow 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 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 brand sticker 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-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

Prop类型默认值
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

Prop类型默认值
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.

改动

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