# Icon button

An icon-only button with a required `label` that becomes its `aria-label` and tooltip; as a toggle, its glyph filled while on; a glyph that follows its state swaps in its box.

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

```tsx
import { IconButton, IconSwap } from "@arcadia/ui";
```

## When to use

- An action shown only as a glyph, with a label naming what it does
- A bar of repeated actions, every glyph in the bar's one ink
- A toggle for one thing, drawing the glyph's filled form while it is on
- A canvas tool, drawn with one of the Canvas toolbar's silhouettes
- The trigger of a menu or another popup, through `render`

## When not to use

- An action with words beside its glyph: use `Button`
- A choice between two to five visible options: use `SegmentedControl`
- A destination: use a link, never a button that navigates

## In context

Copy the project path or toggle its star. Both actions keep their icon box.

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

function MessageActionsDemo() {
  const [starred, setStarred] = useState(false);
  return (
    <Toolbar aria-label="Project actions" rootStyle={cmp.toolbarCompact} leading={
      <div {...sx("cmp-toolbar", cmp.toolbar, page.actions)}>
        <CopyButton iconOnly text="arcadia/design" label="Copy repository path" />
        <IconButton icon="star" label="Star project" pressed={starred} onClick={() => setStarred(!starred)} />
      </div>
    } />
  );
}
```

## Usage

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

<IconButton icon="new-chat" label="New chat" shortcut="mod+n" onClick={newChat} />
```

`label` is required: it becomes the `aria-label` and the tooltip's title, because a tooltip is not a label. The default is `sm`: a 14px glyph in a 24px target. Compose it as a popup trigger with `render`: `<MenuTrigger render={<IconButton icon="more" label="More" />} />`. A bar sets its icons' one ink, `--ib-ink`, and every icon button in it takes it: no glyph in a bar is tinted apart, on or off, the dots of `more` included, so a button in a bar takes no `tone`. A button draws one glyph: `icon` from the kit's set, or `glyph` from the Canvas toolbar's silhouettes, never both, and a toolbar glyph is drawn at the icon's own size and has no filled form.

## Examples

### Tooltips

Hover or focus: the title, a glyph shortcut as keys, and an optional subtitle. Use `tooltip={false}` when the label is already visible beside it.

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

<IconButton icon="settings" label="Settings" shortcut="⌘," />
<IconButton
  icon="plus"
  label="Add context"
  variant="secondary"
  tooltip={{ subtitle: "Files, images and modes" }}
/>
<IconButton icon="sidebar" label="Toggle sidebar" tooltip={false} />
```

### Toggle

`pressed` sets `aria-pressed`, and while on the button draws its glyph's filled form, in the same ink on the same ground: only the form changes. It takes only a glyph that has a filled form (`FilledIconName`).

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

function Toggle() {
  const [fast, setFast] = useState(true);
  const [projects, setProjects] = useState(false);
  return (
    <>
      <IconButton icon="zap" label="Fast mode" pressed={fast} onClick={() => setFast(!fast)} />
      <IconButton icon="folder" label="Projects" pressed={projects} onClick={() => setProjects(!projects)} />
    </>
  );
}
```

### A toolbar glyph

A Canvas tool draws one of the toolbar's silhouettes in place of the kit's icon: it takes glyph and no icon, and is drawn at the icon's own size, so it changes with the text style round it. Five of the thirteen are here, the last one on.

A pressed toolbar glyph lights its ground alone: none of them has a filled form.

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

function Glyphs() {
  const [on, setOn] = useState(false);
  return (
    <>
      <IconButton glyph="cursor" label="Select" shortcut="V" />
      <IconButton glyph="chat-centered-text" label="Comment" shortcut="C" />
      <IconButton glyph="pencil-simple" label="Draw" shortcut="P" />
      <IconButton glyph="scan" label="X-ray" shortcut="G" />
      <IconButton glyph="eraser" label="Eraser" pressed={on} onClick={() => setOn(!on)} />
    </>
  );
}
```

### A pair that swaps

A pair of glyphs that says both states swaps with its label and never fills: it takes `swap` and no `pressed`.

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

function Pairs() {
  const [shown, setShown] = useState(false);
  const [pane, setPane] = useState(true);
  const [large, setLarge] = useState(false);
  return (
    <>
      <IconButton swap icon={shown ? "eye-off" : "eye"} label={shown ? "Hide key" : "Show key"} onClick={() => setShown(!shown)} />
      <IconButton swap icon={pane ? "panel-right-close" : "panel-right"} label={pane ? "Close the side panel" : "Open the side panel"} onClick={() => setPane(!pane)} />
      <IconButton swap icon={large ? "collapse" : "expand"} label={large ? "Collapse" : "Expand"} onClick={() => setLarge(!large)} />
    </>
  );
}
```

