# Button

The text button: six variants that only reassign tokens, four sizes in three text styles, and loading and toggle states.

Live variants: https://design.arcadia.is/components/button

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

## In context

Archive a draft, then undo the change.

```tsx
import { useState } from "react";
import { Button } from "@arcadia/ui";

function Demo() {
  const [archived, setArchived] = useState(false);
  return (
    <div {...sx("cmp-bar", cmp.bar)}>
      <span {...sx("cmp-result", cmp.result)} role="status">{archived ? "Draft archived" : "Launch notes draft"}</span>
      <Button variant={archived ? "secondary" : "primary"} onClick={() => setArchived(!archived)}>
        {archived ? "Undo" : "Archive draft"}
      </Button>
    </div>
  );
}
```

## When to use

- Move a flow forward with send, save or start.
- Use ghost or secondary for repeated actions.
- Confirm before committing a destructive action.

## When not to use

- Use a link for navigation.
- Use a labelled `IconButton` for a glyph-only action.
- Use `SegmentedControl` to pick one of a few options.

## Usage

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

<Button variant="primary" icon="arrow-up" onClick={send}>
  Send
</Button>
```

Reserve `primary` for the one action that moves the flow forward.

Buttons use the compact Body profile by default. Choose `density="large"` for the Headline profile. An explicit `size` without density uses the size ladder.

Use `icon` and `iconEnd` for leading and trailing icons.

## Examples

### Density

Compact by default

Large

### Variants

Compare emphasis before choosing a variant for an action.

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

<Button variant="primary">Primary</Button>
<Button variant="secondary">Secondary</Button>
<Button variant="ghost">Ghost</Button>
<Button variant="text">Text</Button>
<Button variant="danger" icon="trash">Delete</Button>
```

### Bare

Use `bare` when an action should read like surrounding text.

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

<div {...sx("cmp-bare", s.bare)}>
  <Button variant="bare">Nightly dependency check</Button>
  <p {...sx("cmp-bare-line", s.bareLine)}>
    <Button variant="bare" icon="file">Read 3 files in src/lib</Button>
  </p>
</div>
```

### States

Try saving or toggling without shifting the button’s width.

```tsx
import { useState } from "react";
import { Button } from "@arcadia/ui";

function States() {
  const [saving, setSaving] = useState(false);
  const [sidebar, setSidebar] = useState(true);
  const save = () => {
    setSaving(true);
    setTimeout(() => setSaving(false), 1500);
  };
  return (
    <>
      <Button variant="secondary" loading={saving} onClick={save}>
        Save changes
      </Button>
      <Button icon="sidebar" pressed={sidebar} onClick={() => setSidebar(!sidebar)}>
        Sidebar
      </Button>
      <Button variant="secondary" disabled focusableWhenDisabled>
        Answer 2 more
      </Button>
      <Button variant="secondary" size="sm" progress={0.42}>
        Updating 42%
      </Button>
    </>
  );
}
```

### A glyph that follows its state

Copy a link or check for updates to see the icon change.

```tsx
import { useState } from "react";
import { Button, CopyButton } from "@arcadia/ui";

function Swaps() {
  const [checking, setChecking] = useState(false);
  const check = () => {
    setChecking(true);
    setTimeout(() => setChecking(false), 1600);
  };
  return (
    <>
      <CopyButton text="https://design.arcadia.is/components/button" label="Copy link" />
      <Button icon="refresh" loading={checking} onClick={check}>
        Check for updates
      </Button>
    </>
  );
}
```

### Press

Hold either button to see feedback without shifting its neighbours.

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

<Button variant="primary" size="lg" icon="arrow-up">Send</Button>
<Button variant="secondary" size="lg">Hold it down</Button>
```

### A label that gives way

Shrink the available space. The leading icon stays with the first line.

```tsx
import { useState } from "react";
import { Slider, Button } from "@arcadia/ui";

function LongLabel() {
  const [w, setW] = useState(100);
  return (
    <div {...stylex.props(s.frame)}>
      <span {...stylex.props(s.room)} style={{ width: `${w}%` }}>
        <Button variant="secondary" icon="refresh" wrap rootStyle={s.fit}>
          Continue on the large model
        </Button>
      </span>
      <Slider label="Room for the button" min={20} value={w} onValueChange={setW} valueText={(value) => `${value}%`} rootStyle={cmp.scrubSlider} />
    </div>
  );
}
```

