Animated Switch

Use Animated Switch for a binary setting that changes immediately. It uses Base UI switch behavior with a responsive thumb press, an interruptible state transition, and optional thumb drag interaction.

Preview

Reduce motion:Off
Tune:Off
Inspect

Installation

Basic usage

TypeScript
notifications-switch.tsx

Examples

Controlled state

Use checked and onCheckedChange when the switch reflects application state.

TypeScript
controlled-switch.tsx

Size, availability, and errors

Choose sm, md, or lg for the control size. Use disabled only when the setting cannot currently be changed.

Use error when a setting cannot be changed because it needs attention. Both disabled and error switches give a brief nudge on pointer interaction. Error remains focusable and exposes aria-invalid so its state is available to assistive technology.

Drag interaction

The thumb can be dragged between states, while tapping the track continues to toggle immediately. Set enableDrag={false} when the switch appears inside another horizontal drag interaction.

Usage

API

PropertyTypeDefaultDescription
checkedboolean—Controlled checked state.
defaultCheckedbooleanfalseInitial checked state for uncontrolled usage.
onCheckedChange(checked: boolean) => void—Callback fired when the checked state changes.
disabledbooleanfalseWhether the switch is disabled.
errorbooleanfalse The switch remains focusable so assistive technology can announce its invalid state.Whether the switch is in an error state and cannot be changed.
size"sm" | "md" | "lg""md"The size of the switch.
enableDragbooleantrueWhether the thumb can be dragged between states.
classNamestring—Additional classes for the switch track.

Accessibility

Provide an accessible name with a visible label or aria-label. The switch supports keyboard interaction, exposes its checked state to assistive technology, and keeps the same tap and keyboard behavior when drag interaction is disabled. Error switches expose aria-invalid and aria-disabled; disabled switches use the native disabled state.

Styling

Use the size and class name options to fit the control into a settings row or compact toolbar. Reduced-motion preferences remove the press deformation, state transition, and blocked-state nudge while preserving static disabled and error cues.

Contact

Found a bug or need help using this component? Open an issue on GitHub.

License & Usage

Sona UI is available under the MIT license. You may use, modify, and distribute this component in personal and commercial projects.

Was this page helpful?

Your response helps us improve component docs.

Related components