# TagFilter

> A filter of several values that shows each value as a tag in the toolbar, with its own ×, a +N for the ones that don't fit, and a × that clears the filter.

Source: https://raft-design.botiverse.build/components/tag-filter. Generated from the current release; where it differs from what you remember, this page is right.

TagFilter is a toolbar filter that takes several values and shows them. Unset, it is a [PickerTriggerButton](https://raft-design.botiverse.build/components/picker-trigger-button.md) that says the filter's name. Applied, the whole filter is one `info-soft` group as high as the button: the name, which opens the list; a 24px tag for each value with its own ×; `+N` for the tags that don't fit; and, when `clearable`, a × that clears the filter. People see what the filter holds, take one value out, or clear it all, without opening the list ([Select › Filters](https://raft-design.botiverse.build/components/select.md#filters)).

```tsx
import { CircleDot, Hash, User } from "lucide-react";
import { useCallback, useState } from "react";
import { Button } from "@botiverse/raft-ui";
import { ComboboxTagFilter } from "@botiverse/raft-ui/wip";

const STATUSES = ["Todo", "In progress", "In review", "Done", "Closed"] as const;
const PEOPLE = ["Kirby", "Tenny", "Lagoon", "Scout", "Artea", "huxijin"] as const;
const CHANNELS = ["proj-chat", "proj-raft-ui", "triage", "proj-mobile"] as const;
const same = (v: string) => v;
const removeLabel = (label: string) => `Remove ${label}`;
const listFormat = new Intl.ListFormat("en", { type: "unit" });
const statusLabel = (labels: readonly string[]) => `Status: ${listFormat.format(labels)}`;
const assigneeLabel = (labels: readonly string[]) => `Assignee: ${listFormat.format(labels)}`;
const channelValueLabel = (labels: readonly string[]) => `Channel: ${listFormat.format(labels)}`;
const channelLabel = (c: string) => `#${c}`;
const PEOPLE_SEARCH = { label: "Search people", placeholder: "Search" };

export function TagFilterToolbarContent() {
  const [status, setStatus] = useState<string[]>(["In progress", "Done"]);
  const [people, setPeople] = useState<string[]>(["Kirby", "Tenny", "Lagoon", "Scout"]);
  const [channels, setChannels] = useState<string[]>([]);
  const clearAll = useCallback(() => {
    setStatus([]);
    setPeople([]);
    setChannels([]);
  }, []);
  const any = status.length + people.length + channels.length > 0;
  return (
    <div className="flex min-w-0 flex-wrap items-center gap-2">
      <ComboboxTagFilter
        name="Status"
        valueLabel={statusLabel}
        clearFilterLabel="Clear Status"
        icon={<CircleDot aria-hidden data-icon="inline-start" />}
        items={STATUSES}
        value={status}
        onValueChange={setStatus}
        labelOf={same}
        clearLabel="Clear"
        removeLabel={removeLabel}
        testId="status"
      />
      <ComboboxTagFilter
        name="Assignee"
        valueLabel={assigneeLabel}
        clearFilterLabel="Clear Assignee"
        icon={<User aria-hidden data-icon="inline-start" />}
        items={PEOPLE}
        value={people}
        onValueChange={setPeople}
        labelOf={same}
        search={PEOPLE_SEARCH}
        emptyLabel="No match"
        clearLabel="Clear"
        removeLabel={removeLabel}
        className="max-w-72"
        testId="assignee"
      />
      <ComboboxTagFilter
        name="Channel"
        valueLabel={channelValueLabel}
        clearFilterLabel="Clear Channel"
        icon={<Hash aria-hidden data-icon="inline-start" />}
        items={CHANNELS}
        value={channels}
        onValueChange={setChannels}
        labelOf={channelLabel}
        clearLabel="Clear"
        removeLabel={removeLabel}
        testId="channel"
      />
      {any ? (
        <Button size="md" variant="ghost" onClick={clearAll}>
          Clear all
        </Button>
      ) : null}
    </div>
  );
}
```

## When to use

| Situation                                                                                      | Use                                                                                                             |
| ---------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
| A toolbar filter that can hold several values (statuses, people, channels, kinds of place)     | TagFilter (`ComboboxTagFilter`)                                                                                 |
| A toolbar filter that holds one value (a sort, one channel)                                    | A filter button that shows the value ([PickerTriggerButton](https://raft-design.botiverse.build/components/picker-trigger-button.md)) |
| A filter people change seldom and set by habit, behind a Filter button that always says Filter | [FilterChip](https://raft-design.botiverse.build/components/filter-chip.md)                                                                     |
| Chosen values in a form field (people to add, labels)                                          | The combobox's own chips ([Combobox](https://raft-design.botiverse.build/components/combobox.md))                                                  |

## Anatomy

- `TagFilter`: the group, a `role="group"` named by the filter. It holds the parts below and carries the applied fill. Required.
- `TagFilterTrigger`: the filter's name and its optional 14px icon; the button that opens the list. Unset it carries the chevron; applied it doesn't. Required.
- `TagFilterTag`: one value, a 24px tag with a 18px ×. One per value shown.
- `TagFilterMore`: `+N`, the values that don't fit. It opens the list as the trigger does.
- `TagFilterClear`: the × at the group's end that clears the filter, after a 1px seam. The seam is as far from what comes before it, a tag's edge or `+N`'s text, as from the × after it. Only with `clearable`.
- The list: the combobox popup, anchored to the group's start. A header with the filter's name and Clear, an optional search, and the values with a check on each chosen one, in groups when `groups` is given.

`ComboboxTagFilter` puts these together over a `multiple` [Combobox](https://raft-design.botiverse.build/components/combobox.md); use the parts yourself only for a structure it doesn't cover.

## Options

- `maxTagCount`: how many tags show.
  - `"responsive"` (default): as many as fit the group's room, then `+N`. The group is at most 320px wide, or what its parent gives it if less (outside a FitToolbar, which measures the room itself); set a `max-w-*` on it for another cap.
  - A number: at most that many, then `+N`.
  - `"none"`: every tag, and the group wraps onto more lines. Use it only where the toolbar can grow taller.
- `clearable` (default `true`): the × at the group's end. Leave it on: clearing a filter of several values one tag at a time is slow. Turn it off only when the filter can't be empty.
- `search`: a search row at the top of the list, for a list that can hold many values (people, places). With it, give `emptyLabel`. Opened with a mouse or the keyboard, typing goes straight to it, and Enter takes the first match; opened by a tap, it takes no focus, so the keyboard doesn't come up over the list. A list of a few fixed values (statuses, outcomes) has none.
- `searchTextOf`: what the search matches for a value, `labelOf` by default. Give people their display name and handle, so a search for either finds them; give a "Me" row its own word with the person's name and handle, so a search for yourself finds it.
- `disabled`: the filter can't open yet, while its values load: the trigger takes PickerTriggerButton's disabled look. An empty list that opens is more confusing than a button that waits.
- `popupClassName`: the list's width, 240px (`w-60`) by default. Widen it for long names, such as a computer by code and name.
- `groups`, instead of `items`: the list in groups, each `{ label?, items }`. A group's values sit under its heading (`ComboboxGroupLabel`), with a separator between groups; a group without a `label` shows no heading. The type takes one or the other, never both. When the search leaves a group with nothing, its heading and the separator before it go too, so no line is left on its own.
- `tagLabelOf`: a shorter text for a value inside its tag alone, such as a computer's code. Everything else keeps `labelOf`'s full name: the tag's title, its ×'s name, the trigger's name and tooltip, and the search. The ruler measures the short text, since that is what the tag shows.
- `open` and `onOpenChange`: the list's open state, for a page that keeps one filter's list open at a time. Uncontrolled by default.
- `tagsOrder` and `labelOrder`: its steps in a [FitToolbar](https://raft-design.botiverse.build/components/fit-toolbar.md) (below).

```tsx
import { User } from "lucide-react";
import { useState } from "react";
import { ComboboxTagFilter } from "@botiverse/raft-ui/wip";

// The person viewing is Kirby: "Me" stands for them, so People leaves them out.
const ASSIGNEE_GROUPS = [
  { items: ["me", "unassigned"] },
  { label: "People", items: ["Tenny", "Lagoon", "Scout", "Artea", "huxijin"] },
];
const SHORTCUTS: Record<string, string> = { me: "Me", unassigned: "Unassigned" };
const assigneeOf = (v: string) => SHORTCUTS[v] ?? v;
const removeLabel = (label: string) => `Remove ${label}`;
const listFormat = new Intl.ListFormat("en", { type: "unit" });
const assigneeLabel = (labels: readonly string[]) => `Assignee: ${listFormat.format(labels)}`;
const PEOPLE_SEARCH = { label: "Search people", placeholder: "Search" };

export function TagFilterGroupsContent() {
  const [assignees, setAssignees] = useState<string[]>(["me", "Tenny"]);
  return (
    <ComboboxTagFilter
      name="Assignee"
      valueLabel={assigneeLabel}
      clearFilterLabel="Clear Assignee"
      icon={<User aria-hidden data-icon="inline-start" />}
      groups={ASSIGNEE_GROUPS}
      value={assignees}
      onValueChange={setAssignees}
      labelOf={assigneeOf}
      search={PEOPLE_SEARCH}
      emptyLabel="No match"
      clearLabel="Clear"
      removeLabel={removeLabel}
    />
  );
}
```

## In a FitToolbar

An applied filter marks itself `data-active`, as FitToolbar asks of an applied item, so a scrolling group keeps it in view; the caller adds nothing.

In a FitToolbar the filter gives way with the rest of the toolbar ([FitToolbar › Behavior](https://raft-design.botiverse.build/components/fit-toolbar.md#behavior)). It doesn't measure its parent's room there: the toolbar does, and two measures of one room would disagree, as SegmentedControl's `iconsOnlyBelow` is off in a toolbar. It shows every tag the 320px cap holds, and folds into `+N` within that cap only.

- `tagsOrder` (the `tags` step): its tags fold to the first one and `+N`, at least one tag kept.
- `labelOrder` (the `label` step): its name hides and its icon stays. The trigger's accessible name and a tooltip keep the name and every value (`valueLabel`). A filter without an `icon` refuses this step, since nothing would be left to show.

Its trigger and, once it is applied, its clear × are the toolbar's items: one or two per filter. The toolbar stays one Tab stop, and its arrow keys go through them in reading order, the trigger, then its clear ×, then the next item; an unset filter is its trigger alone. A tag's × is for a pointer and never a stop. After the clear × takes the filter's values out, focus goes to the trigger, which then holds the toolbar's Tab stop. An item the arrow keys reach under a scrolling group's fade comes into view. Before, the trigger and the clear × were Tab stops of their own inside the toolbar, and its arrow keys didn't reach them (slock's Tasks and search toolbars).

A filter never moves into More: as [The toolbar over a list](https://raft-design.botiverse.build/patterns/responsive.md#the-toolbar-over-a-list) says, the filters wrap to a line of their own that scrolls sideways. Put them in a `FitToolbarGroup` with `overflow="scroll"` in a toolbar with `wrap`, the sort on the first line.

Give `tags` and `label` the numbers of [The toolbar over a list](https://raft-design.botiverse.build/patterns/responsive.md#the-toolbar-over-a-list): a filter's own tags fold first, then its name becomes its icon. At the same number `tags` comes before `label`, and the filters of one `FitToolbarGroup` with the same number fold and unfold together. Items in a FitToolbar don't shrink; truncation happens only within the 320px cap, the tag first, then the name (0.111.2's rule).

```tsx
import { ArrowDownUp, CircleDot, User } from "lucide-react";
import { useCallback, useState } from "react";
import {
  ComboboxTagFilter,
  FitToolbar,
  FitToolbarButton,
  FitToolbarGroup,
} from "@botiverse/raft-ui/wip";

const STATUSES = ["Todo", "In progress", "In review", "Done"] as const;
const PEOPLE = ["Kirby", "Tenny", "Lagoon", "Scout"] as const;
const same = (v: string) => v;
const removeLabel = (label: string) => `Remove ${label}`;
const listFormat = new Intl.ListFormat("en", { type: "unit" });
const statusLabel = (labels: readonly string[]) => `Status: ${listFormat.format(labels)}`;
const assigneeLabel = (labels: readonly string[]) => `Assignee: ${listFormat.format(labels)}`;

type Filter = "status" | "assignee";

export function TagFilterInFitToolbarContent() {
  const [status, setStatus] = useState<string[]>(["In progress", "In review"]);
  const [people, setPeople] = useState<string[]>(["Kirby", "Tenny"]);
  const [sortedBy, setSortedBy] = useState("Updated");
  // One filter's list open at a time.
  const [open, setOpen] = useState<Filter | null>(null);
  const openStatus = useCallback((next: boolean) => setOpen(next ? "status" : null), []);
  const openAssignee = useCallback((next: boolean) => setOpen(next ? "assignee" : null), []);
  const toggleSort = useCallback(
    () => setSortedBy((sort) => (sort === "Updated" ? "Created" : "Updated")),
    [],
  );
  return (
    <div className="w-full max-w-2xl min-w-60 resize-x overflow-hidden rounded-container border border-line-seam p-3">
      <FitToolbar aria-label="Tasks" wrap className="gap-2">
        <FitToolbarGroup overflow="scroll" wrapOrder={30} className="gap-2">
          <ComboboxTagFilter
            name="Status"
            valueLabel={statusLabel}
            clearFilterLabel="Clear Status"
            icon={<CircleDot aria-hidden data-icon="inline-start" />}
            items={STATUSES}
            value={status}
            onValueChange={setStatus}
            labelOf={same}
            clearLabel="Clear"
            removeLabel={removeLabel}
            open={open === "status"}
            onOpenChange={openStatus}
            tagsOrder={10}
            labelOrder={20}
          />
          <ComboboxTagFilter
            name="Assignee"
            valueLabel={assigneeLabel}
            clearFilterLabel="Clear Assignee"
            icon={<User aria-hidden data-icon="inline-start" />}
            items={PEOPLE}
            value={people}
            onValueChange={setPeople}
            labelOf={same}
            clearLabel="Clear"
            removeLabel={removeLabel}
            open={open === "assignee"}
            onOpenChange={openAssignee}
            tagsOrder={10}
            labelOrder={20}
          />
        </FitToolbarGroup>
        <FitToolbarGroup firstLine className="ms-auto gap-2">
          <FitToolbarButton icon={<ArrowDownUp />} labelOrder={5} onClick={toggleSort}>
            {sortedBy}
          </FitToolbarButton>
        </FitToolbarGroup>
      </FitToolbar>
    </div>
  );
}
```

## States

- Unset: the PickerTriggerButton look, the name and a chevron, as high as the other filter buttons.
- Applied: the group fills `info-soft` at the same height. The name inside it has no fill of its own; it hovers with the medium overlay ([Hover and press](https://raft-design.botiverse.build/foundations/states.md#hover-and-press)).
- A tag sits on the canvas colour with a seam ring. Its × is 18px, 3px from the tag's top, bottom and end, so its hover fill stays inside the tag; only the × hovers.
- The clear × is 24px with a 14px glyph, 4px from the group's end; it hovers with the medium fill.

## Behavior

- Choosing a value in the list adds its tag at the end; tags keep the order the values were chosen in. The list stays open while people choose.
- A tag's × takes that one value out. The clear × takes them all out, and the filter is unset again.
- After a × removes itself, focus goes to the trigger, never to the page or to the next filter. The trigger's new name says what is left.
- With `maxTagCount="responsive"`, tags fold into `+N` before the name is cut, and one tag always shows. When even that one doesn't fit beside the name, the tag gives way first: its label truncates down to a letter, an ellipsis and its ×, and only then the name truncates, so the filter keeps saying what it is ("Destinations [Private c… ×] +2" in a 280px column). The count is measured before the first paint and again when the row resizes, so no frame shows tags that are then taken back.
- On a touch screen, each × and `+N` has a hit area of at least 24px; `+N` counts as part of the trigger ([Accessibility › Hit targets](https://raft-design.botiverse.build/foundations/accessibility.md#hit-targets)).

## Content

- Name the filter by what it filters, as a noun: Status, Assignee, Channel. Don't add the word Filter.
- A tag says the value's name as the list does, or a short form of it through `tagLabelOf` (a computer's code) when the full name would crowd the toolbar. Keep the icon in the list; the tag carries text only.
- A shortcut for the person viewing ("Me") is a plain first row, in an unlabelled first group followed by the separator, then the labelled People group. Its tag says the shortcut's own word (`labelOf` returns "Me"), never expanded to the person's name. Leave the person out of the People group, so the same person can't be two values and two tags.
- Name a tag's × "Remove {value}", and the clear × "Clear {filter}": the clear ×'s name always says which filter it clears.
- The caller writes every sentence, in its own language: the trigger's name once values are chosen (`valueLabel`, from the labels in order), each tag's × (`removeLabel`) and the clear × (`clearFilterLabel`). TagFilter joins no words itself, so a list takes the language's own mark: "Assignee: Kirby, Tenny" in English, the ideographic comma in Chinese (`Intl.ListFormat`, or the catalog's list pattern).

## Accessibility

- The group is a `role="group"` named by the filter; the trigger, the tags' × and the clear × are siblings in it, not nested buttons.
- Two Tab stops per filter: the trigger and the clear ×. A tag's × is for a pointer (`tabIndex=-1`); the keyboard takes a value out by unticking it in the list. In a FitToolbar the trigger and the clear × are toolbar items instead, reached with the arrow keys, and the whole toolbar is one Tab stop ([In a FitToolbar](#in-a-fittoolbar)).
- The clear × is named by `clearFilterLabel`, which must name the filter ("Clear Assignee", not "Clear"): in a toolbar of several filters, the arrow keys reach one clear × after another, and a screen reader reads only that name.
- The trigger's name and tooltip list every chosen value, the folded ones included ("Assignee: Kirby, Tenny, Lagoon, Scout"); unset, it is the filter's name. `+N` is hidden from screen readers, since the trigger already names those values.
- No live region: when the filter clears, focus is on the trigger, and its new name is what a screen reader reads.

## Rules

- Use TagFilter for every filter of several values in a toolbar, so all of them read the same way. Don't summarise chosen values as "2 statuses" in the button.
- Keep a filter of one value a PickerTriggerButton: a tag for a single value with no siblings adds nothing.
- Don't put a tag's icon, avatar or colour in the tag. The list shows them; the toolbar stays text.
- Don't nest one filter's group inside another's, or put other controls inside the group.

## API

### ComboboxTagFilter

The ready-made filter: TagFilter's parts over a `multiple` Combobox, with `maxTagCount` and `clearable`.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `clearable` | `boolean` | `true` | A × at the group's end that clears the filter. |
| `clearFilterLabel` (required) | `string` |  | Names the clear ×: "Clear Assignee". |
| `clearLabel` (required) | `string` |  | What the list's Clear button says: "Clear". |
| `disabled` | `boolean` | `false` | The filter can't be opened yet (its values are still loading): the trigger takes PickerTriggerButton's disabled look. |
| `emptyLabel` | `string` |  | The list's empty state, with a search. |
| `groups` | `readonly ComboboxTagFilterGroup<Value>[]` |  | The list in groups, instead of `items`: each group's values under its heading, a separator between groups. A group the search empties hides with its heading and its separator. |
| `icon` | `ReactNode` |  | A leading icon, 14px, `data-icon="inline-start"`. In a FitToolbar, what stays once the name gives way (`labelOrder`). |
| `items` | `readonly Value[]` |  | Every value, in the list's order. |
| `labelOf` (required) | `(value: Value) => string` |  | A value's name, in the list, the search, the trigger's name and each tag's × and title. |
| `labelOrder` | `number` |  | In a FitToolbar: when its name gives way to its icon (the `label` step); the trigger's name and tooltip keep the name and every value. Needs an `icon`. |
| `maxTagCount` | `number \| "none" \| "responsive"` | `"responsive"` | `"responsive"` (default): as many tags as fit, then +N. A number: at most that many. None: every tag, and the group wraps. |
| `name` (required) | `string` |  | The filter's name: what the button says unset, the list's title and the group's name. |
| `onOpenChange` | `(open: boolean) => void` |  |  |
| `onValueChange` (required) | `(value: Value[]) => void` |  |  |
| `open` | `boolean` |  | The list is open: with `onOpenChange`, the page keeps one filter's list open at a time. Uncontrolled by default. |
| `popupClassName` | `string` |  | The list's width, `w-60` (240px) by default: wider for long names ("w-70" for computers by code and name). |
| `removeLabel` (required) | `(label: string) => string` |  | Names a tag's ×: (label) => "Remove {label}". |
| `renderItem` | `(value: Value) => ReactNode` |  | A row of the list; its name by default. |
| `search` | `{ label: string; placeholder: string; }` |  | A search row in the list, for a list that can hold many values (people, places). |
| `searchTextOf` | `(value: Value) => string` |  | What the search matches for a value, `labelOf` by default: a person's display name and handle ("Kirby kirby"), or a "Me" row's own word with the person's name and handle, so searching for yourself finds it. |
| `tagLabelOf` | `(value: Value) => string` |  | A shorter text for a value inside its tag alone (a computer's code). The tag's title, its ×'s name, the trigger's name and tooltip and the search keep `labelOf`'s full name. `labelOf` by default. |
| `tagsOrder` | `number` |  | In a FitToolbar: when its tags fold to the first one and +N (the `tags` step). |
| `testId` | `string` |  |  |
| `value` (required) | `Value[]` |  |  |
| `valueLabel` (required) | `(labels: readonly string[]) => string` |  | The trigger's name and tooltip once values are chosen, from their labels in order: "Assignee: Kirby, Tenny" (in Chinese, with the ideographic comma). The caller writes the sentence, in its language and with its list mark (Intl.ListFormat or the catalog); unset the trigger's name is `name`. |

### TagFilter

The filter's group: the trigger, the tags, +N and the clear ×. Inside a `Combobox` with `multiple`.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `name` (required) | `string` |  | The filter's name: the group's accessible name. |
| `wrap` | `boolean` | `false` | Tags that don't fit wrap onto more lines (no `maxTagCount`), instead of folding into +N. |
| `render` | `ReactElement \| ((props, state) => ReactElement)` |  | Allows you to replace the component's HTML element with a different tag, or compose it with another component. |

Also takes the attributes of its element.

### TagFilterClear

`clearable`: the × at the group's end that clears the whole filter. A Tab stop, or in a FitToolbar one of its items; focus then goes to the trigger.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `disabled` | `boolean` | `false` | Whether the component should ignore user interaction. |
| `keepMounted` | `boolean` | `false` | Whether the component should remain mounted in the DOM when not visible. |
| `nativeButton` | `boolean` | `true` | Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`). |
| `render` | `ReactElement \| ((props, state) => ReactElement)` |  | Allows you to replace the component's HTML element with a different tag, or compose it with another component. |

