# Tag group

A group of tags that can be removed or selected: features, filters, keywords.

- Family: selection
- Import: `import { TagGroup } from "@senvenli/react/selection";`
- Page: https://design.senvenli.com/docs/components/tag-group
- JSON: https://design.senvenli.com/docs/components/tag-group.json

## Example: A list of tags

Read-only tags, some with an icon.

```tsx
import { TagGroup } from "@senvenli/react/selection";

export default function Example() {
  return (
    <TagGroup
      label="Included in your plan"
      items={[
        { id: "api", label: "API access", icon: "code" },
        { id: "sso", label: "Single sign-on", icon: "key-round" },
        { id: "audit", label: "Audit log", icon: "scroll-text" },
        { id: "support", label: "Priority support" },
      ]}
    />
  );
}
```

## Examples

### Removable

Active filters: each tag has a remove button, and the group says when none are left.

```tsx
import { TagGroup, type TagItem } from "@senvenli/react/selection";
import { useState } from "react";

export default function Example() {
  const [filters, setFilters] = useState<TagItem[]>([
    { id: "status", label: "Status: Paid" },
    { id: "customer", label: "Customer: Ada Lovelace" },
    { id: "period", label: "Last 30 days" },
  ]);
  return (
    <TagGroup
      label="Active filters"
      items={filters}
      onRemove={(keys) => setFilters((current) => current.filter((filter) => !keys.includes(filter.id)))}
      emptyState="No filters. Showing every invoice."
    />
  );
}
```

### Selectable

Tags that toggle, for picking several of a few.

```tsx
import { TagGroup } from "@senvenli/react/selection";
import { useState } from "react";

export default function Example() {
  const [days, setDays] = useState<(string | number)[]>(["mon", "wed"]);
  return (
    <TagGroup
      label="Delivery days"
      description={`${days.length} of 5 days selected.`}
      selectionMode="multiple"
      selectedKeys={days}
      onSelectionChange={setDays}
      items={[
        { id: "mon", label: "Monday" },
        { id: "tue", label: "Tuesday" },
        { id: "wed", label: "Wednesday" },
        { id: "thu", label: "Thursday" },
        { id: "fri", label: "Friday" },
      ]}
    />
  );
}
```

## Guidelines

### When to use it

- Showing or picking keywords, features or active filters.

### Use something else when

- [badge](https://design.senvenli.com/docs/components/badge.md): The tag is an item's status.

## Accessibility

Keyboard:

- Arrow keys: Moves between tags.
- Delete, Backspace: Removes the focused tag.

Built in:

- Groups of three or more removable tags offer "Clear all".

You provide:

- A label for the group.

## API reference

### TagGroup

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| label | `React.ReactNode` |  | yes |  |
| items | `readonly TagItem[]` |  | yes |  |
| onRemove | `(keys: Key[]) => void` |  | no | Makes tags removable (active filters); called with the keys removed. |
| selectionMode | `"single" \| "multiple"` |  | no | Makes tags selectable (features, filters). |
| selectedKeys | `readonly Key[]` |  | no |  |
| defaultSelectedKeys | `readonly Key[]` |  | no |  |
| onSelectionChange | `(keys: Key[]) => void` |  | no |  |
| description | `React.ReactNode` |  | no |  |
| errorMessage | `React.ReactNode` |  | no |  |
| emptyState | `React.ReactNode` |  | no | What to show when there are no tags ("No filters"). |

### Tokens it reads

`--ds-background-neutral`, `--ds-background-neutral-hovered`, `--ds-background-neutral-pressed`, `--ds-background-selected`, `--ds-background-selected-hovered`, `--ds-border-focused`, `--ds-border-selected`, `--ds-icon-subtle`, `--ds-link`, `--ds-radius-control-small`, `--ds-radius-small`, `--ds-radius-xsmall`, `--ds-size-control-compact`, `--ds-space-050`, `--ds-space-075`, `--ds-space-100`, `--ds-text`, `--ds-text-disabled`, `--ds-text-subtlest`

## Related

- [select](https://design.senvenli.com/docs/components/select.md)
- [badge](https://design.senvenli.com/docs/components/badge.md)
