Skip to content

Animated Toggle Switch for React

An animated React toggle switch, an iOS switch in Tailwind, whose thumb springs across a glass track with Motion and fills with the accent color when on.

npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/switch.json

Studio lighting

With label and description

Files to copy

5 folders · 10 files · 4 packages
From theme/src/
  • components/switch/
  • hooks/{use-direction.ts, use-latest.ts, use-media-query.ts, use-preferences.ts}
  • motion/tokens.ts
  • lib/{cn.ts, field-control-shell.tsx, form.ts, motion-props.ts, state-light.tsx}
  • styles/
shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/switch.jsonRegistry item: switch. The styles need the Tailwind v4 glass UI setup.
Packages
pnpm add @base-ui/react clsx motion tailwind-merge

A settings list

One piece of state for a group of switches, each with its own name.

Props

PropTypeDefaultDescription
classNamestringNoneClasses for the outer element: the <label> (or the wrapper holding an error); the control itself when there is neither.
controlClassNamestring | ((state: SwitchRootState) => string | undefined)NoneClasses for the switch track itself. Base UI's state function form works here.
labelReactNodeNoneText next to the switch. Clicking it toggles the switch.
descriptionReactNodeNoneMuted helper text under the label. Only shown together with label.
errorReactNodeNoneError message under the control. Marks it invalid (aria-invalid, danger border) and is announced; linked with aria-describedby.
stateLightbooleanfalseA calm accent light round the track while it is on (data-state="on", an aura on its own layer). The checked fill already says "on": this is the glow only. Off with reduced transparency and forced colours.
checked / defaultCheckedbooleanfalseControlled or uncontrolled state.
onCheckedChange(checked) => voidNoneCalled with the new checked state when the control is toggled.
disabledbooleanfalseMakes the control non-interactive.

Keyboard

KeysAction
Tab/Shift + TabMove focus to or from the switch.
Space/EnterToggle it.

Accessibility

  • role="switch" with aria-checked; the label and description share one <label>.
  • On and off differ by thumb position as well as color; forced colors outline the track.
  • The touch hit area is 44 px; under reduced motion the thumb jumps instead of sliding.

When to use Switch

  • Use Switch as an animated switch in React for settings that apply the moment they change, such as dark mode, push notifications or a public profile toggle in an account page. Use it as a Framer Motion switch: the thumb moves with Motion, the library formerly called Framer Motion, on the shared SheenUI spring.
  • Use label and description together for a preferences row, so the text and the helper line sit in one label element and clicking the words flips the switch.
  • Use the error prop when a toggle can fail to save, for example a server rejection, so the message shows under the control and is announced to screen readers.
  • Skip it when the choice is only applied after a form is submitted. Use Checkbox for that, and use RadioGroup or SegmentedControl when the choice is between named options.
  • Pair it with GlassCard for a settings panel, Toast to confirm that a change was saved, and Field or the settings-page block for grouped preference rows.

Frequently asked questions

Guides that cover it

67 components pages in all: browse them on All components.

By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .