SwipeUI

Switch

An accessible switch with a draggable, stretching thumb and optimistic setting updates.

npx @swipeui/cli add switch
Variant
Preset
Material
Scheme
Feel
Real free component with a browser animation bridge. Native motion, haptics and materials need a device.

When 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.tsx and 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 install so versions match your SDK.

Usage

app/settings.tsx
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

PropTypeDefaultDescription
accessibilityLabel?string—

Spoken name, overriding label.

accessibilityHint?string—

Supporting spoken setting description.

label?string—

Visible label and default spoken name.

checked?boolean—

Controlled checked state; takes precedence over value.

value?boolean—

Legacy controlled state.

defaultChecked?booleanfalse

Initial checked state.

defaultValue?booleanfalse

Legacy initial checked state.

onValueChange?((value: boolean) => void)—

Legacy committed-state callback.

onCheckedChange?((value: boolean) => void | Promise<void>)—

Change handler; a promise enables optimistic persistence with rollback.

disabled?booleanfalse

Disable interaction.

size?"lg" | "md" | "sm"'md'

Track geometry; touch target is always at least 44pt.

stretch?booleantrue

Stretch the thumb while pressed.

draggable?booleantrue

Drag past the midpoint to flip.

thumbIcon?unionundefined

A built-in icon morph drawn in the thumb.

trackLabels?booleanfalse

Draw on/off marks on the track.

minPending?number450

Minimum spinner visibility for asynchronous updates, in milliseconds.

haptics?boolean | (SelectionHaptics & { error?: boolean; })true

Per-event feedback.

motion?MotionOverrideapp feel

Local toggle and drag feel.

messages?{ error?: string; }English

Translate built-in error feedback.

className?string—

Root classes.

style?StyleProp<ViewStyle>—

Root style.

testID?string—

Root identifier; thumb appends -thumb.

SwitchRowProps

PropTypeDefaultDescription
accessibilityLabel?string—

Spoken name, overriding label.

accessibilityHint?string—

Supporting spoken setting description.

label?string—

Visible label and default spoken name.

checked?boolean—

Controlled checked state; takes precedence over value.

value?boolean—

Legacy controlled state.

defaultChecked?booleanfalse

Initial checked state.

defaultValue?booleanfalse

Legacy initial checked state.

onValueChange?((value: boolean) => void)—

Legacy committed-state callback.

onCheckedChange?((value: boolean) => void | Promise<void>)—

Change handler; a promise enables optimistic persistence with rollback.

disabled?booleanfalse

Disable interaction.

size?"lg" | "md" | "sm"'md'

Track geometry; touch target is always at least 44pt.

stretch?booleantrue

Stretch the thumb while pressed.

draggable?booleantrue

Drag past the midpoint to flip.

thumbIcon?unionundefined

A built-in icon morph drawn in the thumb.

trackLabels?booleanfalse

Draw on/off marks on the track.

minPending?number450

Minimum spinner visibility for asynchronous updates, in milliseconds.

haptics?boolean | (SelectionHaptics & { error?: boolean; })true

Per-event feedback.

motion?MotionOverrideapp feel

Local toggle and drag feel.

messages?{ error?: string; }English

Translate built-in error feedback.

className?string—

Root classes.

style?StyleProp<ViewStyle>—

Root style.

testID?string—

Root identifier; thumb appends -thumb.

description?string—

Supporting setting explanation.

icon?ReactNode—

Caller-owned leading icon.

Switch settings

PropDoesDefault
hapticsEnables selection or press feedbacktrue
motionOverrides the app's motion feelApp 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 press and toggle motion roles, so it follows your app's feel. Pass motion to 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 onSupported
Expo SDK 57Yes
Expo SDK 58Yes
React Native CLI (with Expo modules)Yes
Expo GoYes
iOSYes
AndroidYes

This table describes API support and static bundle checks. Native runtime, device accessibility and bare Uniwind compatibility remain pending.

Related items