### A glyph that follows its state

With `swap`, a new `icon` replaces the last in its box: the glyph leaving fades, blurs and shrinks as the one arriving comes back, over 250ms. The button keeps its size, its focus and its press.

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

function Swaps() {
  const [loading, setLoading] = useState(false);
  const [running, setRunning] = useState(false);
  const [checking, setChecking] = useState(false);
  useEffect(() => {
    if (!loading) return;
    const t = setTimeout(() => setLoading(false), 2400);
    return () => clearTimeout(t);
  }, [loading]);
  useEffect(() => {
    if (!checking) return;
    const t = setTimeout(() => setChecking(false), 1600);
    return () => clearTimeout(t);
  }, [checking]);
  return (
    <>
      <IconButton swap icon={loading ? "stop" : "refresh"} label={loading ? "Stop" : "Reload"} onClick={() => setLoading(!loading)} />
      <CopyButton text="make test" label="Copy command" iconOnly variant="ghost" />
      <IconButton swap icon={running ? "stop" : "arrow-up"} label={running ? "Stop" : "Send"} variant="inverse" onClick={() => setRunning(!running)} />
      <IconButton icon="refresh" label="Check for updates" loading={checking} onClick={() => setChecking(true)} />
    </>
  );
}
```

### A menu's trigger

A button that opens a menu is no toggle. While the menu is open it shows its ground, and its glyph stays an outline; `aria-expanded` lights it the same way.

```tsx
import {
  IconButton, Menu, MenuContent, MenuItem, MenuTrigger,
} from "@arcadia/ui";

<Menu>
  <MenuTrigger render={<IconButton icon="more" label="Chat actions" />} />
  <MenuContent align="end">
    <MenuItem icon="pencil">Rename…</MenuItem>
    <MenuItem icon="trash" tone="danger">Delete</MenuItem>
  </MenuContent>
</Menu>
```

### Send at rest and disabled

Use the outlined `secondary` for Add context. Use `inverse` for Send, including when it is disabled.

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

<IconButton icon="plus" label="Add context" variant="secondary" tooltip={false} />
<IconButton icon="arrow-up" label="Send" variant="inverse" disabled tooltip={false} />
<IconButton icon="arrow-up" label="Send" variant="inverse" tooltip={false} />
```

### Busy, stop and send

`loading` swaps the glyph for a spinner in the same box and keeps focus; the `inverse` disc is send, and stop while a turn runs.

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

<IconButton icon="refresh" label="Refresh" loading />
<IconButton icon="stop" label="Stop" variant="inverse" />
```

## Matrix

Toggle, off and on

Rows: Fast mode, Projects, Agents, Contributor, New chat, Urgent, Favorite. Columns: Off, On, On, hover, On, focus, On, disabled.

Toggle and size

Rows: Extra small, Small, Medium, Large. Columns: Fast mode, Projects, Agents, Contributor, New chat, Urgent, Favorite.

Variant and state

Rows: Ghost, Ghost, muted, Secondary, Inverse. Columns: Rest, Hover, Press, Focus, Menu open, Disabled, Loading.

Size and variant

Rows: Extra small, Small, Medium, Large. Columns: Ghost, Ghost, muted, Secondary, Inverse.

## Customising

`rootStyle` takes where the button sits and the `--ib-*` knobs below. [Theming](/foundations/theming) says what else a product may change.

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

const styles = stylex.create({
  // a copy button that turns success-green once it has copied
  copied: { "--ib-fg": "var(--text-success)", "--ib-fg-hover": "var(--text-success)" },
  // a bar's one ink: every icon button in it takes it, on or off
  actions: { "--ib-ink": "var(--icon-3)" },
});

<IconButton icon="check" label="Copied" tone="muted" rootStyle={styles.copied} />
<div {...stylex.props(styles.actions)}>
  <IconButton icon="copy" label="Copy message" />
  <IconButton icon="more" label="More actions" />
</div>
```

- `--ib-size` (`Ref<Control>`, default `var(--control-sm)`): Box width and height: a control height, its pair's icon and the spacing round it.
- `--ib-radius` (`Ref<Radius>`): Corner radius (8, or the inset step 6 inside a card).
- `--ib-bg` (`Paint`, default `transparent`): Fill at rest.
- `--ib-bg-hover` (`Paint`, default `var(--fill-4)`): Fill under the pointer.
- `--ib-bg-open` (`Paint`, default `var(--ib-bg-hover)`): Fill while what it opens is open (`aria-expanded`).
- `--ib-bg-disabled` (`Paint`, default `transparent`): Fill when disabled.
- `--ib-fg` (`Paint`, default `var(--ib-ink, var(--icon-2))`): Glyph colour at rest: its bar's `--ib-ink` where one is set.
- `--ib-fg-hover` (`Paint`, default `var(--icon-1)`): Glyph colour under the pointer.
- `--ib-fg-open` (`Paint`, default `var(--ib-fg-hover)`): Glyph colour while what it opens is open.
- `--ib-fg-disabled` (`Paint`, default `var(--icon-4)`): Glyph colour when disabled.
- `--ib-border` (`Paint`, default `transparent`): A 1px inset ring. Default transparent.

