All components

Switch

Forms

An accessible on/off toggle built on Radix Switch — keyboard operable, with a smooth animated thumb.

Overview

An on/off toggle built on Radix Switch — the right control for an immediate, binary setting that applies without a separate "save" step (think dark mode or notifications).

Controlled vs uncontrolled

Use defaultChecked for simple, uncontrolled usage. For controlled state, pair checked with onCheckedChange:

<Switch checked={on} onCheckedChange={setOn} />

Labeling

A switch needs an accessible name. The simplest approach is to wrap it in a <label> alongside its text, so clicking the label toggles the control.

Accessibility

Radix renders the correct role="switch" and aria-checked state, and the control is fully keyboard operable (Space / Enter) with a visible focus ring.

Installation

Terminal
npx shadcn@latest add https://ui.saumyarex.xyz/r/switch.json

Props

PropTypeDefaultDescription
checked / defaultCheckedbooleanControlled / uncontrolled checked state (Radix Switch).
onCheckedChange(checked: boolean) => voidFires when the state changes.
disabledbooleanfalsePrevents interaction.

Dependencies

@radix-ui/react-switchclsxtailwind-merge