Chip

Slant / Controls

A status chip with a live indicator, which can also be pressed, selected or removed.

Demo

Agent status

Usage

@tuttn/slant
import { Chip } from '@tuttn/slant'

// A status: a span with a leading indicator in the chip's tone.
<Chip tone="accent" indicator="dots">Working</Chip>
<Chip tone="warning" indicator="pulse">Waiting on you</Chip>
<Chip tone="danger" indicator="pixel">Stalled</Chip>
<Chip tone="success" indicator="check">Done</Chip>
<Chip>Queued</Chip>                  // neutral, no indicator

// With onClick it renders a <button type="button">.
<Chip tone={look.tone} indicator={look.indicator} onClick={advance}
  aria-label={`${label}. Change state`}>{label}</Chip>

// Selectable: a toggle button (aria-pressed) with a check while selected.
<Chip selected={design} onSelectedChange={setDesign}>Design</Chip>
<Chip defaultSelected>Web</Chip>     // uncontrolled

// Removable: a × button named "Remove urgent". Backspace and Delete remove too.
<Chip onRemove={() => setTags(tags.filter(tag => tag !== 'urgent'))}>urgent</Chip>
<Chip removeLabel="Retirer" onRemove={drop}>web</Chip>   // "Retirer web"

// Both: two sibling buttons in one span, the chip's own and the ×.
<Chip selected={ios} onSelectedChange={setIos} onRemove={removeIos}>iOS</Chip>

Props

PropTypeDefaultDescription
toneTone'neutral'The chip's colour: its text and indicator read the tone.
indicator'none' | 'dots' | 'pixel' | 'pulse' | 'check''none', or 'check' while a selectable chip is selectedA leading mark in the chip's colour: typing dots, a pixel, a pulsing pixel, or a check.
selectedboolean—Whether a selectable chip is selected. Passing this, `defaultSelected` or `onSelectedChange` makes the chip a toggle button (aria-pressed); leave `selected` out to let the chip keep its own state.
defaultSelectedboolean—The state an uncontrolled selectable chip starts in.
onSelectedChange(selected: boolean) => void—Called with the next state when a click, Space or Enter toggles a selectable chip.
onRemove() => void—Makes the chip removable: a small × button after its text, which calls this, as do Backspace and Delete on the chip's buttons.
removeLabelstringmessages.removeThe remove button's name, read before the chip's text ("Remove Design").
onClickMouseEventHandler—Makes the chip a <button type="button">. On a selectable chip it runs before the toggle; call preventDefault to keep the state.
…restButtonHTMLAttributes—className, style, aria-* and every other attribute go on the root, and so does the ref. The root is a <span>; with onClick or selectable, a <button>; removable, the <span> that holds the buttons, so there they don't reach the chip's own button.

Keyboard

Tabmoves to the chip's buttons: the chip itself when it has an onClick or is selectable, then its ×. A status chip is not focusable.
Enter / Spacepresses the focused button: runs onClick, selects or deselects, or removes
Backspace / Deleteon either of a removable chip's buttons, calls onRemove

Accessibility

  • Every indicator is decorative (aria-hidden): the dots, the pixel, the pulse and the check. The text says the state, so it reads the same without colour or motion.
  • Without a handler a chip is a <span> with no role, read in line. With onClick it is a native <button type="button">: focusable, pressed with Enter or Space, and it never submits a form.
  • A selectable chip is a toggle button: aria-pressed says whether it is selected, so keep its text the same in both states.
  • The × is named by aria-labelledby from a hidden "Remove" and the chip's text: "Remove urgent". removeLabel, or the root's messages.remove, translates the word.
  • When a chip removes itself, its button goes with it: move focus to the next chip, or to the field that adds tags. The demo focuses the next tag's ×, and Restore once none are left.
  • When the text alone doesn't say what pressing does, add an aria-label that starts with the visible text: "Working. Change state".
  • The dots and the pulse stop under prefers-reduced-motion: reduce.

Shapes

The props pick the element. With no handler a chip is a span, and onClick makes it a button. Any of selected, defaultSelected or onSelectedChange makes it a toggle button: selected, it takes the accent's soft fill and border and shows a check, unless you set an indicator. onRemove adds a × button after the text. A removable chip that is also clickable or selectable is a span holding two sibling buttons, the chip's own and the ×, so no button sits inside another. The ref always reaches the outermost element.

For a set of filters with one tab stop and arrow keys, use a ToggleGroup. For a static label, use a Badge.

Full source