Components

Switch

An animated React toggle switch built on shadcn/ui and Radix: the drop-in Switch has a smoothly sliding thumb and three sizes — plus Motion blocks for a sun-to-moon theme toggle and an async switch that shows a spinner while saving and springs back if the request fails.

  • Drop-in shadcn API
  • Radix Switch
  • CSS transitions
  • Optimistic UI
  • prefers-reduced-motion

Installation

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

Replaces components/ui/switch.tsx with the same API. The thumb slides with a short CSS ease-out, so the base Switch runs no JavaScript per frame.

Usage

airplane-mode.tsx
import { Switch } from "@/components/ui/switch"

export function AirplaneMode() {
  return (
    <div className="flex items-center gap-3">
      <Switch id="airplane-mode" />
      <label htmlFor="airplane-mode">Airplane mode</label>
    </div>
  )
}

Examples

Default

Click or press Space: the thumb slides across and the track turns mint.

Sizes

size is sm, default or lg. Disabled switches dim and show a not-allowed cursor.

Settings list

Pair each switch with a <label> and link its hint with aria-describedby.

Email me when a production deploy finishes.

Notify me when someone @mentions me.

A Monday summary of activity.

Blocks

Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.

Icon Switch

A theme toggle whose thumb morphs from a sun into a crescent moon: the rays spin and retract, the core swells and an SVG mask bites the crescent out. Pass checkedIcon and uncheckedIcon to spin between any two icons instead. The morph is also exported as SunMoonIcon.

$ npx shadcn@latest add https://duskui.com/r/icon-switch.json
PropTypeDefaultDescription
checked / onCheckedChangeboolean / (checked: boolean) => void—Controlled state.
defaultCheckedbooleanfalseUncontrolled initial state.
checkedIcon / uncheckedIconReactNode—Custom icons. Omit both for the sun / moon morph.
size"sm" | "default" | "lg""lg"Switch size; lg leaves room for the icon.
thumbClassNamestring—Classes for the thumb, e.g. icon colors.

Loading Switch

For settings that save to a server. Return a promise from onCheckedChange: the switch moves optimistically, shows a spinner inside the thumb and ignores clicks until it settles. If the promise rejects it springs back with a short shake and a red ring. The second row always fails.

$ npx shadcn@latest add https://duskui.com/r/loading-switch.json
PropTypeDefaultDescription
onCheckedChange(checked: boolean) => void | Promise<unknown>—Return a promise to show the spinner; reject to revert.
checked / defaultCheckedboolean—Controlled or initial state. When controlled, update checked inside the handler.
onError(error: unknown) => void—Called with the rejection reason.
loadingbooleanfalseForce the busy state, e.g. while initial data loads.
loadingLabel / errorLabelstring"Saving" / "Couldn't save…"Announced to screen readers.

API Reference

Switch forwards every Radix Switch prop (checked, defaultChecked, onCheckedChange, disabled, required, name, value) plus the additions below.

PropTypeDefaultDescription
size"sm" | "default" | "lg""default"Track and thumb size.
thumbContentReactNode—Rendered inside the thumb (an icon or spinner).
thumbClassNamestring—Classes for the thumb.
classNamestring—Classes for the track. Keep width = 2 × thumb + 4px so the thumb travels exactly.

Accessibility

  • Built on Radix Switch: a button with role="switch" and aria-checked, toggled with Space or Enter, and submitted with forms through a hidden input.
  • Give every switch a visible <label htmlFor> or an aria-label.
  • Loading Switch sets aria-busy, stays focusable while saving and announces progress and failures in a polite live region.
  • Under prefers-reduced-motion the thumb moves instantly, the error shake is skipped and the spinner slows down.