# Chips

> Chips split by interaction - action chips are small Buttons, choice and filter chips a chip-variant ToggleButtonGroup, input chips a TagGroup.

Canonical: https://salmonui.dev/docs/buttons/chips

Chips are built from the building block that matches what the chips do.

| Chips | Build them with |
| --- | --- |
| Action chips (click handlers) | `Button size="sm"` (optionally `variant="chip"` or `"outline"`) |
| Choice chips (one selected) | `ToggleButtonGroup variant="chip"` |
| Filter chips (several selected) | `ToggleButtonGroup variant="chip" selectionMode="multiple"`; a pressed chip shows a check |
| Chips without selection | Chip-styled `Button`s in a `ButtonGroup attached={false}` |
| A single chip | A chip-styled `Button` or `ToggleButton` |
| Input chips (delete, add, drag) | [TagGroup](https://salmonui.dev/docs/buttons/tag-group) |

### Live demo: Choice, filter and action chips

Live demo: https://salmonui.dev/demos/buttons/chips

Single-selection choice chips, multiple-selection filter chips with a check mark, and action chips in a spaced button group.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { Button, ButtonGroup, ToggleButton, ToggleButtonGroup } from '@salmonui/buttons/button';

const CalendarIcon = () => (
  <svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" aria-hidden="true">
    <rect x="2.5" y="3.5" width="11" height="10" rx="1.5" />
    <path d="M2.5 6.5h11M5.5 2v3M10.5 2v3" />
  </svg>
);

const LABELS = ['Open', 'Assigned to me', 'Urgent', 'Has attachments'];

// Choice chips (single), filter chips (multiple, with a check) and action chips.
export default function ChipsDemo() {
  const [filters, setFilters] = useState<readonly string[]>(['Open']);
  return (
    <div style={{ display: 'grid', gap: 12 }}>
      <ToggleButtonGroup aria-label="Size" variant="chip" defaultValue="m">
        <ToggleButton value="s">Small</ToggleButton>
        <ToggleButton value="m">Medium</ToggleButton>
        <ToggleButton value="l">Large</ToggleButton>
      </ToggleButtonGroup>
      <ToggleButtonGroup aria-label="Filters" variant="chip" tone="primary" selectionMode="multiple" value={filters} onValueChange={setFilters}>
        {LABELS.map((label) => (
          <ToggleButton key={label} value={label}>
            {label}
          </ToggleButton>
        ))}
      </ToggleButtonGroup>
      <ButtonGroup aria-label="Suggested replies" variant="chip" attached={false}>
        <Button>Sounds good</Button>
        <Button>Thanks!</Button>
        <Button iconStart={<CalendarIcon />}>Schedule a call</Button>
      </ButtonGroup>
      <p style={{ margin: 0 }}>Active filters: {filters.join(', ') || 'none'}</p>
    </div>
  );
}
```

## Data, content and state

- Each chip is a `ToggleButton` child with its text and a `value`; map your
  data in JSX.
- The selection is `value` / `defaultValue`, by value only, since indexes
  are not stable.
- Avatars, leading and trailing icons are `iconStart` / `iconEnd` nodes.
- `disabled` goes on the group or the item; `className` and other
  attributes go on each item.
- Clicks are the item's `onClick`; to cancel a selection, keep `value`
  controlled and ignore the proposal.
- `tone` and `variant` on the group or the item set the style;
  states are `data-pressed` / `aria-checked`, `:focus-visible` and
  `disabled`.
- Keys are the toggle group keys: arrows move (and select in single mode),
  Space / Enter select or toggle.

```tsx
import { ToggleButton, ToggleButtonGroup } from '@salmonui/buttons/button';
import '@salmonui/buttons/button/styles.css';

<ToggleButtonGroup aria-label="Assignees" variant="chip" selectionMode="multiple" value={ids} onValueChange={setIds}>
  {people.map((person) => (
    <ToggleButton key={person.id} value={person.id} disabled={person.away} iconStart={<Avatar person={person} />}>
      {person.name}
    </ToggleButton>
  ))}
</ToggleButtonGroup>;
```

### Live demo: Chips from data

Live demo: https://salmonui.dev/demos/buttons/chips-mapping

Filter chips mapped from application data with avatar and trailing content, per-chip tone, class, attributes and disabled state, a group-wide disable, a click veto through the controlled value, and single chips.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { Button, ToggleButton, ToggleButtonGroup } from '@salmonui/buttons/button';
import { Checkbox } from '@salmonui/inputs/checkbox';

interface Person {
  readonly id: string;
  readonly name: string;
  readonly initials: string;
  readonly away?: boolean;
  readonly open: number;
}

// Application data, mapped to chips in JSX.
const PEOPLE: readonly Person[] = [
  { id: 'ana', name: 'Ana Lima', initials: 'AL', open: 3 },
  { id: 'ben', name: 'Ben Okafor', initials: 'BO', open: 7 },
  { id: 'chen', name: 'Chen Wei', initials: 'CW', away: true, open: 0 },
  { id: 'dina', name: 'Dina Haddad', initials: 'DH', open: 1 },
];

const Avatar = ({ initials }: { readonly initials: string }) => (
  <span aria-hidden="true" style={{ display: 'inline-grid', placeItems: 'center', inlineSize: 20, blockSize: 20, borderRadius: 'var(--salmon-radius-full)', background: 'var(--salmon-surface-sunken)', fontSize: 10, fontWeight: 600 }}>
    {initials}
  </span>
);

const StarIcon = () => (
  <svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true">
    <path d="M8 1.5l2 4.2 4.6.6-3.4 3.2.9 4.5L8 11.8 3.9 14l.9-4.5L1.4 6.3 6 5.7z" />
  </svg>
);

// Filter chips from data with avatars, trailing counts, per-item tone,
// class and attributes, a disabled item, a click veto and a single chip.
export default function ChipsMappingDemo() {
  const [assignees, setAssignees] = useState<readonly string[]>(['ana']);
  const [locked, setLocked] = useState(false);
  const [teamDisabled, setTeamDisabled] = useState(false);
  const [clicks, setClicks] = useState<readonly string[]>([]);
  const [starred, setStarred] = useState(false);
  return (
    <div style={{ display: 'grid', gap: 12 }}>
      <ToggleButtonGroup
        aria-label="Assignees"
        variant="chip"
        selectionMode="multiple"
        disabled={teamDisabled}
        value={assignees}
        onValueChange={(next) => {
          // Cancel a click: a controlled value that ignores the proposal.
          if (!locked) setAssignees(next);
        }}
      >
        {PEOPLE.map((person) => (
          <ToggleButton
            key={person.id}
            value={person.id}
            disabled={person.away}
            tone={person.open > 5 ? 'warning' : undefined}
            className={`demo-chip demo-chip-${person.id}`}
            data-person-id={person.id}
            title={person.away ? `${person.name} is away` : undefined}
            iconStart={<Avatar initials={person.initials} />}
            iconEnd={<span aria-hidden="true">{person.open}</span>}
            onClick={() => setClicks((list) => [person.name, ...list].slice(0, 3))}
          >
            {person.name}
          </ToggleButton>
        ))}
      </ToggleButtonGroup>
      <div style={{ display: 'flex', flexWrap: 'wrap', alignItems: 'center', gap: 12 }}>
        <Checkbox checked={locked} onCheckedChange={(checked) => setLocked(checked)}>
          Lock selection
        </Checkbox>
        <Checkbox checked={teamDisabled} onCheckedChange={(checked) => setTeamDisabled(checked)}>
          Disable the group
        </Checkbox>
        <ToggleButton variant="chip" pressed={starred} onPressedChange={setStarred} iconStart={<StarIcon />}>
          Starred only
        </ToggleButton>
        <Button variant="chip" tone="danger" onClick={() => setAssignees([])}>
          Clear
        </Button>
      </div>
      <p style={{ margin: 0 }}>
        Selected: <output>{PEOPLE.filter((person) => assignees.includes(person.id)).map((person) => person.name).join(', ') || 'none'}</output> · Starred only: <output>{String(starred)}</output>
      </p>
      <p style={{ margin: 0 }}>
        Recent clicks: <output>{clicks.join(', ') || 'none'}</output>
      </p>
    </div>
  );
}
```

## Design notes

- There are no imperative select or find methods: read and set the
  selection through the controlled `value` and your own data.
- The group is one tab stop with roving focus, so a long chip list does not
  flood the tab order.
- Chips use toggle semantics, not `listbox` / `option`: `radiogroup` /
  `radio` with `aria-checked` (single) or `group` with `aria-pressed`
  (multiple); listbox semantics are left to `Listbox`, and input chips are a
  `TagGroup` grid.
- Input chips are [TagGroup](https://salmonui.dev/docs/buttons/tag-group): `onRemove` adds a
  remove button and Delete / Backspace, cancelling a removal is a controlled
  update you skip, adding and removing are changes to your array,
  `reorderable` and `dragGroup` with `onTagDragStart` / `onTagDrag` /
  `onTagDragEnd` handle drag and drop, and removable tags carry
  `aria-keyshortcuts`.
