# GlassActionButton

> A button for one async action. Its arrow floats apart as a glass droplet; press it and the droplet melts into the body, and stays.

```tsx
import { RotateCcw } from 'lucide-react'
import { useState } from 'react'

import { GlassActionButton } from '@/components/liquid-glass'

const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

// Press it: the arrow droplet melts into the button and stays merged.
// "Start over" changes `resetKey`: the droplet splits off again and the label morphs back.
// Each new label comes in letter by letter.
export default function Demo() {
  const [run, setRun] = useState(0)
  return (
    <div className="flex flex-col items-center gap-6">
      <GlassActionButton
        resetKey={run}
        size="lg"
        material="tinted"
        className="bg-neutral-900 text-white dark:bg-white dark:text-neutral-900"
        pendingLabel="Deploying"
        successLabel="Deployed"
        onAction={() => wait(1400)}
      >
        Deploy to production
      </GlassActionButton>
      <button
        type="button"
        onClick={() => setRun((n) => n + 1)}
        className="flex cursor-pointer items-center gap-1.5 text-sm text-neutral-500 hover:text-neutral-900 dark:text-neutral-400 dark:hover:text-white"
      >
        <RotateCcw className="size-3.5" />
        Start over
      </button>
    </div>
  )
}
```

**Use it for**

- One async commit whose result belongs on the button: Deploy, Publish, Add to cart, Subscribe.
- Actions that stay done: the merged "Deployed ✓" is the new state of the page.

**Not for**

- Plain links and instant actions: use GlassButton.
- Toggling back and forth between two states: use GlassTabs.
- More than one per screen: the droplet is the kit's one gooey effect, keep it special.

```bash
npx shadcn@latest add https://onemorethink.dev/r/action-button.json
```

## Usage

```tsx
import { GlassActionButton } from '@/components/liquid-glass'

<GlassActionButton
  material="tinted"
  className="bg-neutral-900 text-white"
  pendingLabel="Deploying"
  successLabel="Deployed"
  onAction={deploy} // return the promise
>
  Deploy
</GlassActionButton>
```

## The flow

- **Idle.** The arrow floats apart from the body as a separate glass droplet.
- **Press.** The droplet stretches back on a gooey bridge and melts into the body.
- **Pending.** One body; the label morphs to `pendingLabel` and a spinner turns where the arrow was, until the promise from `onAction` settles.
- **Success.** Still one body: `successLabel` and a check that draws itself in. As it lands the glass swells, as if hovered and pressed at the same time, and stretches the label and the check with it; it lets go a moment after the label is in. It stays merged, unless you set `resetAfter`.
- **Error.** If `onAction` throws, the droplet splits off again and `onActionError` gets the error.
- **Start over.** Change `resetKey` and the droplet splits off again from any state. Don't remount the button with `key`: that snaps it back.

## Examples

### Reset after success, and a failing action

```tsx
import { Plus } from 'lucide-react'

import { GlassActionButton } from '@/components/liquid-glass'

const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

// Left: `resetAfter` splits the droplet off again after a success.
// Right: a rejected action splits it off at once, and `onActionError` gets the error.
export default function Demo() {
  return (
    <div className="flex flex-wrap items-center justify-center gap-x-8 gap-y-5">
      <GlassActionButton
        size="lg"
        material="tinted"
        className="bg-blue-600 text-white"
        icon={<Plus />}
        pendingLabel="Adding"
        successLabel="Added"
        resetAfter={2400}
        onAction={() => wait(900)}
      >
        Add to cart
      </GlassActionButton>
      <GlassActionButton
        size="lg"
        material="tinted"
        className="bg-rose-600 text-white"
        pendingLabel="Sending"
        onAction={() => wait(900).then(() => Promise.reject(new Error('Offline')))}
        onActionError={(error) => console.warn(error)}
      >
        Send (fails)
      </GlassActionButton>
    </div>
  )
}
```

With `resetAfter={2400}` the droplet splits off again 2.4 seconds after a success, ready for the next press. Any icon element can be the droplet with `icon`; the spinner and the check are built in.

## API reference

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` | — | The idle label, and the button's accessible name. |
| `onAction` | `() => unknown` | — | Required. Runs on press; return a promise to stay pending until it settles. |
| `onActionError` | `(error) => void` | — | Called when `onAction` throws; the button returns to idle. |
| `pendingLabel` | `ReactNode` | `children` | Label while `onAction` runs. |
| `successLabel` | `ReactNode` | `children` | Label after it succeeds. |
| `resetAfter` | `number` | `0` | Milliseconds before splitting off again after a success. `0` stays merged. |
| `resetKey` | `unknown` | — | Change it to return to idle: the droplet splits off again. A running action's result is dropped. |
| `icon` | `ReactNode` | `arrow` | The droplet's idle icon. |
| `material` | `'clear' \| 'tinted'` | `'clear'` | As on GlassButton. |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Height 32, 40 or 48 px. |
| `…rest` | `button attributes` | — | Passed to the `<button>` (not `onClick`: use `onAction`). |

## Accessibility

- A real `<button>`; Enter and Space run the action.
- Pending sets `aria-busy`, and pending or done sets `aria-disabled` instead of `disabled`, so focus stays on the button.
- A `role="status"` region announces the pending and success labels.
- With reduced motion the droplet and labels switch without animation.

Source: https://onemorethink.dev/docs/action-button