Variants, tones and sizes only reassign knobs, and each state reads its own (`--ib-bg-hover`, `--ib-fg-disabled`), so a knob you set never drops a state. The `--ib-*-open` pair is a trigger's while what it opens is open; a toggle that is on keeps its rest pair. A bar sets `--ib-ink` on itself rather than `--ib-fg` on each button.

## Accessibility

`steady` keeps the button still under a press. Use it when a badge sits outside the button.

`label` is the whole accessible name. `shortcut` shows in the tooltip and is exposed as `aria-keyshortcuts` (`"⌘N"` reads as `Meta+N`), never as part of the name; binding the key is yours. Tooltips open after 350ms, and once one has shown, its neighbours open instantly for 200ms.

A toggle's label names what it turns on and stays the same, on or off ("Fast mode"): `pressed` is heard as pressed or not, and seen as the glyph's form, never by its colour alone. A pair that swaps says its action instead ("Show key", then "Hide key") and carries no `aria-pressed`. With `swap` the glyphs are drawing only: the label is the name and follows the state ("Reload", then "Stop"), the button keeps its focus, and the glyph leaving is hidden from a reader from its first frame. Under reduced motion the swap is one frame. `IconSwap` is the swap itself, for a kit part's slot that takes an element. A menu's trigger says `aria-haspopup` and `aria-expanded`; under forced colours, which drop every fill, its open ground is the system's highlight, and a toggle that is on keeps its filled glyph. A toolbar glyph has no filled form, so a pressed one lights its ground alone and says it in `aria-pressed` only; where a row of them is one tool in hand, the row is one group of choices, each heard as checked and the row one tab stop, as the canvas dock draws its tools.

A disabled icon button can't be focused, so its tooltip never shows. When it needs to say why, add `focusableWhenDisabled` so it stays focusable with `aria-disabled`.

- Enter + Space: Activate

## API reference

### IconButton

- `label` (`string`, required): Required: becomes `aria-label` and the tooltip title. A tooltip is not a label.
- `shortcut` (`string | readonly string[]`): Shown in the tooltip and exposed as `aria-keyshortcuts`: `"⌘N"`, `["⌘","N"]`, `"mod+n"`, `"⏎"`.
- `variant` (`"ghost" | "secondary" | "inverse"`, default `"ghost"`)
- `tone` (`"neutral" | "muted"`)
- `size` (`"xs" | "sm" | "md" | "lg"`, default `"sm"`): Boxes 20 / 24 / 28 / 32: the glyph boxes are 12 / 14 / 14 / 16 (Caption, Body, Body, Headline).
- `pressed` (`boolean`): Toggle state: `aria-pressed`; while it is on, a kit icon with a filled form draws that form and a toolbar glyph lights its ground.
- `swap` (`boolean`): `icon` follows the button's state (refresh to stop, show to hide, copy to check), its `label` with it: a new glyph swaps in place of the last in the same box (`IconSwap`). Leave it off where the glyph changes on every hover or tab switch: there it changes in one frame.
- `steady` (`boolean`): Keep the glyph and its hit target still when pressed.
- `loading` (`boolean`, default `false`): Busy: the glyph becomes a spinner, `aria-busy`, activation blocked, focus kept.
- `flush` (`boolean`): Negative margin of (box − glyph) / 2 so the glyph aligns optically with neighbouring text edges.
- `tooltip` (`false | IconButtonTooltipOptions`): Tooltip options, or `false` for none (e.g. when the label is already visible next to it).
- `rootStyle` (`IconButtonStyle`): Typed StyleX override: where the button sits and its `--ib-*` 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).
- `icon` (`FilledIconName | IconName`): The kit icon it draws. One with a filled form (`FilledIconName`) takes that form while the button is `pressed`; any icon may `swap` to the icon of its state instead, and then never reads as on.
- `glyph` (`ToolbarGlyphName`): A Canvas toolbar glyph in place of `icon`, at the icon's own size. None has a filled form, so a `pressed` button lights its ground alone, and it does not `swap`.

Rest props go to `<button>` props.

### IconSwap

- `icon` (`SwapGlyph`, required): The glyph for the control's state now. When it changes, the glyph leaving and the one arriving cross in one box, the icon's: a name is known by itself, an element by its `key`.
