NativeUI Primitives

Switch Group

Coordinate multiple switches with shared state management and layout utilities.


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

SwitchGroup manages multiple switches as a coordinated group with record-based state.

FeatureDescriptionPlatforms
SwitchGroupRoot container coordinating switchesiOS, Android, Web

Setup & Usage Guide

SwitchGroup simplifies managing multiple toggle switches together.

1. Install and Import

Install from npm:

npm install @native-ui-org/primitives

Then import from the package:

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

2. Basic Usage

Manage multiple switches as a group:

<SwitchGroup defaultValue={{ notifications: true, darkMode: false }}>
  <View style={styles.row}>
    <Text>Notifications</Text>
    <Switch name="notifications">
      {({ checked }) => (
        <View style={checked ? styles.on : styles.off}>
          <View style={styles.thumb} />
        </View>
      )}
    </Switch>
  </View>
  
  <View style={styles.row}>
    <Text>Dark Mode</Text>
    <Switch name="darkMode">
      {({ checked }) => (
        <View style={checked ? styles.on : styles.off}>
          <View style={styles.thumb} />
        </View>
      )}
    </Switch>
  </View>
</SwitchGroup>

3. Controlled State

Manage group state externally:

const [settings, setSettings] = useState({
  notifications: true,
  darkMode: false,
  autoSave: true,
});

<SwitchGroup value={settings} onValueChange={setSettings}>
  <Switch name="notifications">
    {({ checked }) => /* ... */}
  </Switch>
  <Switch name="darkMode">
    {({ checked }) => /* ... */}
  </Switch>
  <Switch name="autoSave">
    {({ checked }) => /* ... */}
  </Switch>
</SwitchGroup>

4. Disabled Group

Disable all switches at once:

<SwitchGroup disabled>
  <Switch name="option1">{({ checked }) => /* ... */}</Switch>
  <Switch name="option2">{({ checked }) => /* ... */}</Switch>
</SwitchGroup>

API Reference

SwitchGroup

Container coordinating multiple switches.

PropTypeDefaultDescription
valueRecord<string, boolean>Controlled state values
defaultValueRecord<string, boolean>{}Initial state (uncontrolled)
onValueChange(value: Record<string, boolean>) => voidCallback when any switch changes
disabledbooleanfalseDisable all switches
asChildbooleanfalseUse Slot pattern

Note: Individual switches use their name prop to identify themselves in the group state.


Platform Behavior

PlatformImplementationCharacteristics
iOS / AndroidContext-based state coordinationNative performance
WebFieldset with switchesForm compatible
All PlatformsConsistent APISame props, same behavior

Accessibility

Web:

  • Semantic grouping with fieldset
  • Individual switches properly labeled
  • Keyboard navigation

Mobile:

  • Group announced by screen readers
  • Individual switch states accessible

Version History

VersionNotes
0.1.0Initial release — switch group with record-based state management.

Summary: SwitchGroup simplifies managing multiple toggle switches. Use it for settings panels, feature flags, or grouped boolean controls. Provides coordinated state management with record-based values.