# Button

Runs an action. Built on ui-sdk's Button: Enter and Space press it, a returned Promise keeps it busy, and render turns it into a link. Four intents and four sizes, from the Refined UI Components page.

- Beta: ready to use; its API can still change in a minor release.
- Category: Actions

```tsx
import { Button, IconButton } from "@leap-crm/design-system";
```

## Best practices

- Do: Use `primary` for the main action and `outline` for the actions beside it, like Save and Cancel. Use `ghost` in toolbars and rows, and `destructive` for deleting.
- Do: Return a Promise from `onClick`, so the button stays busy until the work settles. Use `loading` only for busy state that comes from outside, like a mutation.
- Do: Pass `showResult` with labels, like `{ done: "Saved", error: "Couldn't save" }`, when the outcome should be announced. The button's own name never changes.
- Do: Pass an `<a>` or a router link to `render` when the button navigates, like Download agreement, so it keeps link semantics.
- Don't: Use `xs` (28 px) for a primary action or anything used on touch. Those need a 44 px hit target; keep `xs` for dense rows and toolbars.
- Don't: Put a Button inside a sentence or a paragraph. Use `TextLink` for an action that reads as part of the text.

## Keyboard

| Keys | What they do |
| --- | --- |
| Enter, Space | Presses the focused button. |
| Tab | Reaches it. A disabled button is skipped. |

## Related

- [Split Button](./split-button.md)
- [Button Group](./button-group.md)
- [Text Link](./text-link.md)
- [Toggle](./toggle.md)
- [Close Button](./close-button.md)

## Examples

What the examples import:

```tsx
import { Check, Download, Plus, Save, Send, Settings, Trash2 } from "lucide-react";
import { Fragment, type ReactNode, useState } from "react";
import { Button, IconButton } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
const intents = ["primary", "outline", "ghost", "destructive"] as const;

const sizes = ["xs", "sm", "md", "lg"] as const;

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

function Matrix({
  columns,
  children,
}: {
  columns: readonly string[];
  children: ReactNode;
}) {
  return (
    <div className="grid grid-cols-[auto_repeat(4,auto)] items-center justify-start gap-x-6 gap-y-4">
      <span />
      {columns.map(column => (
        <span key={column} className="text-small text-neutral-text-muted">
          {column}
        </span>
      ))}
      {children}
    </div>
  );
}

function RowLabel({ children }: { children: ReactNode }) {
  return (
    <span className="text-small text-neutral-text-muted capitalize">
      {children}
    </span>
  );
}

function BusyDemo() {
  const [loading, setLoading] = useState(false);
  return (
    <div className="flex flex-col gap-5">
      <div className="flex flex-wrap items-center gap-3">
        <Button icon={<Plus />} onClick={() => wait(2000)}>
          Add contact
        </Button>
        <Button intent="outline" onClick={() => wait(2000)}>
          Send
        </Button>
        <IconButton
          intent="outline"
          icon={<Download />}
          aria-label="Download agreement"
          onClick={() => wait(2000)}
        />
        <Button intent="ghost" icon={<Check />} onClick={() => wait(80)}>
          Quick save
        </Button>
      </div>
      <div className="flex flex-wrap items-center gap-3">
        <Button intent="outline" icon={<Send />} loading={loading}>
          Send email
        </Button>
        <Button
          intent="ghost"
          size="sm"
          aria-pressed={loading}
          onClick={() => setLoading(value => !value)}
        >
          {loading ? "Stop loading" : "Start loading"}
        </Button>
      </div>
    </div>
  );
}

const fail = (ms: number) =>
  new Promise((_, reject) => setTimeout(() => reject(new Error("Failed")), ms));

function ResultDemo() {
  return (
    <div className="flex flex-wrap items-center gap-3">
      <Button
        icon={<Save />}
        showResult={{ done: "Saved", error: "Couldn't save" }}
        onClick={() => wait(900)}
      >
        Save
      </Button>
      <Button
        intent="outline"
        showResult={{ done: "Sent", error: "Couldn't send" }}
        onClick={() => wait(900)}
      >
        Send
      </Button>
      <Button
        intent="outline"
        icon={<Send />}
        showResult={{ done: "Sent", error: "Couldn't send" }}
        onClick={() => fail(900)}
      >
        Send (fails)
      </Button>
      <IconButton
        intent="outline"
        icon={<Download />}
        aria-label="Download agreement"
        showResult={{ done: "Downloaded" }}
        onClick={() => wait(80)}
      />
    </div>
  );
}
```

### Intents and sizes

Primary for the main action, Outline beside it, Ghost in toolbars and rows, Destructive for deleting.

