Cómo funciona
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.
Úsalo para
- Media controls where one button alternates between two actions.
- Menu toggles, theme switches and any two-state icon control.
Busca otra opción cuando
- Settings that need a visible label: use a switch.
- More than two states: use a toggle group or a menu.
Uso
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" />
</>
)
}Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
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. |
Accesibilidad
- 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.
Movimiento
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.
Añádelo a tu proyecto
Los miembros Pro copian un prompt y su agente de programación instala el código, igual que con los gratuitos.
SwapIconButton.jsxswap-icon-button.csstokens.cssREADME.md
Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.



