本文へ移動
日本語

仕組み

The four states (idle, loading, success, error) are stacked in one grid cell, so the button is as wide as the widest layer and switching only changes opacity and transform; the form never shifts. A ref blocks double clicks, loading lasts at least minLoading, and success keeps blocking clicks while error allows a retry. Timers are cleared on unmount.

向いている場面

  • Submit, pay or save actions that wait for a server and must report the result right there.
  • Full-width primary buttons at the bottom of mobile screens (with block).
  • When one button should prevent double submits and show the outcome without a toast.

向かない場面

  • Tasks longer than about 10 seconds or with real progress: use a progress bar or task list.
  • Long errors the user must read or act on: use inline form errors or a dialog.
  • Small actions repeated on every list row: use lightweight inline feedback.

使い方

import { StateButton } from './StateButton.jsx'

export function SubmitOrder({ orderId }) {
  return (
    <StateButton
      block
      onAction={() => api.post('/orders/submit', { orderId })}
      loadingText="Sending"
      successText="Order placed"
      errorText={(err) => (err instanceof Error ? err.message : 'Submit failed, please try again')}
    >
      Place order
    </StateButton>
  )
}

プロパティ

プロパティ型既定値説明
onAction() => Promise<unknown>—Async action run on click. Resolving shows success; rejecting or throwing shows failure.
childrenReactNode—Label of the default state.
loadingTextReactNode'Processing…'Text beside the spinner. An empty string shows only the spinner (screen readers still announce “Processing”).
successTextReactNode'Done'Text of the success state.
errorTextReactNode | ((error) => ReactNode)'Failed, please try again'Text of the failure state. A function receives the error and is called only after a real failure; the latest text is kept afterwards.
resetAfternumber2000How long success or failure stays before resetting, in ms. 0 disables the reset.
minLoadingnumber400Minimum time the loading state shows, in ms, so the spinner never just flashes.
blockbooleanfalseFills the width of the parent.
variant'accent' | 'ink''accent'Fill at rest: accent colour, or ink (text colour, inverts in the dark theme). Success and error always switch to green and red.
onSuccess(result) => void—Called on success with the value onAction resolved.
onError(error) => void—Called on failure with the error onAction rejected.
...restButtonHTMLAttributes—Other attributes go to the <button>, such as disabled and onClick (call e.preventDefault() to skip a run).

アクセシビリティ

  • aria-busy is set while loading, and clicks are blocked with aria-disabled rather than disabled, so keyboard focus is kept.
  • A role="status" live region outside the button announces the loading, success and failure text.
  • Inactive state layers are aria-hidden, so the button's name is always the current text.
  • Height is 44px for touch, with a visible focus ring.
  • Reduced motion removes movement, shake and check drawing, keeping only fades and a slower spinner.

モーション

Content enters in 240ms ease-out and leaves in 168ms ease-in; background 240ms; check drawing 360ms; failure shake 420ms; press scale 0.98 in 100ms; spinner 800ms linear. While loading, a soft band of light sweeps across the button every 1.4s (off with reduced motion).

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

StateButton.jsxstate-button.csstokens.cssREADME.md

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

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