A two-state setting that takes effect immediately; use a checkbox when it applies only after Save. The track measures 32 × 18 on desktop and 51 × 31 on phones (the native iOS size). The on state uses the brand color and the thumb slides on a spring in one continuous move.
Status: stable · Components: Switch
Import
Section titled “Import”import { Switch } from 'nacre'import { Text, View, signal } from 'nsh'import { Switch } from 'nacre'import { TEXT_SIZE } from '../../src/textSize'
export function SwitchUsage() { const sound = signal(true) return ( <View class="flex-row items-center gap-3"> <Switch checked={sound()} onChange={(checked) => sound.set(checked)} accessibilityLabel="New message sound" /> <Text class={`${TEXT_SIZE.small} text-foreground`}>New message sound</Text> </View> )}Examples
Section titled “Examples”States
Section titled “States”On, off, disabled on, disabled off. A disabled switch can’t be pressed and is not in the Tab order.
import { View, signal } from 'nsh'import { Switch } from 'nacre'
export function SwitchStates() { const on = signal(true) const off = signal(false) return ( <View class="flex-row flex-wrap items-center gap-4"> <Switch checked={on()} onChange={(checked) => on.set(checked)} accessibilityLabel="On" /> <Switch checked={off()} onChange={(checked) => off.set(checked)} accessibilityLabel="Off" /> <Switch checked onChange={() => {}} accessibilityLabel="Disabled on" disabled /> <Switch checked={false} onChange={() => {}} accessibilityLabel="Disabled off" disabled /> </View> )}Settings rows
Section titled “Settings rows”Use TouchRow on phones with a leading label and description and a trailing switch. On desktop use a horizontal Field.
import { View, signal } from 'nsh'import { Field, Switch, TouchRow } from 'nacre'import { TOUCH } from '../../src/textSize'
export function SwitchSettings() { const quiet = signal(false) const badge = signal(true) const quietLabel = () => 'Do not disturb' const badgeLabel = () => 'Dock badge' const hint = () => 'New messages won’t show notifications but still count as unread' const quietSwitch = <Switch checked={quiet()} onChange={(checked) => quiet.set(checked)} accessibilityLabel={quietLabel()} /> const badgeSwitch = <Switch checked={badge()} onChange={(checked) => badge.set(checked)} accessibilityLabel={badgeLabel()} /> if (TOUCH) return <View class="self-stretch"> <TouchRow label={quietLabel()} description={hint()} trailing={quietSwitch} /> <TouchRow label={badgeLabel()} trailing={badgeSwitch} /> </View> return ( <View class="w-[360px] max-w-full gap-3"> <Field orientation="horizontal" label={quietLabel()} description={hint()}>{quietSwitch}</Field> <Field orientation="horizontal" label={badgeLabel()}>{badgeSwitch}</Field> </View> )}Inverse surface
Section titled “Inverse surface”On a solid primary fill, use surface=“inverse”: a faint foreground track when off, a solid foreground track when on, a primary thumb and a foreground focus ring.
import { Text, View, signal } from 'nsh'import { Switch } from 'nacre'import { TEXT_SIZE, TOUCH } from '../../src/textSize'
export function SwitchInverse() { const online = signal(true) return ( <View class={`${TOUCH ? 'min-h-12' : 'h-9'} px-3 rounded-lg bg-primary flex-row items-center gap-3 self-start`}> <Text class={`${TEXT_SIZE.small} font-medium text-primary-foreground`}>HK hotline</Text> <Switch surface="inverse" checked={online()} onChange={(checked) => online.set(checked)} accessibilityLabel="HK hotline online" /> <Switch surface="inverse" checked={false} onChange={() => {}} disabled accessibilityLabel="Disabled" /> </View> )}Keyboard
Section titled “Keyboard”| Key | Action |
|---|---|
| Tab | Focus the switch |
| Space | Toggle |
Accessibility
Section titled “Accessibility”- role is switch and the on state reads as checked; accessibilityLabel is required and names the setting (“Do not disturb”), not “on / off”.
- A disabled switch is not in the Tab order.
Do / Don’t
Section titled “Do / Don’t”Do
- Take effect as soon as it’s toggled
- Label it with the setting’s name, not an action
Don’t
- Form fields that apply only after Save (use a checkbox)
- Use a switch for two different options (use a segmented control or radio buttons)
Property descriptions follow the Chinese source comments.
Switch
Section titled “Switch”| Prop | Type | Default | Description |
|---|---|---|---|
checked * |
boolean |
— | |
onChange * |
(checked: boolean) => void |
— | |
accessibilityLabel * |
string |
— | |
disabled |
boolean |
— | |
surface |
'default' | 'inverse' |
— | default 白卡与浮层上;inverse 主色实底上:轨道前景色淡底、开态前景色实底,滑块取主色,焦点环取前景色 |
testID |
string |
— | |
class |
string |
— |
* required