Switch

A persistent on or off setting.

  • Native host
  • In the first release
import { Switch, switchPrimitive } from "nitro-ui-sdk/switch";
HeroUI Native
Switch
Expo UI
SwitchToggle
  • Switch example screen, light appearance
    Light
  • Switch example screen, dark appearance
    Dark
On this page

A persistent on/off setting backed by the native platform switch (UISwitch on iOS, MaterialSwitch on Android). Use Switch when toggling takes effect immediately. Use Checkbox for form options submitted later or when a tri-state "mixed" value is required.

Anatomy

<Switch.Root label="Notifications" hint="Receive project updates">
  <Switch.Label />
  <Switch.Hint />
  <Switch.Control />
</Switch.Root>
Part Description
Switch.Root Manages on/off state, controller attachment, agent registration, and row layout.
Switch.Label Primary row label (hidden from duplicate screen-reader focus).
Switch.Hint Secondary helper text tied to the switch.
Switch.Control Native UISwitch / MaterialSwitch host with at least a 44 pt tap target.

Usage

import { Switch } from "nitro-ui-sdk/switch";

<Switch.Root label="Notifications" defaultValue />;

Variants and sizes

variant selects "standard" (default, semantic active track with the platform thumb) or "tonal" (pairs the active track with its semantic foreground thumb and a dark-mode contrast-balanced midtone track). color supports "accent" (default), "success", and "danger". size is fixed to "system" to match the native platform switch metrics.

<Switch.Root label="Accent" color="accent" defaultValue />
<Switch.Root label="Auto-save" color="success" defaultValue />
<Switch.Root label="Maintenance lock" color="danger" defaultValue />
<Switch.Root label="Background sync" variant="tonal" defaultValue />

States

Pass disabled to prevent changes and dim the control and captions using tokens.opacity.disabled.

<Switch.Root label="Managed by policy" defaultValue disabled />

Controlled

Provide value and onValueChange to own the boolean state. Leaving value unchanged or calling details.preventDefault() reconciles the native switch back to the committed value.

import { useState } from "react";
import { Switch } from "nitro-ui-sdk/switch";

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

<Switch.Root
  label="Offline mode"
  value={enabled}
  onValueChange={(next) => setEnabled(next)}
/>;

Props

Switch.Root

Prop Type Default Description
label string — Required visible and accessible name.
hint string undefined Optional helper text and accessibility hint.
value boolean undefined Controlled checked state.
defaultValue boolean false Initial uncontrolled checked state.
onValueChange (value: boolean, details: ActionDetails) => void undefined Called before the switch value changes.
variant "standard" | "tonal" "standard" Track and thumb pairing style.
size "system" "system" Fixed system switch size.
color "accent" | "success" | "danger" "accent" Semantic intent color for the active track.
disabled boolean false Disables user, agent, and controller updates.
styles PartStyles<SwitchPart, SwitchVariant, SwitchSize, SwitchState> undefined Per-part style overrides.
api Controller<SwitchActions> undefined Imperative controller created with switchPrimitive.createApi().
agentId string undefined Stable agent identity when rendered in recyclable rows.
layout SharedViewProps["layout"] undefined Additional layout props merged onto the root container.
children ReactNode Default anatomy Custom part composition.

Switch.Control, Switch.Label, Switch.Hint

Prop Type Default Description
part SwitchPart Matching part name Overrides the style slot resolved from styles.
children string From root props Optional text override for Label or Hint.

Controller API

import { Switch, switchPrimitive } from "nitro-ui-sdk/switch";

const api = switchPrimitive.createApi();
<Switch.Root label="Notifications" api={api} />;
api.current?.toggle();
Action Signature Result
toggle (source?: ActionSource) => ChangeStatus Flips the switch between true and false.
setChecked (checked: boolean, source?: ActionSource) => ChangeStatus Sets an explicit boolean state.

Styles

Style resolvers receive { tokens, variant, size, state } where state is { disabled: boolean; checked: boolean; color: SwitchColor }.

<Switch.Root
  label="Custom track"
  defaultValue
  styles={{
    control: ({ tokens }) => ({ text: { color: tokens.colors.warning } }),
  }}
/>

Accessibility

Switch.Control hosts the native UISwitch / MaterialSwitch with accessibilityLabel and accessibilityHint, so VoiceOver and TalkBack announce the switch role, label, on/off value, and hint as a single control. Switch.Label and Switch.Hint remain hidden from duplicate assistive focus. On iOS, UISwitch supplies native haptic feedback on touch; agent and API actions never vibrate.

Agent API

Action Arguments Notes
toggle none Flips the switch and settles controlled updates.
setChecked boolean Sets an explicit boolean state.

Platform notes

iOS Android
Implementation UISwitch centered inside a 44 pt+ tap host. UISwitch has no public UIKit API for the off-track fill color, so inactiveColor is omitted on iOS and UIKit renders its native off track. MaterialSwitch with selectionHint accessibility delegate; tokens.colors.default tints the off-state track via trackTintList.
Verification Unit, type, iOS harness, and simulator screenshot verified. Written to compile; not built locally (no Android NDK on this machine).