Also takes the attributes of its element.

### TagFilterMore

`+N`: the tags that don't fit. It opens the list as the trigger does; hidden from screen readers, whose trigger names every value.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `count` (required) | `number` |  |  |

Also takes the attributes of its element.

### TagFilterTag

One value: a 24px ComboboxChip with its ×, for a pointer only (the keyboard unticks it in the list).

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `removeLabel` (required) | `string` |  | Names the tag's ×: "Remove Codex". |
| `render` | `ReactElement \| ((props, state) => ReactElement)` |  | Allows you to replace the component's HTML element with a different tag, or compose it with another component. |

Also takes the attributes of its element.

### TagFilterTrigger

The filter's name, the button that opens the list. Unset it carries the chevron.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `valueLabel` | `string` |  | The trigger's accessible name: the filter's name unset ("Runtime"), with every chosen value applied ("Runtime: Claude Code, Codex"), which is also its tooltip. A combobox takes no name from its text, so pass it unset too. |
| `disabled` | `boolean` | `false` | Whether the component should ignore user interaction. |
| `nativeButton` | `boolean` | `true` | Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`). |
| `render` | `ReactElement \| ((props, state) => ReactElement)` |  | Allows you to replace the component's HTML element with a different tag, or compose it with another component. |

Also takes the attributes of its element.
