Switch
The thumb stretches toward the side it is moving to, then settles.
See it as an AI agent does.See this component the way an AI agent does.
Installation
npx shadcn@latest add @keyframery/switchThis adds components/keyframery/switch.tsx and its primitive to your project, and installs UI SDK. The motion tokens are added to your global CSS the first time you install a component.
Usage
import { Switch } from "@/components/keyframery/switch"
export default function Usage() {
return (
<label className="flex items-center gap-2">
<Switch />
Airplane mode
</label>
)
}Examples
Settings list
A switch takes effect at once. For a choice that's saved with a form, a checkbox is clearer.
Controlled
Pass checked and onCheckedChange to drive something else with it.
Motion
| Move | What you see |
|---|---|
| Stretch | The thumb stretches toward the side it is moving to, then settles. |
| Press | It widens while you hold it. |
Timing and springiness follow the motion style you set. To change how fast this switch moves, pass speed:
<Switch speed="fast">
…
</Switch>"fast" is 0.6×, "slow" 1.4×, and a number sets the multiplier. With reduced motion turned on, nothing moves: changes happen at once or with a short fade.
Theming
Set these CSS variables on the component or a parent. Leave them unset to follow the light and dark theme. The open Customize panel below the preview lets you try these settings.
| Variable | What it changes | Default |
|---|---|---|
--switch-off | Unchecked track | --muted; --accent in dark |
--switch-on | Checked track | --primary |
--switch-thumb | Thumb surface | --background; foreground mixed with 8% background in dark; --primary-foreground when checked in dark |
--switch-width | Track width | 38px |
--switch-height | Track height | 22px |
--switch-thumb-size | Resting thumb width | 18px |
--switch-radius | Track and thumb corners | 9999px |
--switch-pad | Thumb inset | 2px |
API reference
Every part takes className and children. Parts from UI SDK also take render, to draw your own element, and a function of their state for className and style. Props not listed here are in the UI SDK switch reference.
Switch
Renders a <button>, and takes its props too.
Prop
Type
Other parts
These take the props of the element they render, and nothing else:
SwitchThumb: a<span>
Keyboard
| Key | What it does |
|---|---|
| Space | Turns it on or off. |
| Enter | Turns it on or off. |
Accessibility
Keyboard, focus and screen-reader behaviour come from UI SDK and are not changed. More in Accessibility.
Swap the import
Already using UI SDK's switch with your own styles? Keep your markup and change one line. The primitive adds motion and nothing else.
// Before
import { … } from "@ui-sdk/react/switch"
// After: same parts, same props, now animated
import { … } from "@/components/keyframery/primitives/switch"Questions
How does the animated Switch for UI SDK move?
Stretch: The thumb stretches toward the side it is moving to, then settles. Press: It widens while you hold it.
Do I need Motion or Framer Motion for the animated Switch?
No. Every move is plain CSS, so there is nothing to install besides the component and UI SDK.
Can I keep my own styling and only take the motion?
Yes. Import from @/components/keyframery/primitives/switch instead. It has the same parts and props as @ui-sdk/react/switch and adds motion only.
What happens to the Switch when someone turns on reduced motion?
Nothing moves. State changes happen at once, or with a short fade.