Keyframery

Toggle

The pressed surface pops in, and in a group one hover highlight glides between toggles.

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/toggle

This adds components/keyframery/toggle.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 { Toggle } from "@/components/keyframery/toggle"

export default function Usage() {
  return <Toggle aria-label="Bold">B</Toggle>
}

Examples

On and off

A button that stays pressed. ToggleIcon animates the icon as it turns on.

Group

ToggleGroup with multiple for formatting. Without it, one at a time, like alignment.

Motion

MoveWhat you see
PopThe pressed surface pops in and shrinks away on release.
GlideIn a group, one hover highlight glides between toggles.
PressToggles press in.

Timing and springiness follow the motion style you set. To change how fast this toggle moves, pass speed:

<Toggle speed="fast">
  …
</Toggle>

"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
--toggle-trackGroup well and standalone pressed fill--muted
--toggle-keyPressed key in a group--background; --accent in dark
--toggle-inkToggle text--muted-foreground; --foreground when hovered or pressed
--toggle-radiusToggle corners8px small; 9px medium
--toggle-group-radiusGroup corners12px
--toggle-gapSpace between grouped toggles0.125rem
--toggle-heightToggle height and icon width1.75rem small; 2rem medium

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 toggle reference.

Toggle

Renders a <button>, and takes its props too.

Prop

Type

ToggleGroup

Renders a <div>, and takes its props too.

Prop

Type

ToggleIcon

Renders a <span>, and takes its props too.

Prop

Type

Keyboard

KeyWhat it does
SpaceTurns it on or off.
EnterTurns it on or off.
← →Moves between toggles in a group.

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 toggle with your own styles? Keep your markup and change one line. The primitive adds motion and nothing else.

// Before
import { … } from "@ui-sdk/react/toggle"

// After: same parts, same props, now animated
import { … } from "@/components/keyframery/primitives/toggle"

Questions

How does the animated Toggle for UI SDK move?

Pop: The pressed surface pops in and shrinks away on release. Glide: In a group, one hover highlight glides between toggles. Press: Toggles press in.

Do I need Motion or Framer Motion for the animated Toggle?

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/toggle instead. It has the same parts and props as @ui-sdk/react/toggle and adds motion only.

What happens to the Toggle when someone turns on reduced motion?

Nothing moves. State changes happen at once, or with a short fade.

All components

On this page