Use a switch for a setting that takes effect immediately: notifications, privacy, a theme. For
choices that are only applied when a form is submitted, use a checkbox instead.
The switch follows the MotionKit motion grammar in three beats. Pressing charges the thumb: it
stretches toward the side it will travel to. Releasing throws it across on a spring (the
action). When it arrives it settles: instead of overshooting the track it squashes against
the far wall and bounces back to round. How much of each beat you see depends on the temperament:
calm has no bounce and barely stretches, playful stretches and wobbles.
Requires the @motionkit namespace in components.json. See Installation.
It installs to components/ui/switch.tsx, the same path as the shadcn switch, and keeps its API
(checked, defaultChecked, onCheckedChange, disabled, name, value, required), so it is
a drop-in replacement.
Built on Radix Switch: a native <button role="switch"> with aria-checked, so screen readers
announce it as a switch that is on or off. It submits with forms when you pass name.
Give it an accessible name: a <label htmlFor> pointing at its id, or aria-label.
Keyboard: Space and Enter toggle it (the thumb charges while the key is held, like a
press), Tab moves focus. Focus shows a 3px ring in the theme's ring color.
Hover and keyboard focus grow a soft halo behind the thumb, so feedback does not rely on a
color change alone.
Target size: md and lg are at least 24px tall. sm draws a 20px track but keeps a 24px tall
hit area, so it meets WCAG 2.5.8 on its own, without relying on spacing.
Icons inside the thumb are decorative (aria-hidden); the state is conveyed by aria-checked.
Colors come from your theme: the track is input when off and primary when on, the thumb is
background, so contrast follows your shadcn theme in light and dark.
Only transform animates (thumb position, stretch and squash) plus the track color. One motion
value drives the position; the wall squash is derived from it, so React never re-renders while
the thumb moves.
With reduced motion (the OS setting, or MotionKitProvider reducedMotion="always") the thumb
moves to the other side in a short 200ms ease with no stretch, squash or bounce, and the icons
cross-fade.
Technical sheet
Motion
calm
260 ms · bounce 0
lively
400 ms · bounce 0.3
playful
540 ms · bounce 0.45
Main spring of each temperament. Set it with the temperament prop or MotionKitProvider; the default is lively.
Size
Not measured yet · budget 6.0 kB gzip
Component and MotionKit helpers; excludes react, react-dom, @radix-ui/react-switch, motion.
Dependencies
@radix-ui/react-switch · peer
motion · peer
Triggers
toggle, press
Reduced motion
The thumb glides to the other side in a short fade-length move with no stretch, squash or bounce.
Keyboard
Space
Toggles the switch (the thumb charges while the key is down)
Enter
Toggles the switch
Tab
Moves focus to and from the switch
Browsers
Last 2 versions of Chrome, Edge, Firefox and Safari (iOS 16.4+)