How it works
Each preset is drawn so that its two states share parts. Play and pause are two four-point outlines, so where CSS path interpolation exists the triangle splits and squares off into two bars. The menu's outer bars slide to the centre and turn into a cross while the middle bar shrinks away. The sun's disc grows while a mask disc slides in to carve the crescent, and the rays shrink and turn away. Everything moves on one 420ms spring, so the swap feels physical rather than like two images trading places.
Use it for
- Media controls where one button alternates between two actions.
- Menu toggles, theme switches and any two-state icon control.
Look elsewhere when
- Settings that need a visible label: use a switch.
- More than two states: use a toggle group or a menu.
Usage
import { useState } from 'react'
import { SwapIconButton } from './SwapIconButton.jsx'
export function PlayerControls() {
const [playing, setPlaying] = useState(false)
return (
<>
<SwapIconButton preset="play" variant="solid" size="lg" pressed={playing} onPressedChange={setPlaying} />
<SwapIconButton preset="menu" variant="ghost" labels={{ off: 'Show queue', on: 'Hide queue' }} />
{/* A constant name reports aria-pressed instead of swapping the name */}
<SwapIconButton preset="theme" label="Dark mode" />
</>
)
}Props
| Prop | Type | Default | What it does |
|---|---|---|---|
preset | 'play' | 'menu' | 'theme' | 'play' | Built-in pair: play/pause, menu/close or sun/moon. |
icons | [ReactNode, ReactNode] | — | Any two icons (off, on) instead of a preset; they swap with a turning crossfade. |
pressed | boolean | — | Controlled state. |
defaultPressed | boolean | false | Initial state when uncontrolled. |
onPressedChange | (pressed: boolean) => void | — | Called with the next state. |
labels | { off: string, on: string } | — | Accessible name in each state, describing the action it will take (Play / Pause). English defaults per preset. |
label | string | — | A constant name instead; the button then reports aria-pressed. |
variant | 'soft' | 'solid' | 'accent' | 'ghost' | 'soft' | Surface style. |
size | 'sm' | 'md' | 'lg' | 'md' | 36, 44 or 56px circle. |
Accessibility
- A native button. By default its aria-label names the action it will take and swaps with the state, with no aria-pressed, so it never says “Pause, pressed”.
- With a constant label it reports aria-pressed instead; both follow the toggle-button guidance.
- The name also appears as a title tooltip; the glyph is hidden from screen readers.
- Focus-visible shows a 2px ring outside a surface-colour gap; sizes are 36, 44 and 56px.
- Reduced motion replaces the morph with a short fade; nothing turns, scales or slides.
Motion
All parts move on a 420ms spring with a slight overshoot; fading parts take half that. The sun/moon glyph makes one full turn as it changes, settling upright. Pressing scales the button to 90% for 100ms and springs back.
Add it to your project
Pro members copy a prompt and their coding agent installs the source, the same way as the free components.
SwapIconButton.jsxswap-icon-button.csstokens.cssREADME.md
Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.



