A two-state button that can be either on or off.
Installation
Install the packages
Add the shadcn/ui component
Animate it
The CLI finds your shadcn/ui components through components.json, swaps their primitive imports for Animate UI's and removes the CSS animation classes Motion replaces.
Usage
Import the primitive from Animate UI instead of Base UI. Everything else stays the same.
import { Toggle } from '@animate-ui/react/primitives/base-ui/toggle';
export function BoldToggle() {
return (
<Toggle aria-label="Toggle bold">
<BoldIcon />
</Toggle>
);
}Using the @base-ui/react umbrella import instead? The barrel mirrors it: import { Toggle } from '@animate-ui/react/primitives/base-ui'.
Toggle.Props and Toggle.State are kept. The same Toggle is the item of a Toggle Group.
Animation
The motion of Animate UI v1's toggle:
- Toggle scales to 0.97 while pressed, on Motion's default spring for scale (stiffness 550, damping 30). No hover scale.
- The press scale runs on the compositor (a
transform), so shadcn/ui'stransition-alldoesn't tween its frames. - The on/off colours are the component's CSS: shadcn/ui colours the toggle itself (
data-[state=on]:bg-muted) and fades it withtransition-all. v1's styled toggle faded a separate highlight in and out instead (0.3s). - Toggling with Space changes the state at once.
- Under reduced motion, there is no press scale.
API Reference
Toggle takes exactly the props of Base UI's Toggle, plus:
| Prop | Type | Default |
|---|---|---|
transition? | Transition | { type: 'spring', stiffness: 550, damping: 30, restSpeed: 10 } |
A render element (or function) of your own gets Base UI's behaviour, without the animation. AnimateUIProvider's tempo scales these transitions with everything else.