# Button

Triggers an action, from the main call to action to a small icon button.

The button for every action: primary for the main one, outline, ghost or danger for the rest. The app's core button, in the app's shape: a pill unless the app is rounded. Height comes from symmetric padding plus a minimum, so the label is always centred and every size lands on a whole pixel, on the one control scale (xs 24, sm 28, md 32, lg 36). Icon-only (square) at every size; shape rounded for card headers and toolbars; ghost + muted for the small icon buttons in headers, fields and rows. Works on <button> and <a>.

Kind: component · Category: Actions · Status: stable · Targets: React, Vue, Svelte

## Add

```bash
npx fndn add button
```

It writes the folder `button/` into the project's ui folder (`index.ts`, `button.tsx`, `Button.vue`, `Button.svelte`, `button.recipe.ts`), and what it is built from: [Spinner](/components/spinner.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { Button } from "@/components/ui/button";
```

```vue
import { Button } from "@/components/ui/button";
```

```svelte
import { Button } from "$lib/components/ui/button";
```

## Parts

In the DOM the root carries `data-slot="button"` and each part `data-slot="button-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `button` | `button` |  | The button itself; may also be an <a>. |
| `icon` | `span` | `button-icon` | optional | Wraps and centres the leading (or only) icon; aria-hidden. A trailing icon (a caret after the label: menu and popover triggers) is a second icon part after the label, with data-trailing; the same size and gap rules apply. The icon inside is 1em of the label, a size larger when it stands alone (14px at xs, 15px at sm, 16px at md, 18px at lg). Two icons in it share one cell and cross-fade (see the swapped prop). |
| `label` | `span` | `button-label` | optional | Text of the button; truncates at the end when the button has less room than its label. Optional in hand-written markup. Two label parts nested in it share one cell and cross-fade: the first shows until the button is swapped (Copy / Copied); the button is as wide as the label it shows. |
| `spinner` | `span` | `button-spinner` | optional | While busy: the real Spinner (decorative), marked data-button="spinner", 1em in the button's own ink. It takes the leading icon's cell (the glyph stays, transparent); without a leading icon it is centred over the button and the label stays, transparent, so the width never changes. |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `"primary" \| "outline" \| "secondary" \| "accent" \| "success" \| "danger" \| "danger-outline" \| "ghost"` | `"outline"` | The names follow shadcn's (outline, secondary, ghost), and the default is outline (shadcn's unnamed default is our primary). ghost: transparent, hover fill hover, press hover-2 plus the press scale, hover-2 while open (aria-expanded). danger: filled red, for the confirmation of a destructive action (the alert dialog's action). danger-outline: the surface with red text and a red edge, filled red on hover, for the button that starts a destructive action in a danger zone (GitHub's "Delete this repository"). |
| `size` | `"xs" \| "sm" \| "md" \| "lg"` | `"md"` | The one control height scale (every pressable or typed-into control shares it, so an sm Button and an sm Select line up): xs 24px (12px text, 6px inline padding, 4px gap), sm 28px (13px, 12px, 6px), md 32px (14px, 16px, 8px), lg 36px (14px, 18px, 8px). Weight 500 at every size. Size changes dimensions only (height, padding, text, gap, icon; rounded takes radius.chip at xs and sm, as a field of that height does), never the fill, ring or focus. |
| `shape` | `"pill" \| "rounded"` |  | Radius only. Left out, the app's shape: a pill, or rounded in a rounded app ("shape": "rounded" in the project's fndn.json). Named, it is that shape whatever the app's. pill: fully round, so a pill square is a circle. rounded: the radius of a field of the same height, so a Button and a Select in one row have the same corners: radius.chip at xs and sm, radius.control at md and lg. |
| `square` | `boolean` | `false` | Icon-only: as wide as it is tall at every size (24, 28, 32, 36). Needs an aria-label. |
| `muted` | `boolean` | `false` | Quiet at rest, ink when hovered, pressed or open (with variant ghost). An icon-only button (square) rests in ink-3: the small icon buttons in card headers, fields and rows. A button with a label rests in ink-2, since its label is read. |
| `swapped` | `boolean` | `false` | Two states in one cell (Copy / Copied): the icon part holds two icons and the label part two label parts; swapped shows the second of each, cross-fading. The button is as wide as the label it shows. Announce the change through a status region. |
| `iconSize` | `number` |  | Square buttons: the icon's size in px, for a host whose glyphs follow another rhythm (sidebar-nav's 18px rail). Defaults to 14 (xs), 15 (sm), 16 (md), 18 (lg). |
| `busy` | `boolean` | `false` | Work under way (Saving…, Deploying…): the spinner replaces the leading icon, or covers the label (an icon button: its icon), aria-busy="true", a press is dropped (a submit button does not submit) and does not scale. The button keeps its width and its focus; change the label yourself if the verb changes. |
| `disabled` | `boolean` | `false` |  |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `click` | native click |

