Switch
An accessible switch with a draggable, stretching thumb and optimistic setting updates.
npx @swipeui/cli add switchWhen to use it
- Settings that apply immediately, such as Notifications, Dark mode or Location access.
- On/off rows in a settings list, labelled by the row.
- Feature toggles that don't need a Save button.
When not to use it
- Choices that are submitted later with a form. Use Checkbox.
- Choosing between named options, like Daily or Weekly. Use Segmented control or Radio group.
- Actions that run once, like Sync now. Use Button.
Installation
Run this from your app's root. If SwipeUI isn't set up yet, run npx @swipeui/cli init first; it sets up Uniwind, the theme tokens and the cn helper.
npx @swipeui/cli add switch- Adds
components/swipeui/ui/use-switch.ts,components/swipeui/ui/switch.tsxand 15 shared helpers. - Installs react-native-reanimated, react-native-worklets, react-native-gesture-handler, uniwind, clsx, tailwind-merge, expo-haptics, class-variance-authority, react-native-svg. In Expo apps it uses
npx expo installso versions match your SDK.
Usage
import { Switch } from '@/components/swipeui/ui/switch';
<Switch accessibilityLabel="Notifications" value={enabled} onValueChange={setEnabled} />;The track is bg-input when off and bg-primary when on; the thumb is bg-background. The thumb uses the shared toggle spring on the UI thread, and jumps when Reduce Motion is on. The preset supplies the default feel; motion can override it locally.
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
accessibilityLabel? | | — | Spoken name, overriding label. |
accessibilityHint? | | — | Supporting spoken setting description. |
label? | | — | Visible label and default spoken name. |
checked? | | — | Controlled checked state; takes precedence over value. |
value? | | — | Legacy controlled state. |
defaultChecked? | | | Initial checked state. |
defaultValue? | | | Legacy initial checked state. |
onValueChange? | | — | Legacy committed-state callback. |
onCheckedChange? | | — | Change handler; a promise enables optimistic persistence with rollback. |
disabled? | | | Disable interaction. |
size? | | | Track geometry; touch target is always at least 44pt. |
stretch? | | | Stretch the thumb while pressed. |
draggable? | | | Drag past the midpoint to flip. |
thumbIcon? | | | A built-in icon morph drawn in the thumb. |
trackLabels? | | | Draw on/off marks on the track. |
minPending? | | | Minimum spinner visibility for asynchronous updates, in milliseconds. |
haptics? | | | Per-event feedback. |
motion? | | | Local toggle and drag feel. |
messages? | | | Translate built-in error feedback. |
className? | | — | Root classes. |
style? | | — | Root style. |
testID? | | — | Root identifier; thumb appends -thumb. |
SwitchRowProps
| Prop | Type | Default | Description |
|---|---|---|---|
accessibilityLabel? | | — | Spoken name, overriding label. |
accessibilityHint? | | — | Supporting spoken setting description. |
label? | | — | Visible label and default spoken name. |
checked? | | — | Controlled checked state; takes precedence over value. |
value? | | — | Legacy controlled state. |
defaultChecked? | | | Initial checked state. |
defaultValue? | | | Legacy initial checked state. |
onValueChange? | | — | Legacy committed-state callback. |
onCheckedChange? | | — | Change handler; a promise enables optimistic persistence with rollback. |
disabled? | | | Disable interaction. |
size? | | | Track geometry; touch target is always at least 44pt. |
stretch? | | | Stretch the thumb while pressed. |
draggable? | | | Drag past the midpoint to flip. |
thumbIcon? | | | A built-in icon morph drawn in the thumb. |
trackLabels? | | | Draw on/off marks on the track. |
minPending? | | | Minimum spinner visibility for asynchronous updates, in milliseconds. |
haptics? | | | Per-event feedback. |
motion? | | | Local toggle and drag feel. |
messages? | | | Translate built-in error feedback. |
className? | | — | Root classes. |
style? | | — | Root style. |
testID? | | — | Root identifier; thumb appends -thumb. |
description? | | — | Supporting setting explanation. |
icon? | | — | Caller-owned leading icon. |
Switch settings
| Prop | Does | Default |
|---|---|---|
| haptics | Enables selection or press feedback | true |
| motion | Overrides the app's motion feel | App feel |
| checked / value | Controlled state; checked takes precedence | Uncontrolled |
| defaultChecked / defaultValue | Initial state | false |
| onCheckedChange | Save a change; promises show pending and roll back on failure | Unset |
| onValueChange | Legacy committed-state callback | Unset |
| label | Visible label and accessible name | Unset |
| size | Small, medium or large track; target stays 44pt | md |
| stretch | Widen the thumb while pressed | true |
| draggable | Flip by dragging past the midpoint | true |
| thumbIcon | Morph pair inside the thumb | Unset |
| trackLabels | Draw on/off marks | false |
| minPending | Minimum async spinner time in milliseconds | 450 |
| disabled | Prevent changes | false |
| messages | Translate error feedback | English |
Composition and behavior
Wrap the app in GestureHandlerRootView for thumb gestures. SwitchRow makes its complete label-and-description row one switch target. accessibilityLabel overrides the name derived from label; the checked state stays available on the switch role. Selection haptics land at the accepted thumb detent. Async failures restore the previous value, announce messages.error and use the error haptic.
- A 52 by 32 point track with a sliding thumb.
- value and onValueChange control it; defaultValue for uncontrolled use.
- haptics and motion adjust feedback.
Behavior and gestures
- Tap to flip; the thumb slides across and you feel a light selection tick when it lands.
- The tick plays only once the change is accepted, not if the animation is interrupted.
- Disabled switches dim and ignore taps.
- In right-to-left layouts the thumb moves the other way.
Accessibility
- Announced as a switch with on or off state.
- Give it accessibilityLabel unless a surrounding row already labels it.
- The 32-point track has extra touch area, so the target is 44 points.
- Reduce Motion makes the thumb jump instead of sliding.
Motion
- Uses the
pressandtogglemotion roles, so it follows your app's feel. Passmotionto tune this one instance. - The thumb slides with the toggle spring, clamped so it doesn't bounce past the end.
- Reduce Motion and the none feel make it jump.
Performance
- The thumb runs on the UI thread with Reanimated 4.
Notes for AI
- Pass label or accessibilityLabel. Wrap the app in GestureHandlerRootView for drag support.
- Bare apps need the Reanimated and Worklets Babel plugin; the CLI installs both packages.
Compatibility
| Runs on | Supported |
|---|---|
| Expo SDK 57 | Yes |
| Expo SDK 58 | Yes |
| React Native CLI (with Expo modules) | Yes |
| Expo Go | Yes |
| iOS | Yes |
| Android | Yes |
This table describes API support and static bundle checks. Native runtime, device accessibility and bare Uniwind compatibility remain pending.