# Chip

A small tag in three sizes: a status word, a count or a shortcut, a token someone put there, a filter, or a team's category in its hue.

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

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

## When to use

- A status word with its tone, a count, or the shortcut a control runs, at `sm` or `md`
- What someone put in the composer: a mode, a file or an attachment
- A filter in force over a list, filled when on and outlined when off
- A category a team makes, named and given one of six hues

## When not to use

- A plain action with nothing to remove: use `Button`
- An unread mark or a light for something new: use `StatusDot`
- A priority, which is a rank and never a hue: use `PriorityMark`

## In context

Remove a file from the context, then restore the set.

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

function Context() {
  const initial = ["dialog.tsx", "settings.css", "README.md"];
  const [files, setFiles] = useState(initial);
  const tokens = useRef<HTMLDivElement>(null);
  const restore = useRef<HTMLButtonElement>(null);
  const removeFile = (file: string, index: number) => {
    setFiles((current) => current.filter((name) => name !== file));
    requestAnimationFrame(() => {
      const remaining = tokens.current?.querySelectorAll("button");
      const next = remaining?.[index] ?? remaining?.[index - 1] ?? restore.current;
      next?.focus();
    });
  };
  return (
    <div {...sx("chip-filters", page.over)}>
      <div ref={tokens} role="group" aria-label="Context files" {...sx("chip-filters-group", page.line)}>
        {files.map((file, index) => (
          <Chip key={file} icon="file-code" removeLabel={"Remove " + file} onRemove={() => removeFile(file, index)}>
            {file}
          </Chip>
        ))}
      </div>
      <div {...sx("chip-filters-line", page.line)}>
        <span role="status" {...sx("cmp-result", cmp.result)}>{files.length === 0 ? "No files attached" : "Files attached: " + files.length}</span>
        <Button ref={restore} size="sm" variant="ghost" onClick={() => setFiles(initial)}>Restore files</Button>
      </div>
    </div>
  );
}
```

## Usage

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

<Chip icon="paperclip" onRemove={remove}>
  screenshot.png
</Chip>
```

By default a chip has a raised fill and a hairline edge. Three sizes, each a Caption pair: `lg` (24px, the default) for a token someone put there, `md` (20px) for a status or a count, `sm` (16px) for row meta, a key or a word. With `onRemove` a token ends in a 20px remove button in its ink. A chip stops at its container's edge and at `--chip-max-w`: a label with no room ends with an ellipsis.

## Examples

### Status words and counts

At `sm` and `md` a chip only states something. `variant="outline"` gives a neutral or muted one a hairline and no ground; a toned one keeps its wash. A lone count is at least as wide as it is tall.

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

<Chip size="md" variant="outline">Draft</Chip>
<Chip size="md" variant="outline" tone="muted">Archived</Chip>
<Chip size="md" tone="attention">New</Chip>
<Chip size="md" tone="success" icon="check">Passed</Chip>
<Chip size="md" tone="warning">Slow</Chip>
<Chip size="md" tone="danger">3 failing</Chip>
```

```tsx
import { Chip, ListRow } from "@arcadia/ui";

<div {...sx("cmp-rows", cmp.rows)}>
  <ListRow leading="new-chat" label="New chat" meta={<Chip size="sm" variant="outline">⌘N</Chip>} />
  <ListRow leading="todo" label="Reviews" meta={<Chip size="sm" variant="outline">3</Chip>} />
  <ListRow leading="info" label="Release notes" meta={<Chip size="sm" tone="attention">New</Chip>} />
  <ListRow leading="terminal" label="Nightly build" meta={<Chip size="sm" tone="success" icon="check">Passed</Chip>} />
</div>
```

### Tones

Pick by what the chip says. `attention` is the fixed amber light, for something new; `accent` is the theme colour. A mode takes its mode's tone (`plan`, `ask`, `debug`); a context chip stays `neutral`, or `muted` once it no longer applies.

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

<Chip icon="file-code">Neutral</Chip>
<Chip tone="muted" icon="file">Muted</Chip>
<Chip tone="accent" icon="plan">Accent</Chip>
<Chip tone="attention">New</Chip>
<Chip tone="plan" icon="plan">Plan</Chip>
<Chip tone="ask" icon="ask">Ask</Chip>
<Chip tone="debug" icon="debug">Debug</Chip>
<Chip tone="danger" icon="alert">Too large</Chip>
```

