NativeUI Primitives

Switch

Create accessible toggle switches for on/off states with smooth animations across all platforms.


Preview

Interactive Demo

Installation

npm install @native-ui-org/primitives
pnpm add @native-ui-org/primitives
yarn add @native-ui-org/primitives
bun add @native-ui-org/primitives

Overview

Switch is a toggle component for binary on/off states with native-feeling animations.

FeatureDescriptionPlatforms
SwitchAnimated toggle for boolean stateiOS, Android, Web

Setup & Usage Guide

Switch provides the foundation for toggle controls with full styling control.

1. Install and Import

Install from npm:

npm install @native-ui-org/primitives

Then import from the package:

import { Switch } from "@native-ui-org/primitives";

2. Basic Usage (Uncontrolled)

Let the component manage its own state:

<Switch defaultChecked={false}>
  {({ checked }) => (
    <View style={checked ? styles.switchOn : styles.switchOff}>
      <View style={[styles.thumb, checked && styles.thumbOn]} />
    </View>
  )}
</Switch>

3. Controlled State

Manage switch state externally:

const [enabled, setEnabled] = useState(false);

<Switch checked={enabled} onCheckedChange={setEnabled}>
  {({ checked }) => (
    <View style={checked ? styles.on : styles.off}>
      <View style={styles.thumb} />
    </View>
  )}
</Switch>

4. With Label

<View style={styles.row}>
  <Text>Enable notifications</Text>
  <Switch checked={notifications} onCheckedChange={setNotifications}>
    {({ checked }) => (
      <View style={checked ? styles.on : styles.off}>
        <View style={styles.thumb} />
      </View>
    )}
  </Switch>
</View>

5. Disabled State

<Switch disabled checked={true}>
  {({ checked }) => (
    <View style={styles.disabled}>
      <View style={styles.thumb} />
    </View>
  )}
</Switch>

API Reference

Switch

Toggle component for boolean states.

PropTypeDefaultDescription
checkedbooleanControlled checked state
defaultCheckedbooleanfalseInitial state (uncontrolled)
onCheckedChange(checked: boolean) => voidCallback when state changes
disabledbooleanfalseDisable interactions
requiredbooleanfalseMark as required (forms)
namestringForm field name
valuestring"on"Form value when checked
childrenfunction | React.ReactNodeRender function or static content

Platform Behavior

PlatformImplementationCharacteristics
iOS / AndroidAnimated View componentsNative 60fps animations
WebButton with animated stateKeyboard accessible
All PlatformsConsistent APISame props, same behavior

Accessibility

Web:

  • role="switch"
  • aria-checked reflects state
  • Keyboard support (Space to toggle)
  • Focus management

Mobile:

  • Accessibility role "switch"
  • State announced to screen readers
  • Works with VoiceOver and TalkBack

Version History

VersionNotes
0.1.0Initial release — switch with controlled/uncontrolled states and animations.

Summary: Switch provides accessible toggle controls for boolean states. Use it for settings, feature toggles, or any on/off control. Supports both controlled and uncontrolled modes with proper accessibility.