```tsx
<Matrix columns={sizes}>
  {intents.map(intent => (
    <Fragment key={intent}>
      <RowLabel>{intent}</RowLabel>
      {sizes.map(size => (
        <Button
          key={`${intent}-${size}`}
          intent={intent}
          size={size}
          icon={intent === "destructive" ? <Trash2 /> : <Plus />}
        >
          Button
        </Button>
      ))}
    </Fragment>
  ))}
</Matrix>
```

### Busy

Press each. The icon turns into the spinner and back. Without an icon, the spinner opens its own space beside the text. Quick save settles in 80 ms and still plays the whole swap, because busy holds for at least 600 ms once shown. The second row drives the loading prop. With reduced motion, the swap is a plain crossfade.

```tsx
<BusyDemo />
```

### Result

With showResult, the spinner turns into a tick that draws itself in when onClick's Promise resolves, or a cross when it rejects, then the icon comes back. The result holds for 1.4 s after the spinner's full pass, so a quick job still reads. Pass labels and the outcome is announced politely; the button's name never changes. Without an icon, the mark keeps its space open until it leaves.

```tsx
<ResultDemo />
```

### States

Hover and press each to see their states; Tab to see focus. Loading comes from the loading prop. Pending comes from a Promise returned by onClick: press it and it stays busy for two seconds.

```tsx
<Matrix columns={["Default", "Disabled", "Loading", "Pending"]}>
  {intents.map(intent => (
    <Fragment key={intent}>
      <RowLabel>{intent}</RowLabel>
      <Button key={`${intent}-default`} intent={intent} icon={<Plus />}>
        Save
      </Button>
      <Button
        key={`${intent}-disabled`}
        intent={intent}
        icon={<Plus />}
        disabled
      >
        Save
      </Button>
      <Button
        key={`${intent}-loading`}
        intent={intent}
        icon={<Plus />}
        loading
      >
        Save
      </Button>
      <Button
        key={`${intent}-pending`}
        intent={intent}
        icon={<Plus />}
        onClick={() => wait(2000)}
        pendingAnnouncement="Saving"
      >
        Press me
      </Button>
    </Fragment>
  ))}
</Matrix>
```

### Icon Button

Square at the same heights as Button, Ghost by default. aria-label is required, because there is no visible text to name it.

```tsx
<Matrix columns={sizes}>
  {intents.map(intent => (
    <Fragment key={intent}>
      <RowLabel>{intent}</RowLabel>
      {sizes.map(size => (
        <IconButton
          key={`${intent}-${size}`}
          intent={intent}
          size={size}
          icon={intent === "destructive" ? <Trash2 /> : <Settings />}
          aria-label={intent === "destructive" ? "Delete" : "Settings"}
        />
      ))}
    </Fragment>
  ))}
</Matrix>
```

### As a link

Pass an <a> or a router Link to render. It keeps link semantics; a disabled link can't navigate.

```tsx
<div className="flex gap-3">
  <Button
    intent="outline"
    icon={<Download />}
    render={<a href="/button" />}
  >
    Download agreement
  </Button>
  <Button intent="outline" disabled render={<a href="/button" />}>
    Disabled link
  </Button>
</div>
```

## Props

Both also take every prop of ui-sdk's Button: onClick, disabled, focusableWhenDisabled, type, pendingAnnouncement and render.

### Button

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` |  | What it shows inside. |
| `intent` | `"primary" \| "outline" \| "ghost" \| "destructive"` | `"primary"` | primary for the main action, outline beside it, ghost in toolbars and rows, destructive for deleting. |
| `size` | `"xs" \| "sm" \| "md" \| "lg"` |  | xs 28 · sm 32 · md 36 (the default) · lg 40. |
| `icon` | `ReactNode` |  | Leading icon. It turns into the spinner while the button is busy, and back. |
| `loading` | `boolean` | `false` | Busy from outside state, like a mutation. Prefer returning a Promise from onClick. |
| `showResult` | `boolean \| ButtonResultLabels` | `false` | When onClick's Promise settles, the spinner turns into a tick (resolved) or a cross (rejected) for a moment before the icon returns. Pass labels to have the outcome announced. |
| `result` | `"done" \| "error"` |  | Holds a result mark in place of the icon, like the option a lawyer already picked on a card. A showResult tick settles into it without redrawing. |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |

### IconButton

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `icon` (required) | `ReactNode` |  | The only content. It turns into the spinner while the button is busy, and back. |
| `aria-label` (required) | `string` |  | An icon-only button has no visible text, so its name is required. |
| `intent` | `"primary" \| "outline" \| "ghost" \| "destructive"` | `"ghost"` | primary for the main action, outline beside it, ghost in toolbars and rows, destructive for deleting. |
