suiss/uim · React Native Component

Toggle

A two-state button that can be turned on or off, for formatting and filter controls.

Toggle is an open-source mobile component from suiss/uim for React Native and Expo, with SwiftUI and Jetpack Compose versions built from the same design tokens. The @esuiss/uim CLI copies its source into your project, so you own the code.

iOS
Toggle on iOS in light mode

Toggle is a Pressable with an on and off state. When on, it shows a selected background and its text turns to the link color; it scales down slightly while pressed. It has two variants, default and outline, and three sizes.

ToggleIcon is an Icon sized for use inside a toggle.

Installation

$ pnpm dlx @esuiss/uim@latest add toggle

The CLI also adds what it builds on: Icon, Text.

Manual

  1. Install the dependencies:
npx expo install class-variance-authority cn
  1. Add the pieces it builds on: Icon, Text, and the primitives components/primitives/toggle.tsx, components/primitives/use-render.tsx.

  2. Copy the source into components/ui/toggle.tsx:

uniwind/components/ui/toggle.tsx
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import { Pressable } from "react-native"
import {
  Toggle as TogglePrimitive,
  type ToggleState,
} from "@/components/primitives/toggle"
import { resolveClassName } from "@/components/primitives/use-render"
import { Icon } from "@/components/ui/icon"
import { TextClassContext } from "@/components/ui/text"

const toggleVariants = cva(
  "flex-row items-center justify-center gap-1 rounded-lg",
  {
    variants: {
      variant: {
  1. Update the import paths to match your project.

Usage

import { Toggle, ToggleIcon } from "@/components/ui/toggle"
<View className="flex-row">
  <ToggleDemo accessibilityLabel="Toggle bold">
    <ToggleIcon as={TextBIcon} />
  </ToggleDemo>
</View>

Control the state with pressed and onPressedChange(pressed, details), or leave it uncontrolled with defaultPressed. Put text in a Text and icons in ToggleIcon.

Icon-only toggles need an accessibilityLabel. To group toggles with single or multiple selection, use Toggle Group. toggleVariants, toggleStateClassName and toggleTextClassName are exported for reuse in custom components.

Examples

Pressed

An icon toggle that starts in the on state.

iOS
Toggle (pressed) on iOS in light mode

Variants

The default and outline variants.

iOS
Toggle (variants) on iOS in light mode

Sizes

Outline toggles in the sm, default and lg sizes.

iOS
Toggle (sizes) on iOS in light mode

With text

An outline toggle with an icon and a text label.

iOS
Toggle (with text) on iOS in light mode

Disabled

Disabled toggles in the off and on states.

iOS
Toggle (disabled) on iOS in light mode

API Reference

Toggle

The pressable two-state button.

ExtendsReact.ComponentProps<typeof TogglePrimitive>
PropType
About
Visual style: default or outline.
Type
"default""outline"
Default
"default"
About
Height and padding: default, sm or lg.
Type
"default""sm""lg"
Default
"default"

ToggleIcon

An icon sized for a toggle.

ExtendsReact.ComponentProps<typeof Icon>

Accessibility

  • Uses the togglebutton role and reports checked and disabled in accessibilityState.
  • Inside a single-select Toggle Group, it uses the radio role instead.
  • Icon-only toggles need an accessibilityLabel.

Notes

  • Changing the state triggers a selection haptic when a haptics adapter is provided through HapticsContext.
  • Uses the bg-item-selected, bg-item-pressed, bg-control-disabled, text-link and border-separator tokens.
  • Light and dark colors follow the system appearance.