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 switchpnpm dlx @work-rjkashyap/unified-ui add switchnpx @work-rjkashyap/unified-ui add switchbunx @work-rjkashyap/unified-ui add switchIf 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-uipnpm add @work-rjkashyap/unified-uiyarn add @work-rjkashyap/unified-uibun add @work-rjkashyap/unified-uiAnatomy
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
| Prop | Type | Default | Description |
|---|---|---|---|
size | "sm" | "md" | "md" | Size of the switch. |
label | ReactNode | — | Label text displayed next to the switch. |
labelPosition | "left" | "right" | "right" | Position of the label relative to the track. |
description | ReactNode | — | Description text below the label. |
checked | boolean | — | Controlled checked state. |
defaultChecked | boolean | — | Default checked state (uncontrolled). |
onCheckedChange | (checked: boolean) => void | — | Callback when the checked state changes. |
disabled | boolean | false | Disables the switch. |
required | boolean | false | Whether the switch is required in a form. |
name | string | — | Form field name for native form submission. |
value | string | — | Value submitted with the form. |
className | string | — | Additional CSS classes. |
Accessibility
- Built on Radix UI Switch primitive for full keyboard and screen reader support.
- Toggle with Space or Enter keys.
aria-checkedstate 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
disabledattribute and reduces opacity. - Spring animation on the thumb respects
prefers-reduced-motionviauseReducedMotion.