Switch

Free

A glass switch for a setting that takes effect at once: the capsule knob springs across, turns into a clear lens while held, and puts itself back if the save fails.

npx shadcn@latest add https://pixilui.com/r/switch.json

Preview

The knob springs across and clears into a lens while held.
Save

When to use

Use it when

  • A setting that applies the moment it flips: alerts, retries, sharing.
  • A settings list, label and second line on the left, the switch on the right (SwitchRow).

Use something else when

  • A choice saved later with a form's submit: use Checkbox.
  • One choice among several: use Radio group or a segmented control.

Installation

Terminal
npx shadcn@latest add https://pixilui.com/r/switch.json

Adds switch.tsx, and installs clsx and tailwind-merge. The URL works in any app with a components.json, with no registry to add first.

Usage

example.tsx
import { Switch } from "@/components/pixilui/switch";

<Switch onCheckedChange={onCheckedChange}>Save changes</Switch>

44 by 20 at every width; a row's words wrap and the switch stays at the end.

API reference

PropTypeDefault
namestring–A form field name: a hidden input carries value while the switch is on.
defaultCheckedbooleanfalseUncontrolled: the state on the first render.
checkedboolean–Controlled: on or off.
onCheckedChange(checked: boolean) => void | Promise<void>–Runs with the new state on a click, Space or Enter. Return a promise to save first: the switch shows the new state, is aria-busy until it settles, and goes back if it rejects.
valuestring"on"The value sent with the form while on.

Also takes every native <button> attribute except onChange, value, defaultValue, role, and a ref.

Keyboard

TabMoves to each switch in turn.
Space EnterFlips the focused switch.

Accessibility

  • A button with role="switch" and aria-checked.
  • In a SwitchRow the label names it and the second line describes it (aria-describedby); alone, give it an aria-label.
  • The row is a label, so a click on the words flips it once.
  • While a returned promise is pending the switch is aria-busy and keeps its focus.

Motion

  • The knob crosses in 300ms with a small overshoot; the candy glass cross-fades in over 200ms with its edge and glow.
  • Held, the knob widens 4px toward where it will go and turns into a clear lens in 150ms.
  • Hover on an off switch shows a 22% wash of the color.
  • Reduced motion: instant.

PixilUI Pro opens soon. The first 20 licenses are $99.