### Wrapped label

Wrap a longer label with its trailing icon on the first line.

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

<Button variant="secondary" wrap iconEnd="chevron-down">
  Choose which projects can use this integration
</Button>
```

### Round ends

Compare the extra room round ends give to text, not icons.

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

<Button variant="secondary" roundEnds rootStyle={ends.pill}>Continue</Button>
<Button variant="secondary" roundEnds icon="plus" rootStyle={ends.pill}>New chat</Button>
```

### A keycap at an end

Put a keycap at either end without giving up icon spacing.

```tsx
import { Button, Kbd } from "@arcadia/ui";

<Button size="sm" variant="secondary" icon="check">Current</Button>
<Button size="sm" variant="secondary"><Kbd keys="1" /> Current</Button>
<Button size="sm" variant="secondary">Proposed <Kbd keys="2" /></Button>
```

```tsx
import { Button, Kbd } from "@arcadia/ui";

<Button variant="secondary" size="lg"><Kbd keys="1" /> Current</Button>
<Button variant="secondary" size="lg"><Kbd keys="2" /> Proposed</Button>
```

### As a link

Keep link semantics with `render` and `nativeButton={false}`.

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

<Button
  variant="secondary"
  iconEnd="chevron-right"
  nativeButton={false}
  render={<a href="/foundations/motion" />}
>
  Motion guide
</Button>
```

## Matrix

Variant and state

Rows: Primary, Secondary, Ghost, Text, Danger, Bare. Columns: Rest, Hover, Press, Focus, Toggled, Disabled, Loading.

Size and kind

Rows: Extra small, Small, Medium, Large. Columns: Ghost with icon, Secondary, Secondary with hint, Primary with end icon.

Height and corner, with round ends

Rows: 24 tall, corner 8, 24 tall, corner R/2, 24 tall, corner 3R/4, 24 tall, corner pill, 32 tall, corner 8, 32 tall, corner R/2, 32 tall, corner 3R/4, 32 tall, corner pill, 40 tall, corner 8, 40 tall, corner R/2, 40 tall, corner 3R/4, 40 tall, corner pill. Columns: Label, Lead icon, Trail icon, Both icons.

## Customising

Set layout and override the button’s documented knobs.

```tsx
import * as stylex from "@stylexjs/stylex";

const styles = stylex.create({
  skip: { "--button-px": "var(--space-0)", marginInlineStart: "auto" },
});

