# Status button

> Save, Saving, Saved: the spinner opens in, the check draws itself, errors shake, and the label morphs between them.

The most common button in any product, and usually the least considered: it greys out, maybe shows a spinner, and snaps back. Here every change is one motion. A spinner slides open beside the label as it morphs to "Saving"; on success a check draws itself and the label becomes "Saved", keeping the letters they share; on failure the button gives a small shake and asks you to try again. Its width eases to each label, so nothing around it jumps.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/status-button.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): `text-morph`
- Docs and live preview: https://hairlineui.com/ui/status-button
- Registry item (source): https://hairlineui.com/r/status-button.json

## Usage

```tsx
import { StatusButton, type ActionStatus } from "@/components/status-button";

const [status, setStatus] = useState<ActionStatus>("idle");

<StatusButton status={status} onClick={save} onReset={() => setStatus("idle")} />
```

## With a request

Drive status from your request; the button handles every transition.

```tsx
"use client";
import { useState } from "react";
import { StatusButton, type ActionStatus } from "@/components/status-button";

export function SaveSettings({ values }: { values: Settings }) {
  const [status, setStatus] = useState<ActionStatus>("idle");

  const save = async () => {
    setStatus("pending");
    try {
      const res = await fetch("/api/settings", { method: "PUT", body: JSON.stringify(values) });
      setStatus(res.ok ? "success" : "error");
    } catch {
      setStatus("error");
    }
  };

  return <StatusButton status={status} labels={{ idle: "Save changes" }} onClick={save} onReset={() => setStatus("idle")} />;
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `status` | `"idle" \| "pending" \| "success" \| "error"` | `"idle"` | What the button shows. |
| `labels` | `Partial<Record<status, string>>` | `Save, Saving, Saved, Try again` | Wording per state. |
| `icon` | `ReactNode` | – | An icon beside the idle label, e.g. a link icon for Copy link. |
| `variant` | `"primary" \| "outline" \| "ghost"` | `"primary"` | Filled with your brand colour, a hairline outline, or quiet text for toolbars. |
| `size` | `"default" \| "sm"` | `"default"` | 36px, or 28px for headers and toolbars (Copy in a code block, Undo in a receipt). |
| `onReset / resetAfter` | `() => void / number` | `— / 1800` | Called that long after success, to go back to idle. |

## States

- **idle**: The label and, if you pass one, an icon.
- **pending**: A spinner opens in from zero width; the label morphs ("Save changes" → "Saving"). Clicks are ignored and aria-busy is set.
- **success**: The spinner blurs out as a check draws itself in 420ms; "Saved". onReset fires after resetAfter.
- **error**: A 380ms shake, a soft red tint and an alert icon; "Try again".

## What it handles

It is the most common button in any product and usually the least considered: it greys out, maybe shows a spinner, and snaps back. This one handles every state of an action in one motion, and many other components in the library are built on it.

- **Save, Saving, Saved**: A spinner opens beside the label as it morphs to “Saving”, then the label becomes “Saved”, keeping the letters they share.
- **A check that draws itself**: On success the spinner blurs out as a check draws in.
- **Errors shake and offer a retry**: On failure the button gives a small shake, tints softly red and reads “Try again”.
- **No layout jumps**: The width eases to each label, so nothing around the button moves.
- **Three looks, two sizes**: Primary, outline or ghost, at 36px or at 28px for toolbars.
- **Accessible by default**: While pending it ignores clicks without dimming or losing focus, and state changes are announced beside it.

## Replaces

a loading button, a spinner, a success tick, an error shake, label changes, a reset timer

## Where it fits

- **Save buttons**: Forms and settings where saving takes a moment and may fail.
- **Copy buttons**: Copy link or Copy code, with a check when it worked.
- **Retry and Undo**: Small toolbar buttons that show their own progress.
- **Sign-up and checkout**: Submit buttons that show waiting, success and failure without a layout jump.

## Notes

- State changes are announced from a status region beside the button, so the button’s name stays just its label.
- While pending it is aria-busy and ignores clicks, without dimming or losing focus.
- The check is a stroked path that draws itself; the shake uses the Web Animations API. No animation library.
- Installs Text morph alongside it. With reduced motion, states change in place.

License: MIT (https://hairlineui.com/license#components)
