Skip to content
English

How it works

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.

Use it for

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

Look elsewhere when

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

Usage

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

Props

PropTypeDefaultWhat it does
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).

Accessibility

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

Motion

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

Add it to your project

Copy the prompt into Codex or Claude Code. The agent reads the published source, shows you what it will write and adds the component next to your own.

StateButton.jsxstate-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.

Buttons
Aurora rim button comes with Pro. Its preview shows once you unlock it.
Bite button comes with Pro. Its preview shows once you unlock it.