Signature
Action button
Save → Saved, with a label that morphs.
Save draft
A button that tells you what is happening to it. Click one: the label morphs from the action to its progress to its result, and the width springs to fit each word.
When to use
- A single async action whose result the user wants confirmed in place: Save draft, Send invoice, Sync.
- Anywhere a toast would be too far from the thing that was clicked.
- For long or cancellable work use a progress bar; for a plain spinner on a button use
Button loading.
Errors and retry
A rejected promise shows the error state and returns to idle after 1.6 seconds, so the next click is a retry. The first click below fails on purpose.
Controlled state
Pass state and onStateChange to drive the button from your own mutation (for example a React Query status). Success and error still ask for idle after resetAfter milliseconds through onStateChange.
API
ActionButton
| Prop | Type | Default | Description |
|---|---|---|---|
| onAction | () => unknown | Promise<unknown> | — | Runs on click. A promise drives pending → success (resolved) or error (rejected). |
| state | 'idle' | 'pending' | 'success' | 'error' | — | Controlled state. Omit to let onAction drive it. |
| onStateChange | (state) => void | — | Called on every change, controlled or not. |
| pendingLabel | ReactNode | 'Working…' | Label while pending. |
| successLabel | ReactNode | 'Done' | Label after success. |
| errorLabel | ReactNode | 'Failed' | Label after a failure. |
| resetAfter | number | 1600 | Milliseconds before success / error return to idle. 0 keeps the result. |
| idleIcon | ReactNode | — | Icon before the idle label. |
| variant, size | ButtonProps | — | Same as Button: 28 / 32 / 36 / 44px and every variant. |
| ...props | ButtonProps | — | Forwarded to Button, including ref, disabled and onClick (runs before onAction). |
Keyboard
| Key | Action |
|---|---|
| Enter | Runs the action. |
| Space | Runs the action. |
Accessibility
- A native button that keeps focus through every state — it is never disabled mid-action.
- While pending it sets aria-busy and ignores clicks.
- A polite live region announces the pending, success and error labels, so the morph is not only visual.
- Success and error colours (success / destructive fill with their foreground) keep 4.5:1 contrast, and the icon and label carry the meaning too.
Motion
- Width: measured before and after, then animated with the Web Animations API on
--ui-spring-morph(550ms, slight settle). Interrupting mid-morph starts from the width on screen. - Labels: the new label rises 4px with a 2px blur that clears (220ms, ease-out); the old one lifts 4px and blurs out (160ms, ease-in) over the top of it.
- Icons: the spinner is a 14px ring; the check draws itself in 320ms; the cross pops in. The fill turns success or destructive over 150ms.
- Reduced motion: no width spring, no blur or movement. The label swaps with a 1ms fade, the colour still changes and the live region still announces.