## Slots

| Slot | Content |
| --- | --- |
| `default` | Label |
| `icon` | Leading icon (wrapped in the icon part) |
| `trailingIcon` | Trailing icon after the label, e.g. a caret (wrapped in an icon part with data-trailing). React: the trailingIcon prop (a node); Vue: the trailingIcon slot; Svelte: the trailingIcon snippet. |

## States

`hover`, `press`, `focus-visible`, `disabled`, `expanded`, `swapped`, `busy`

## Keyboard and accessibility

Role: button.

Keyboard:

- Enter / Space activate

Notes:

- An icon-only button needs an accessible name (aria-label).
- Use aria-expanded on a button that opens something; the outline variant styles it.
- A link that looks like a button is an <a> with the button recipe's classes, not a button with a click handler.
- Disabled buttons are inert: no hover, no press, not clickable.
- Two states in one cell: the stacked icons and labels are aria-hidden, the button keeps a fixed aria-label, and the change is announced through a role="status" region (code-block does this).
- Busy: aria-busy="true" and the click is dropped, but the button stays focusable and named (not disabled), so focus is not lost while the work runs. Announce the outcome through a status region.

## Motion

- press: scale to motion.press-scale over duration.press, easing.out-strong (label and icon scale with the surface); ghost also darkens to hover-2
- hover (pointer only): fill, colour, opacity or brightness over duration.fast, ease
- keyboard: a popup or disclosure trigger (aria-haspopup or aria-expanded) changes at once when the keyboard opens or closes it (kbd-instant): the open fill appears with Enter or ArrowDown, not over duration.fast. Other Buttons keep their transitions (prompt-bar's send travels while the user types)
- two states in one cell: opacity and a 2px blur cross-fade over duration.base, ease
- no entrance and no other motion
- two states (swapped, e.g. Copy / Copied): the labels and icons cross-fade with a 2px blur at duration.base, and the button's width eases from the old label's to the new one's in the same frames (duration.base, out-strong); from the keyboard the swap is instant
- busy: the spinner and what it replaces swap at once, both ways (no fade: from the keyboard it is instant, and a button that is busy at mount plays nothing); the spinner turns (the Spinner's loop)

## Tokens

`color.surface`, `color.ink`, `color.on-ink`, `color.inset`, `color.hover`, `color.hover-2`, `color.line-strong`, `color.accent`, `color.accent-ink`, `color.on-accent`, `color.green`, `color.red`, `shadow.btn`, `radius.pill`, `radius.shape`, `radius.shape-sm`, `text.xs`, `text.sm`, `text.md`, `duration.fast`, `duration.base`, `duration.press`, `easing.out-strong`, `motion.press-scale`, `color.ink-3`, `radius.control`, `radius.chip`

## Dependencies

- [Spinner](/components/spinner.md)

## Examples

### Save or cancel a form

React · `save-changes.tsx`

```tsx
import { Button } from "@/components/ui/button";

/* a form footer: the primary action last, Cancel before it */
export default function SaveChanges() {
  return (
    <div className="inline-flex items-center gap-2">
      <Button>Cancel</Button>
      <Button variant="primary">Save changes</Button>
    </div>
  );
}
```

Vue · `save-changes.vue`

```vue
<script setup lang="ts">
import { Button } from "@/components/ui/button";
</script>

<!-- a form footer: the primary action last, Cancel before it -->
<template>
  <div class="inline-flex items-center gap-2">
    <Button>Cancel</Button>
    <Button variant="primary">Save changes</Button>
  </div>
</template>
```

Svelte · `save-changes.svelte`

```svelte
<script lang="ts">
  import { Button } from "$lib/components/ui/button";
</script>

<!-- a form footer: the primary action last, Cancel before it -->
<div class="inline-flex items-center gap-2">
  <Button>Cancel</Button>
  <Button variant="primary">Save changes</Button>
</div>
```

### Delete a project

React · `delete-project.tsx`

```tsx
import { Trash } from "@phosphor-icons/react";
import { Button } from "@/components/ui/button";

/* a danger zone: the outline danger look (red edge, red fill on hover); the filled danger is for the confirmation that follows. The label names what goes */
export default function DeleteProject() {
  return (
    <div className="flex max-w-96 flex-col items-start gap-3">
      <div>
        <p className="text-sm font-medium text-ink">Delete billing-api</p>
        <p className="text-sm text-ink-2">Removes 128 deploys, 3 environments and every API key of this project.</p>
      </div>
      <Button variant="danger-outline" icon={<Trash data-slot="icon" weight="bold" />}>Delete project</Button>
    </div>
  );
}
```

Vue · `delete-project.vue`

```vue
<script setup lang="ts">
import { PhTrash } from "@phosphor-icons/vue";
import { Button } from "@/components/ui/button";
</script>

<!-- a danger zone: the outline danger look (red edge, red fill on hover); the filled danger is for the confirmation that follows. The label names what goes -->
<template>
  <div class="flex max-w-96 flex-col items-start gap-3">
    <div>
      <p class="text-sm font-medium text-ink">Delete billing-api</p>
      <p class="text-sm text-ink-2">Removes 128 deploys, 3 environments and every API key of this project.</p>
    </div>
    <Button variant="danger-outline">
      <template #icon><PhTrash data-slot="icon" weight="bold" /></template>
      Delete project
    </Button>
  </div>
</template>
```

Svelte · `delete-project.svelte`

```svelte
<script lang="ts">
  import Trash from "phosphor-svelte/lib/TrashIcon";
  import { Button } from "$lib/components/ui/button";
</script>

<!-- a danger zone: the outline danger look (red edge, red fill on hover); the filled danger is for the confirmation that follows. The label names what goes -->
<div class="flex max-w-96 flex-col items-start gap-3">
  <div>
    <p class="text-sm font-medium text-ink">Delete billing-api</p>
    <p class="text-sm text-ink-2">Removes 128 deploys, 3 environments and every API key of this project.</p>
  </div>
  <Button variant="danger-outline">
    {#snippet icon()}<Trash data-slot="icon" weight="bold" />{/snippet}
    Delete project
  </Button>
</div>
```

### Merge a pull request whose checks passed

React · `merge-pull-request.tsx`

```tsx
import { CheckCircle } from "@phosphor-icons/react";
import { Button } from "@/components/ui/button";

/* success: the green fill for the one action whose colour says "this is safe to go", once every
 * check has passed; the line above it says why */
export default function MergePullRequest() {
  return (
    <div className="flex max-w-96 flex-col items-start gap-3">
      <div>
        <p className="flex items-center gap-1.5 text-sm font-medium text-ink">
          <CheckCircle data-slot="icon" weight="fill" className="shrink-0 text-green" aria-hidden />
          All checks have passed
        </p>
        <p className="text-sm text-ink-2">4 successful checks · Approved by Priya Raman. Merges #482 into main.</p>
      </div>
      <Button variant="success">Merge pull request</Button>
    </div>
  );
}
```

Vue · `merge-pull-request.vue`

```vue
<script setup lang="ts">
import { PhCheckCircle } from "@phosphor-icons/vue";
import { Button } from "@/components/ui/button";
</script>

<!-- success: the green fill for the one action whose colour says "this is safe to go", once every check has passed; the line above it says why -->
<template>
  <div class="flex max-w-96 flex-col items-start gap-3">
    <div>
      <p class="flex items-center gap-1.5 text-sm font-medium text-ink">
        <PhCheckCircle data-slot="icon" weight="fill" class="shrink-0 text-green" aria-hidden="true" />
        All checks have passed
      </p>
      <p class="text-sm text-ink-2">4 successful checks · Approved by Priya Raman. Merges #482 into main.</p>
    </div>
    <Button variant="success">Merge pull request</Button>
  </div>
</template>
```

Svelte · `merge-pull-request.svelte`

```svelte
<script lang="ts">
  import CheckCircle from "phosphor-svelte/lib/CheckCircleIcon";
  import { Button } from "$lib/components/ui/button";
</script>

<!-- success: the green fill for the one action whose colour says "this is safe to go", once every check has passed; the line above it says why -->
<div class="flex max-w-96 flex-col items-start gap-3">
  <div>
    <p class="flex items-center gap-1.5 text-sm font-medium text-ink">
      <CheckCircle data-slot="icon" weight="fill" class="shrink-0 text-green" aria-hidden="true" />
      All checks have passed
    </p>
    <p class="text-sm text-ink-2">4 successful checks · Approved by Priya Raman. Merges #482 into main.</p>
  </div>
  <Button variant="success">Merge pull request</Button>
</div>
```

### Icon buttons on a pull request row

React · `row-actions.tsx`

```tsx
import { DotsThree, LinkSimple, PencilSimple } from "@phosphor-icons/react";
import { Button } from "@/components/ui/button";
import { Tooltip } from "@/components/ui/tooltip";

/* icon-only buttons: ghost and muted, square, each named by aria-label and its tooltip */
const actions = [
  { label: "Edit title", Icon: PencilSimple },
  { label: "Copy link", Icon: LinkSimple },
  { label: "More actions", Icon: DotsThree },
];

export default function RowActions() {
  return (
    <div className="flex w-80 items-center justify-between gap-3">
      <p className="min-w-0 text-sm text-ink">#482 Add retry to webhook delivery</p>
      <div role="toolbar" aria-label="Pull request actions" className="flex shrink-0 gap-0.5">
        {actions.map(({ label, Icon }) => (
          <Tooltip key={label} content={label}>
            <Button variant="ghost" size="sm" square muted aria-label={label} icon={<Icon data-slot="icon" weight="bold" />} />
          </Tooltip>
        ))}
      </div>
    </div>
  );
}
```

Vue · `row-actions.vue`

```vue
<script setup lang="ts">
import { PhDotsThree, PhLinkSimple, PhPencilSimple } from "@phosphor-icons/vue";
import { Button } from "@/components/ui/button";
import { Tooltip } from "@/components/ui/tooltip";

/* icon-only buttons: ghost and muted, square, each named by aria-label and its tooltip */
const actions = [
  { label: "Edit title", Icon: PhPencilSimple },
  { label: "Copy link", Icon: PhLinkSimple },
  { label: "More actions", Icon: PhDotsThree },
];
</script>

<template>
  <div class="flex w-80 items-center justify-between gap-3">
    <p class="min-w-0 text-sm text-ink">#482 Add retry to webhook delivery</p>
    <div role="toolbar" aria-label="Pull request actions" class="flex shrink-0 gap-0.5">
      <Tooltip v-for="{ label, Icon } in actions" :key="label" :content="label">
        <Button variant="ghost" size="sm" square muted :aria-label="label">
          <template #icon><component :is="Icon" data-slot="icon" weight="bold" /></template>
        </Button>
      </Tooltip>
    </div>
  </div>
</template>
```

Svelte · `row-actions.svelte`

```svelte
<script lang="ts">
  import DotsThree from "phosphor-svelte/lib/DotsThreeIcon";
  import LinkSimple from "phosphor-svelte/lib/LinkSimpleIcon";
  import PencilSimple from "phosphor-svelte/lib/PencilSimpleIcon";
  import { Button } from "$lib/components/ui/button";
  import { Tooltip } from "$lib/components/ui/tooltip";

  /* icon-only buttons: ghost and muted, square, each named by aria-label and its tooltip */
  const actions = [
    { label: "Edit title", Icon: PencilSimple },
    { label: "Copy link", Icon: LinkSimple },
    { label: "More actions", Icon: DotsThree },
  ];
</script>

<div class="flex w-80 items-center justify-between gap-3">
  <p class="min-w-0 text-sm text-ink">#482 Add retry to webhook delivery</p>
  <div role="toolbar" aria-label="Pull request actions" class="flex shrink-0 gap-0.5">
    {#each actions as { label, Icon } (label)}
      <Tooltip content={label}>
        {#snippet trigger(props)}
          <Button {...props()} variant="ghost" size="sm" square muted aria-label={label}>
            {#snippet icon()}<Icon data-slot="icon" weight="bold" />{/snippet}
          </Button>
        {/snippet}
      </Tooltip>
    {/each}
  </div>
</div>
```

### Small buttons in a table toolbar

React · `table-toolbar.tsx`

```tsx
import { CaretDown, FunnelSimple, Plus } from "@phosphor-icons/react";
import { Button } from "@/components/ui/button";

/* sm (28px) in a toolbar above a table: every control of the row shares the size */
export default function TableToolbar() {
  return (
    <div className="flex w-80 items-center gap-2">
      <Button size="sm" icon={<FunnelSimple data-slot="icon" weight="bold" />}>Filter</Button>
      <Button size="sm" trailingIcon={<CaretDown data-slot="icon" weight="bold" />}>Newest</Button>
      <Button variant="primary" size="sm" className="ms-auto" icon={<Plus data-slot="icon" weight="bold" />}>New project</Button>
    </div>
  );
}
```

Vue · `table-toolbar.vue`

```vue
<script setup lang="ts">
import { PhCaretDown, PhFunnelSimple, PhPlus } from "@phosphor-icons/vue";
import { Button } from "@/components/ui/button";
</script>

<!-- sm (28px) in a toolbar above a table: every control of the row shares the size -->
<template>
  <div class="flex w-80 items-center gap-2">
    <Button size="sm">
      <template #icon><PhFunnelSimple data-slot="icon" weight="bold" /></template>
      Filter
    </Button>
    <Button size="sm">
      Newest
      <template #trailingIcon><PhCaretDown data-slot="icon" weight="bold" /></template>
    </Button>
    <Button variant="primary" size="sm" class="ms-auto">
      <template #icon><PhPlus data-slot="icon" weight="bold" /></template>
      New project
    </Button>
  </div>
</template>
```

Svelte · `table-toolbar.svelte`

```svelte
<script lang="ts">
  import CaretDown from "phosphor-svelte/lib/CaretDownIcon";
  import FunnelSimple from "phosphor-svelte/lib/FunnelSimpleIcon";
  import Plus from "phosphor-svelte/lib/PlusIcon";
  import { Button } from "$lib/components/ui/button";
</script>

<!-- sm (28px) in a toolbar above a table: every control of the row shares the size -->
<div class="flex w-80 items-center gap-2">
  <Button size="sm">
    {#snippet icon()}<FunnelSimple data-slot="icon" weight="bold" />{/snippet}
    Filter
  </Button>
  <Button size="sm">
    Newest
    {#snippet trailingIcon()}<CaretDown data-slot="icon" weight="bold" />{/snippet}
  </Button>
  <Button variant="primary" size="sm" class="ms-auto">
    {#snippet icon()}<Plus data-slot="icon" weight="bold" />{/snippet}
    New project
  </Button>
</div>
```

### Deploying to Production

React · `deploying.tsx`

```tsx
import { useEffect, useRef, useState } from "react";
import { RocketLaunch } from "@phosphor-icons/react";
import { Button } from "@/components/ui/button";

/* busy: the spinner takes the icon's place, the button keeps its width and its focus, and a second
 * press does nothing; the status beside it says what is running */
export default function Deploying() {
  const [busy, setBusy] = useState(false);
  const timer = useRef(0);
  useEffect(() => () => clearTimeout(timer.current), []);
  const deploy = () => {
    setBusy(true);
    timer.current = window.setTimeout(() => setBusy(false), 2500);
  };
  return (
    <div className="flex items-center gap-3">
      <Button variant="primary" busy={busy} onClick={deploy} icon={<RocketLaunch data-slot="icon" weight="bold" />}>
        Deploy to Production
      </Button>
      <span className="text-sm text-ink-2" role="status">{busy ? "Deploying a1b2c3d…" : ""}</span>
    </div>
  );
}
```

Vue · `deploying.vue`

```vue
<script setup lang="ts">
import { onBeforeUnmount, ref } from "vue";
import { PhRocketLaunch } from "@phosphor-icons/vue";
import { Button } from "@/components/ui/button";

/* busy: the spinner takes the icon's place, the button keeps its width and its focus, and a second
 * press does nothing; the status beside it says what is running */
const busy = ref(false);
let timer = 0;
onBeforeUnmount(() => clearTimeout(timer));
function deploy() {
  busy.value = true;
  timer = window.setTimeout(() => (busy.value = false), 2500);
}
</script>

<template>
  <div class="flex items-center gap-3">
    <Button variant="primary" :busy="busy" @click="deploy">
      <template #icon><PhRocketLaunch data-slot="icon" weight="bold" /></template>
      Deploy to Production
    </Button>
    <span class="text-sm text-ink-2" role="status">{{ busy ? "Deploying a1b2c3d…" : "" }}</span>
  </div>
</template>
```

Svelte · `deploying.svelte`

```svelte
<script lang="ts">
  import RocketLaunch from "phosphor-svelte/lib/RocketLaunchIcon";
  import { Button } from "$lib/components/ui/button";

  /* busy: the spinner takes the icon's place, the button keeps its width and its focus, and a second
   * press does nothing; the status beside it says what is running */
  let busy = $state(false);
  let timer = 0;
  $effect(() => () => clearTimeout(timer));
  function deploy() {
    busy = true;
    timer = window.setTimeout(() => (busy = false), 2500);
  }
</script>

<div class="flex items-center gap-3">
  <Button variant="primary" {busy} onclick={deploy}>
    {#snippet icon()}<RocketLaunch data-slot="icon" weight="bold" />{/snippet}
    Deploy to Production
  </Button>
  <span class="text-sm text-ink-2" role="status">{busy ? "Deploying a1b2c3d…" : ""}</span>
</div>
```

### Only owners can transfer (disabled)

React · `owners-only.tsx`

```tsx
import { Button } from "@/components/ui/button";

/* disabled says why next to it: a disabled button alone gives no reason */
export default function OwnersOnly() {
  return (
    <div className="flex max-w-80 flex-col items-start gap-2">
      <Button disabled aria-describedby="transfer-why">Transfer project</Button>
      <p id="transfer-why" className="text-sm text-ink-2">Only Maya Chen, the workspace owner, can transfer projects.</p>
    </div>
  );
}
```

Vue · `owners-only.vue`

```vue
<script setup lang="ts">
import { Button } from "@/components/ui/button";
</script>

<!-- disabled says why next to it: a disabled button alone gives no reason -->
<template>
  <div class="flex max-w-80 flex-col items-start gap-2">
    <Button disabled aria-describedby="transfer-why">Transfer project</Button>
    <p id="transfer-why" class="text-sm text-ink-2">Only Maya Chen, the workspace owner, can transfer projects.</p>
  </div>
</template>
```

Svelte · `owners-only.svelte`

```svelte
<script lang="ts">
  import { Button } from "$lib/components/ui/button";
</script>

<!-- disabled says why next to it: a disabled button alone gives no reason -->
<div class="flex max-w-80 flex-col items-start gap-2">
  <Button disabled aria-describedby="transfer-why">Transfer project</Button>
  <p id="transfer-why" class="text-sm text-ink-2">Only Maya Chen, the workspace owner, can transfer projects.</p>
</div>
```

## Recipe

`button.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* button — the app's core button, in the app's shape (a pill unless the app is rounded). Works on
 * <button> and <a>.
 * Height comes from symmetric padding plus a minimum, so the label is always centred and every
 * size lands on a whole pixel: xs 24 · sm 28 · md 32 · lg 36, the one height scale every control
 * shares, so a sm Button and a sm Select line up in a toolbar. Size changes dimensions only
 * (height, padding, text, gap, icon, the radius step that keeps a rounded xs concentric): every
 * size has the same weight, fill, ring and focus. Text 12 · 13 · 14 · 14.
 *
 * The adapters always put text in the label part, which is what gets cut when the button is
 * squeezed; there is no bare-text button.
 *
 * Parts are found by data-slot
 * (button, button-icon, button-label); the root slot is `base`.
 *
 * Two states in one cell (Copy / Copied): the icon part holds two icons, the label part holds two
 * label parts. The first shows until the button is `swapped` (a prop, set by the caller), then
 * they cross-fade. The button is as wide as the label it shows: the label that is not showing is out of flow,
 * pinned to the start of the cell, so it fades from where it stood. The stacked runs are
 * aria-hidden and the button has an aria-label; announce the change through a status region.
 *
 * Busy (a save or a deploy under way): the real Spinner (marked data-button="spinner") takes the
 * leading icon's cell, or, without an icon, sits over the label (an icon button: over its icon),
 * while what it covers turns transparent in place: the button keeps its width. It swaps at once
 * both ways (no fade, so nothing plays on first paint and the keyboard is instant); the press
 * does not scale and the adapters drop the click (aria-busy="true"; still focusable). */

/* the 1px top highlight of the filled variants */
const FILL = "shadow-[inset_0_1px_0_oklch(1_0_0/0.14)]";

export const button = tv({
  slots: {
    base: [
      /* the stylesheet resets only buttons marked data-slot or inside a component; these two are
       * what that reset adds, for when the recipe is put on another element */
      "appearance-none [text-align:inherit]",
      "inline-flex shrink-0 items-center justify-center max-w-full",
      "rounded-shape font-medium leading-none whitespace-nowrap no-underline",
      /* an <a> gets the touch behaviour real buttons have */
      "touch-manipulation select-none [-webkit-touch-callout:none]",
      "disabled:opacity-50 disabled:pointer-events-none aria-disabled:opacity-50 aria-disabled:pointer-events-none",
      /* motion: colours, opacity and filter change fast, the shadow a beat slower; a press scales
       * the whole button (label and icon with it), so it reads as a physical press. From the pointer
       * only: Space and Enter act at once, like every other control (the adapters start the
       * modality tracker; without it the press always scales). A popup or disclosure trigger
       * (aria-haspopup, aria-expanded) changes at once when the keyboard opens or closes it
       * (kbd-instant): Enter or ArrowDown fills it now, not over duration.fast. Only on those: a
       * Button that moves while the user types (prompt-bar's send) keeps its travel */
      "[&[aria-haspopup]]:kbd-instant [&[aria-expanded]]:kbd-instant motion-colors-fast motion-opacity-fast motion-filter-fast motion-shadow-base motion-transform-press data-swapping:motion-size-base [:root:not([data-fndn-input=keyboard])_&:active]:scale-press",
    ],
    /* a label longer than the room it has is cut at the end of its own text (unicode-bidi); the
     * 2px block padding, taken back by the margin, keeps descenders out of the clip */
    label: [
      "min-w-0 -my-0.5 py-0.5 overflow-hidden text-ellipsis [unicode-bidi:plaintext]",
      /* while the width eases to a swapped label (Copy → Copied), the new label is laid out at its
       * full width before the button has reached it: drawn whole into the padding for those frames,
       * never cut with an ellipsis: an ellipsis there would flicker */
      "[[data-swapping]_&]:overflow-visible",
      /* holding two label parts (Copy / Copied): one grid cell, nothing clipped */
      "[&:has(>[data-slot=button-label])]:inline-grid [&:has(>[data-slot=button-label])]:relative [&:has(>[data-slot=button-label])]:my-0",
      "[&:has(>[data-slot=button-label])]:py-0 [&:has(>[data-slot=button-label])]:overflow-visible",
      /* the two labels share the cell and cross-fade; the second waits out of flow */
      "[&>[data-slot=button-label]]:[grid-area:1/1] [&>[data-slot=button-label]]:motion-opacity-base [&>[data-slot=button-label]]:motion-filter-base",
      "[&>[data-slot=button-label]:nth-child(2)]:opacity-0 [&>[data-slot=button-label]:nth-child(2)]:[filter:blur(2px)]",
      "[&>[data-slot=button-label]:nth-child(2)]:absolute [&>[data-slot=button-label]:nth-child(2)]:top-0 [&>[data-slot=button-label]:nth-child(2)]:start-0 [&>[data-slot=button-label]:nth-child(2)]:whitespace-nowrap",
    ],
    /* wraps the icon and centres it; the icon is 1em of the button's text. A trailing icon (a caret
     * after the label) is the same part after the label, with data-trailing: same size, same gap. */
    icon: [
      "inline-flex shrink-0 items-center justify-center [&>[data-slot=icon]]:size-[1em] [&>svg]:size-[1em] [&:has(>:nth-child(2))]:inline-grid",
      /* two icons share the cell and cross-fade: the first shows until the button is swapped */
      "[&:has(>:nth-child(2))>*]:[grid-area:1/1] [&:has(>:nth-child(2))>*]:motion-opacity-base [&:has(>:nth-child(2))>*]:motion-filter-base",
      "[&>:nth-child(2)]:opacity-0 [&>:nth-child(2)]:[filter:blur(2px)]",
    ],
  },
  variants: {
    size: {
      xs: { base: "min-h-6 gap-1 px-1.5 py-0 text-xs rounded-shape-sm" },
      sm: { base: "min-h-7 gap-1.5 px-3 py-[7px] text-sm rounded-shape-sm" },
      md: { base: "min-h-8 gap-2 px-4 py-[9px] text-md" },
      lg: { base: "min-h-9 gap-2 px-[18px] py-[11px] text-md" },
    },
    /* --_join: what a filled look paints in the 1px between it and the next button of a button
     * group (its own fill, a little towards its text colour). The looks with a ring (outline)
     * or with nothing (ghost) leave it unset: the ring, or nothing, is their join. */
    variant: {
      /* an open outline button keeps the hover fill; hovering it still shows the hover */
      outline: { base: "bg-surface text-ink shadow-btn aria-expanded:bg-hover hover:bg-inset aria-expanded:hover:bg-inset" },
      primary: { base: ["bg-ink text-on-ink hover:opacity-90 [--_join:color-mix(in_oklab,var(--fndn-on-ink)_22%,var(--fndn-ink))]", FILL] },
      secondary: { base: "bg-hover-2 text-ink shadow-none hover:bg-line-strong [--_join:var(--fndn-line-strong)]" },
      accent: { base: ["bg-accent text-on-accent hover:bg-accent-ink [--_join:color-mix(in_oklab,var(--fndn-on-accent)_26%,var(--fndn-accent))]", FILL] },
      success: { base: ["bg-green text-on-accent hover:brightness-95 [--_join:color-mix(in_oklab,var(--fndn-on-accent)_26%,var(--fndn-green))]", FILL] },
      danger: { base: ["bg-red text-on-accent hover:brightness-95 [--_join:color-mix(in_oklab,var(--fndn-on-accent)_26%,var(--fndn-red))]", FILL] },
      /* the danger zone's button (GitHub's "Delete this repository"): the outline button's surface
       * with red text and a red edge; hovered it fills red, so the risk shows before the press. The
       * filled danger stays for the confirmation itself (the alert dialog's action). Like outline,
       * its edge is its join */
      "danger-outline": {
        base: [
          "bg-surface text-red shadow-[0_0_0_1px_color-mix(in_oklab,var(--fndn-red)_55%,transparent),0_1px_2px_0_oklch(0_0_0/0.05)]",
          "hover:bg-red hover:text-on-accent hover:shadow-[0_0_0_1px_var(--fndn-red),0_1px_2px_0_oklch(0_0_0/0.05)]",
        ],
      },
      /* transparent until hovered, for dense toolbars. It presses darker than it hovers, and an
       * open or current one keeps the darker fill, hovered or not. */
      ghost: { base: "bg-transparent text-ink shadow-none hover:bg-hover active:bg-hover-2 aria-expanded:bg-hover-2 aria-expanded:hover:bg-hover-2" },
    },
    /* radius only. Left out, the button takes the app's shape (rounded-shape, rounded-shape-sm up
     * to 28px): a pill, or a field's corners in a rounded app ("shape": "rounded" in the project's fndn.json).
     * Named, it is that shape whatever the app's. A pill square is round. */
    shape: {
      pill: { base: "rounded-pill" },
      rounded: { base: "rounded-control" },
    },
    /* icon-only: as wide as it is tall. An icon on its own reads a size larger than one next to a
     * label; --fndn-button-icon-size sets it from outside. */
    square: {
      true: { base: "size-8 p-0", icon: "text-[length:var(--fndn-button-icon-size,15px)]" },
    },
    /* two states in one cell (Copy / Copied): swapped, the first label and icon wait out of flow and
     * the second ones show; they cross-fade */
    swapped: {
      true: {
        label: [
          "[&>[data-slot=button-label]:first-child]:opacity-0 [&>[data-slot=button-label]:first-child]:[filter:blur(2px)]",
          "[&>[data-slot=button-label]:first-child]:absolute [&>[data-slot=button-label]:first-child]:top-0 [&>[data-slot=button-label]:first-child]:start-0 [&>[data-slot=button-label]:first-child]:whitespace-nowrap",
          "[&>[data-slot=button-label]:nth-child(2)]:static [&>[data-slot=button-label]:nth-child(2)]:opacity-100 [&>[data-slot=button-label]:nth-child(2)]:[filter:none]",
        ],
        icon: [
          "[&>:first-child:not(:only-child)]:opacity-0 [&>:first-child:not(:only-child)]:[filter:blur(2px)]",
          "[&>:nth-child(2)]:static [&>:nth-child(2)]:opacity-100 [&>:nth-child(2)]:[filter:none]",
        ],
      },
    },
    /* a spinner over what it replaces; the press does not scale (the click is dropped) */
    busy: {
      true: {
        base: "cursor-progress [:root:not([data-fndn-input=keyboard])_&:active]:scale-100",
      },
    },
    /* busy without a leading icon: the spinner is centred over the button, the label stays (hidden)
     * to hold the width */
    cover: {
      true: {
        base: "relative",
        label: "opacity-0",
        icon: "opacity-0",
      },
    },
    /* busy with a leading icon: the spinner shares the icon's cell, the glyph stays (hidden) */
    holding: {
      true: { icon: "inline-grid [&>*]:[grid-area:1/1] [&>:not([data-button=spinner])]:opacity-0" },
    },
    /* quiet at rest, ink when hovered, pressed or open. A label is text someone reads, so it rests
     * in ink-2; an icon alone (square) rests in ink-3 (below): the icon buttons of headers,
     * toolbars, fields */
    muted: {
      true: { base: "text-ink-2 hover:text-ink active:text-ink aria-expanded:text-ink" },
    },
  },
  compoundVariants: [
    { muted: true, square: true, class: { base: "text-ink-3" } },
    { square: true, size: "xs", class: { base: "size-6", icon: "text-[length:var(--fndn-button-icon-size,14px)]" } },
    { square: true, size: "sm", class: { base: "size-7" } },
    { square: true, size: "md", class: { icon: "text-[length:var(--fndn-button-icon-size,16px)]" } },
    { square: true, size: "lg", class: { base: "size-9", icon: "text-[length:var(--fndn-button-icon-size,18px)]" } },
    /* rounded takes the radius of a field of the same height (chip up to 28px, control from 32px),
     * so a small button and a small select in one row have the same corners */
    { shape: "rounded", size: ["xs", "sm"], class: { base: "rounded-chip" } },
  ],
  defaultVariants: { variant: "outline", size: "md" },
});

/* the busy spinner (part `spinner`, on the real Spinner): 1em of the button's text, in its ink (the
 * Spinner's own greys would vanish on a filled button); in the icon's cell, or centred over the
 * button (cover). Its own recipe, not a slot of `button`: toggle extends `button` and has no spinner. */
export const buttonSpinner = tv({
  base: "box-border size-[1em] border-[1.5px] border-current/25 border-t-current [grid-area:1/1]",
  variants: { cover: { true: "absolute inset-0 m-auto" } },
});

export type ButtonRecipeProps = VariantProps<typeof button>;
```
