Unified UI

Switch

A toggle switch component with spring animation, label support, and configurable label position.

Overview

The Switch component is a toggle for binary on/off settings. Built on Radix UI with spring animation via Framer Motion. Supports labels, descriptions, two sizes, and configurable label position.


Installation

Install the component via the CLI in one command.

npx @work-rjkashyap/unified-ui add switch
pnpm dlx @work-rjkashyap/unified-ui add switch
npx @work-rjkashyap/unified-ui add switch
bunx @work-rjkashyap/unified-ui add switch

If you haven't initialized your project yet, run npx @work-rjkashyap/unified-ui init first. See the CLI docs for details.

Or install the full package

Use this approach if you prefer to install the entire design system as a dependency instead of copying individual components.

npm install @work-rjkashyap/unified-ui
pnpm add @work-rjkashyap/unified-ui
yarn add @work-rjkashyap/unified-ui
bun add @work-rjkashyap/unified-ui

Anatomy

import { Switch } from "@work-rjkashyap/unified-ui";

Examples

Basic Usage

Sizes

Label Position

With Description

Disabled State

Settings Pattern


Controlled

Use checked and onCheckedChange to control the switch state externally.

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

<Switch label="Dark mode" checked={enabled} onCheckedChange={setEnabled} />;

Without Label

When used without a label, provide an aria-label for accessibility.

<Switch aria-label="Toggle dark mode" />

Props

PropTypeDefaultDescription
size"sm" | "md""md"Size of the switch.
labelReactNodeLabel text displayed next to the switch.
labelPosition"left" | "right""right"Position of the label relative to the track.
descriptionReactNodeDescription text below the label.
checkedbooleanControlled checked state.
defaultCheckedbooleanDefault checked state (uncontrolled).
onCheckedChange(checked: boolean) => voidCallback when the checked state changes.
disabledbooleanfalseDisables the switch.
requiredbooleanfalseWhether the switch is required in a form.
namestringForm field name for native form submission.
valuestringValue submitted with the form.
classNamestringAdditional CSS classes.

Accessibility

  • Built on Radix UI Switch primitive for full keyboard and screen reader support.
  • Toggle with Space or Enter keys.
  • aria-checked state is managed automatically by Radix.
  • When no visible label is provided, you must supply an aria-label.
  • The label is clickable — clicking it toggles the switch.
  • Description text is linked to the switch via aria-describedby.
  • Focus ring is visible on keyboard navigation only (focus-visible).
  • Disabled state sets both the disabled attribute and reduces opacity.
  • Spring animation on the thumb respects prefers-reduced-motion via useReducedMotion.

On this page