本文へ移動
日本語
「変形アイコンボタン」は Pro に含まれます。解除するとプレビューが表示されます。

仕組み

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.

向いている場面

  • Media controls where one button alternates between two actions.
  • Menu toggles, theme switches and any two-state icon control.

向かない場面

  • Settings that need a visible label: use a switch.
  • More than two states: use a toggle group or a menu.

使い方

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" />
    </>
  )
}

プロパティ

プロパティ型既定値説明
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.
pressedboolean—Controlled state.
defaultPressedbooleanfalseInitial 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.
labelstring—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.

アクセシビリティ

  • 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.

モーション

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.

プロジェクトに追加

Pro メンバーはプロンプトをコピーし、コーディングエージェントが無料コンポーネントと同じ手順でソースを導入します。

SwapIconButton.jsxswap-icon-button.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

ボタン
「オーロラ縁ボタン」は Pro に含まれます。解除するとプレビューが表示されます。
「かじりボタン」は Pro に含まれます。解除するとプレビューが表示されます。