Ir al contenido
Español

Cómo funciona

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.

Úsalo para

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

Busca otra opción cuando

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

Uso

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

Propiedades

PropiedadTipoPor defectoQué hace
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).

Accesibilidad

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

Movimiento

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

Añádelo a tu proyecto

Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.

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

Botones
Botón de borde aurora viene con Pro. La vista previa aparece al desbloquearlo.
Botón con mordiscos viene con Pro. La vista previa aparece al desbloquearlo.