### Outline

Use `variant="outline"` for a faint hairline and no fill on a neutral or muted chip.

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

<Chip variant="outline" icon="calendar" onRemove={remove} removeLabel="Remove Late">Late</Chip>
<Chip variant="outline" icon="file-code">dialog.tsx</Chip>
```

### Labels

`hue` makes a chip a team's category: one of `LABEL_HUES` by id (`chart-1` to `chart-6`) or `none`, a grey dot. The hue is only in the dot before the name, inside the outline, and a label keeps it as its series in a chart. Never a status tone, so a red label never reads as an error. [HuePicker](/components/color-picker#choosing-a-hue) chooses one.

Each named label sits beside its dot-only form. The dot keeps the name as a hint.

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

function Labels() {
  const [shown, setShown] = useState(TEAM);
  return (
    <div {...stylex.props(page.row)}>
      {shown.map((l) => (
        <div key={l.name} {...stylex.props(page.pair)}>
          <Chip hue={l.hue} onRemove={() => setShown((s) => s.filter((x) => x !== l))}>{l.name}</Chip>
          <Chip hue={l.hue} dotOnly>{l.name}</Chip>
        </div>
      ))}
    </div>
  );
}
```

### Filters that are on or off

With `pressed` a chip is a toggle button: off, the outline with no ground; on, the filled ground in the primary ink. The form says which, so it reads in grey too.

```tsx
import { useState } from "react";
import { Chip, Input } from "@arcadia/ui";

function Filters() {
  const [on, setOn] = useState<ReadonlySet<string>>(() => new Set(["Crew"]));
  const [unused, setUnused] = useState(false);
  const [query, setQuery] = useState("");
  const turn = (p: string, pressed: boolean) => setOn((was) => { const s = new Set(was); if (pressed) s.add(p); else s.delete(p); return s; });
  const shown = AGENTS.filter((a) => (!on.size || a.products.some((p) => on.has(p))) && (!unused || a.unused) && a.name.includes(query.trim().toLowerCase())).length;
  return (
    <div {...sx("chip-filters", page.over)}>
      <Input size="md" type="search" leadingIcon="search" placeholder="Search by name" aria-label="Search agents" value={query} onChange={(e) => setQuery(e.target.value)} />
      <div {...sx("chip-filters-line", page.line)}>
        <div role="group" aria-label="Filters" {...sx("chip-filters-group", page.line)}>
          {PRODUCTS.map((p) => (
            <Chip key={p} pressed={on.has(p)} onPressedChange={(next) => turn(p, next)} count={AGENTS.filter((a) => a.products.includes(p)).length}>{p}</Chip>
          ))}
          <Chip pressed={unused} onPressedChange={setUnused} count={AGENTS.filter((a) => a.unused).length}>Unused</Chip>
        </div>
        <span aria-live="polite" {...sx("chip-filters-count", page.count)}>{`${shown} of ${AGENTS.length} agents`}</span>
      </div>
    </div>
  );
}
```

A row of them sits over the list it narrows, in a group named for what they filter, and the count of what shows ends the line. `count` puts how many each holds after its label in the quietest ink, heard after the name ("Crew, 2").

### Interactive

For a plain action, put a chip in a bare Button. The chip cannot have its own remove button.

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

<Button variant="bare" onClick={open}>
  <Chip icon="paperclip">screenshot.png</Chip>
</Button>
```

### In a toolbar

`weight="bold"` sets the label at 600. The glyph keeps the Caption icon's box and regular weight.

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

function InToolbar() {
  const [on, setOn] = useState(true);
  return (
    <>
      <IconButton icon="plus" label="Add context" variant="secondary" />
      <Chip icon="multitask" pressed={on} onPressedChange={setOn} weight="bold">Multitask</Chip>
      <IconButton icon="mic" label="Dictate" variant="ghost" tone="muted" />
    </>
  );
}
```

## Matrix

Tone and kind

Rows: Neutral, Muted, Accent, Attention, Success, Warning, Danger, Plan, Ask, Debug. Columns: Label, Icon, Removable, Remove hover, Remove focus.

