Keyframery

Switch

The thumb stretches toward the side it is moving to, then settles.

See it as an AI agent does.

Resize preview. Focus the handle and use the left or right arrow keys. Hold Shift for larger steps.— px
Customize1 controls
Animation speed1×

Installation

npx shadcn@latest add @keyframery/switch

This 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

MoveWhat you see
StretchThe thumb stretches toward the side it is moving to, then settles.
PressIt 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.

VariableWhat it changesDefault
--switch-offUnchecked track--muted; --accent in dark
--switch-onChecked track--primary
--switch-thumbThumb surface--background; foreground mixed with 8% background in dark; --primary-foreground when checked in dark
--switch-widthTrack width38px
--switch-heightTrack height22px
--switch-thumb-sizeResting thumb width18px
--switch-radiusTrack and thumb corners9999px
--switch-padThumb inset2px

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

KeyWhat it does
SpaceTurns it on or off.
EnterTurns 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.

All components

On this page