Skip to main content

switch

A control that allows the user to toggle between checked and not checked.

Philosophy

Switches represent immediate, binary state changes - unlike checkboxes, which defer action to a form submission. This semantic distinction matters for UX: a switch should take effect instantly. We support custom SVG indicators so the on/off metaphor can match your design language without sacrificing the accessible role="switch" foundation.

How It's Built

Loading diagram...

Installation


npx @gentleduck/cli add switch

npx @gentleduck/cli add switch

Usage

import { Switch } from "@/components/ui/switch"
import { Switch } from "@/components/ui/switch"
<Switch />
<Switch />

Examples

Form

RTL Support

Direction is resolved through the shared primitives direction module. Use a local dir="rtl" override when the component exposes it, or set DirectionProvider at app/root level for global RTL/LTR behavior.

Motion

Use MotionSwitch for a physical-feeling toggle powered by motion. The thumb slides with a bouncy spring, stretches in the direction of travel when pressed, and the track color crossfades between border and primary.

API Reference

Switch

PropTypeDefaultDescription
dir'ltr' | 'rtl'-Text direction override. Resolved via useDirection (dir prop -> DirectionProvider -> 'ltr').
classNamestring-Additional class names for the checkbox input
indicatorReact.ReactElement-Custom element for the unchecked state indicator
checkedIndicatorReact.ReactElement-Custom element for the checked state indicator
onChange(event: ChangeEvent) => void-Native checkbox change event handler
onCheckedChange(checked: boolean) => void-Callback with the new checked boolean when toggled
refReact.Ref<HTMLInputElement>-Ref forwarded to the native checkbox input
styleReact.CSSProperties-Inline styles applied to the input element
...propsReact.HTMLProps<HTMLInputElement>-Additional props to spread to the input element

MotionSwitch

Renders its own motion-controlled track and thumb. The thumb slides with a bouncy spring, stretches in the direction of travel while pressed, and the track color crossfades between border and primary. Requires the motion package. Does not support the indicator / checkedIndicator props — use the base Switch for custom SVG indicators.

PropTypeDefaultDescription
dir'ltr' | 'rtl'-Text direction override. In RTL the thumb starts on the right and the stretch origin flips.
checkedboolean-Controlled checked state
defaultCheckedbooleanfalseUncontrolled initial checked state
disabledbooleanfalseDisables interaction and lowers opacity
onChange(event: ChangeEvent) => void-Native checkbox change event handler
onCheckedChange(checked: boolean) => void-Callback with the new checked boolean when toggled
classNamestring-Additional class names for the outer track label
styleReact.CSSProperties-Inline styles applied to the outer track label
...propsReact.HTMLProps<HTMLInputElement>-Additional props spread onto the hidden input element

See also

  • Checkbox - Multi-selection toggle control
  • Radio Group - Single selection from a set of options