Size

Rows: Neutral, Accent. Columns: Word, Count, Token.

## Customising

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

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

const styles = stylex.create({
  // a context chip that no longer applies, pushed to the end of its row
  stale: { "--chip-bg": "var(--fill-5)", marginInlineStart: "auto" },
});

<Chip icon="file" rootStyle={styles.stale}>old-plan.md</Chip>
```

- `--chip-bg` (`Paint`, default `var(--bg-raised)`): Fill (the tone's wash).
- `--chip-ring` (`Paint`, default `var(--float-edge)`): The hairline inside the edge.
- `--chip-fg` (`Paint`, default `var(--text-2)`): Label colour, and the remove button's.
- `--chip-icon` (`Paint`, default `var(--icon-2)`): Leading glyph colour.
- `--chip-max-w` (`Ref<Measure>`, default `240px`): Width past which the label shortens or truncates.
- `--chip-h` (`Ref<Control | Space>`, default `var(--control-sm)`): Height, and the least width of a short one at `sm` and `md`.
- `--chip-px` (`Ref<Space>`, default `var(--space-2)`): Inline padding.
- `--chip-radius` (`Ref<Radius>`, default `var(--radius-round)`): Corner radius: round for a label, nested control corners for a removable chip.

## Accessibility

A chip is a `<span>` read as part of what it sits in; only a remove button, a `pressed` toggle or its host button takes focus. Name the remove button after what it removes ("Remove dialog.tsx"); a label's says "Remove" and its name. A lone count reads as "3", so name it in its row. A shortcut beside a control is decoration: put it on the control with `aria-keyshortcuts`.

Removing a chip takes its button out from under focus, so move focus somewhere sensible. A toggle is one Tab stop, Space or Return turns it, and a reader hears whether it is pressed. A label's dot alone is an image named by its name. Under forced colours an outlined chip keeps its edge, a filter that is on stands on the system's highlight and a label's dot keeps its hue. Its height is a least height, so a person's own text spacing grows it.

## API reference

### Chip

- `variant` (`"secondary" | "outline"`, default `"secondary"`): `outline` takes a neutral or muted chip's ground away for a hairline inside its edge (a status word, a filter in force). A toned chip keeps its wash.
- `icon` (`IconName | ReactElement`): Leading glyph: a registry name, or an element in the glyph's box.
- `swap` (`boolean`): On a chip that is a control (`render` a button): `icon` follows its state, a new one swapping in place of the last (`IconSwap`).
- `hue` (`LabelHue`): A category a team makes: one of `LABEL_HUES` by id, or `none`. An outlined chip, its hue only in the dot before the name.
- `dotOnly` (`boolean`): The dot alone, where the room has gone: the name is its accessible name and its hint.
- `tone` (`"neutral" | "muted" | "accent" | "attention" | "success" | "warning" | "danger" | "plan" | "ask" | "debug"`): Mode tones (plan / ask / debug) tint at --mode-soft-strength. `attention` is the fixed amber light ("New"); `accent` the theme colour.
- `size` (`"sm" | "md" | "lg"`, default `"lg"`)
- `weight` (`"regular" | "bold"`, default `"regular"`)
- `onRemove` (`() => void`): Renders a trailing `xs` IconButton in the chip's ink.
- `revealRemoveOnInteraction` (`boolean`)
- `removeLabel` (`string`, default `"Remove"`): The remove button's label and tooltip. Or "Remove" and the label's name.
- `removeHint` (`ReactNode`): Extra tooltip line for the remove button, e.g. the shortcut's effect.
- `pressed` (`boolean`): A filter that is on or off (a row of quiet filters above a list): the chip is a toggle button, `aria-pressed`. Off, an outline with no ground; on, the filled ground and the primary ink: the form says it, not a colour.
- `count` (`number`): How many it holds, after the label in the quietest ink with tabular figures, as a `Tab` takes it: seen "New 3", heard "New, 3".
- `onPressedChange` (`(pressed: boolean) => void`): With `pressed`: told the state a press asks for.
- `rootStyle` (`ChipStyle`): Typed StyleX override: where the chip sits and its `--chip-*` knobs.
- `children` (`string`): Its name.

Rest props go to `<span>` props and `render`.