<Button variant="text" rootStyle={styles.skip}>Skip</Button>
```

- `--button-h` (`Ref<Control> | Derived`, default `var(--control-md)`): Height.
- `--button-px` (`Ref<Space>`, default `var(--button-inset, var(--space-3-5))`): Inline padding on a side that ends in text. An icon's side stands closer to its edge than this on purpose: the glyph's drawing sits inside its box, so less padding there makes the two ends look equal; a small button beside an icon takes 10 here, from the 12 of its text alone. A keycap (a Kbd chip at either end) stands in that slot too, so its side takes the icon inset and never this.
- `--button-radius` (`Ref<Radius> | Derived`): Corner radius (8, or the inset step 6 inside a card). A larger one gives each text side its round end's room (R12).
- `--button-gap` (`Ref<Space>`, default `var(--space-1-5)`): Gap between icon, label and hint.
- `--button-bg` (`Paint`, default `transparent`): Fill at rest.
- `--button-bg-hover` (`Paint`, default `var(--fill-4)`): Fill under the pointer, or while its popup is open.
- `--button-bg-active` (`Paint`, default `var(--button-bg-hover)`): Fill while pressed.
- `--button-bg-pressed` (`Paint`, default `var(--fill-3)`): Fill when toggled on (`pressed`).
- `--button-bg-disabled` (`Paint`, default `transparent`): Fill when disabled.
- `--button-fg` (`Paint`, default `var(--text-2)`): Label and icon colour at rest.
- `--button-fg-hover` (`Paint`, default `var(--text-1)`): Label colour under the pointer.
- `--button-fg-pressed` (`Paint`, default `var(--text-1)`): Label colour when toggled on.
- `--button-fg-disabled` (`Paint`, default `var(--text-4)`): Label colour when disabled.
- `--button-border` (`Paint`, default `transparent`): Border colour (1px) at rest.
- `--button-border-hover` (`Paint`, default `var(--button-border)`): Border colour under the pointer.
- `--button-hint` (`Paint`, default `var(--text-3)`): Key hint ink.
- `--button-hint-disabled` (`Paint`, default `var(--button-fg-disabled)`): Key hint ink when disabled.

Set hover, focus and disabled colours independently.

## Accessibility

A native `<button>` by default. `loading` sets `aria-busy` and blocks activation without dropping focus; `pressed` sets `aria-pressed`, and under forced colours, which drop every fill, a button that is on stands on the system's highlight. Keyboard focus draws the 2px ring, outset by 2px. Its height is a least height, so a person's own text spacing grows it instead of clipping the label.

A `hint` is drawn only: it is `aria-hidden` and becomes `aria-keyshortcuts` (`"⇧Tab"` → `Shift+Tab`, `"⏎"` → `Enter`), so the accessible name is the label alone. Binding the key is yours.

- Enter: Activates the button
- Space: Activates the button

## API reference

### Button

- `variant` (`"primary" | "secondary" | "ghost" | "text" | "danger" | "bare"`, default `"ghost"`): `primary` is the monochrome ink CTA, for the one action that moves the flow forward (send). `bare` is a row or a line of text that presses: no fill, frame, padding or height of its own, in the text style and ink of the line it stands in, its label on its capitals, its ink a step up under the pointer.
- `size` (`"xs" | "sm" | "md" | "lg"`, default `"md"`): Heights 20 / 24 / 28 / 32: `xs` and `sm` stand in a Caption pair, `md` in Body, `lg` in Headline, all bold.
- `tone` (`"neutral"`)
- `density` (`"compact" | "large"`)
- `icon` (`IconName`): Leading icon. While `loading`, a spinner takes its slot so the width never changes.
- `swap` (`boolean`): `icon` or `iconEnd` follows the button's state (play to pause, copy to check). A new glyph swaps in place of the last in the icon's box (`IconSwap`). Leave it off where the glyph changes on every hover or tab switch, where it changes in one frame.
- `iconEnd` (`IconName`): Trailing icon. Beside a wrapped text label, it sits on the first line.
- `hint` (`ShortcutSpec | ReactElement`): Trailing key hint, drawn quietly: `"⇧Tab"`, `"⏎"`, `["⌘","K"]`, `"mod+k"`, or a `Kbd`. It stands at the end of a button wider than its words (a search trigger), the label taking the room between. Visual only (`aria-hidden`, so the name stays "Allow", not "Allow ⏎"); the keys are exposed as `aria-keyshortcuts` on the button. Binding the key is yours.
- `loading` (`boolean`, default `false`): Busy: `aria-busy`, a spinner, and activation blocked while focus is kept (no native disabled).
- `progress` (`number`): Determinate progress, 0..1: a ring fills in the icon's slot (a download, an upload) while the label says the number. The button stays pressable; `loading` wins when both are set.
- `pressed` (`boolean`): Toggle state: sets `aria-pressed` and `data-pressed`. Omit for a plain action button.
- `labelRow` (`boolean`): The label is a row of parts (an avatar beside words), each on its capitals.
- `wrap` (`boolean`, default `false`): Its label wraps onto more lines instead of running past the button, growing it: a choice whose words are longer than a narrow column. One line keeps the control's height.
- `roundEnds` (`boolean`): Each side that ends in text takes the room its round end cuts (R12) under a pill's corner too. A pill always takes it.
- `rootStyle` (`ButtonStyle`): Typed StyleX override: where the button sits (margins, self-alignment, flex and grid placement, size bounds) and its `--button-*` knobs.
- `render` (`RenderProp<{ disabled: boolean }>`): Replace the rendered element, e.g. `render={<a href="/settings" />}`, or a function of props and state.
- `disabled` (`boolean`): Native disabled: no pointer events, no tooltip, skipped by Tab.
- `focusableWhenDisabled` (`boolean`): Keep a disabled control focusable and hoverable (`aria-disabled`), so a tooltip can say why.
- `nativeButton` (`boolean`): Button semantics, inferred from what renders: a `<button>` is native, an `<a>` stays a link, anything else gets `role="button"` and key handling. `false` forces button semantics on whatever renders (a link acting as a button).

Rest props go to `<button>` props.
