# Filter bar

> The bar above a list, done like Linear’s: one panel from property to value, chips you change in place, and a sentence that becomes filters.

Most filter bars open a menu, then a second popover, then drop a chip somewhere, and changing "is" to "is not" means another menu. Here one panel does it all. Filter opens it under the button: pick a property and its values slide in from the side, pick one and it lands in the bar as a chip while the panel folds back. Typing searches properties and values at once, so "hig" offers Priority › High straight away. A chip reads Status | is | Todo: click "is" and it turns into "is not", click the value and the panel opens from the chip with its values ticked, so you can add a second or swap it. "Todo" becomes "Todo, In progress", "is" becomes "is any of", each through a morph, and the chips around make room by gliding. Give it parse() and the panel offers Filter by “high priority bugs for Maya”: your model turns the sentence into chips. Filters are plain data that maps onto a query, and matchFilters() applies them to rows you already have.

## Install

```bash
npx shadcn@latest add @hairline/filter-bar
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): `text-morph`, `number-roll`
- Docs and live preview: https://hairlineui.com/ui/filter-bar
- Registry item (source): https://hairlineui.com/r/filter-bar.json

## Usage

```tsx
import { FilterBar, matchFilters, type Filter, type FilterField } from "@/components/filter-bar";

const fields: FilterField<Issue>[] = [
  { id: "status", label: "Status", type: "option", options: [{ value: "todo", label: "Todo" }, { value: "done", label: "Done" }] },
  { id: "title", label: "Title", type: "text" },
  { id: "updated", label: "Updated", type: "date" },
];

const [filters, setFilters] = useState<Filter[]>([]);
const rows = matchFilters(issues, filters, fields);

<FilterBar fields={fields} value={filters} onValueChange={setFilters} count={rows.length} total={issues.length} noun={["issue", "issues"]} />
```

## Filters that run on your server, with a sentence box

Filters are plain objects, so they go straight into a query string and back out of it. parse sends the sentence to a route that asks a model for filters in the same shape; anything it can't place, it leaves out.

```tsx
"use client";
import { useState } from "react";
import useSWR from "swr";
import { FilterBar, type Filter, type FilterField, type NewFilter } from "@/components/filter-bar";
import { DataTable } from "@/components/data-table";

const fields: FilterField[] = [
  { id: "status", label: "Status", type: "option", options: statuses },
  { id: "assignee", label: "Assignee", type: "option", options: members },
  { id: "estimate", label: "Estimate", type: "number", unit: ["point", "points"] },
  { id: "updated", label: "Updated", type: "date" },
];

export function IssueList() {
  const [filters, setFilters] = useState<Filter[]>([]);
  const { data } = useSWR(`/api/issues?filters=${encodeURIComponent(JSON.stringify(filters))}`, (u) => fetch(u).then((r) => r.json()));

  return (
    <>
      <FilterBar
        fields={fields}
        value={filters}
        onValueChange={setFilters}
        count={data?.matched}
        total={data?.total}
        noun={["issue", "issues"]}
        parse={async (text): Promise<NewFilter[]> => {
          // Your route asks a model (AI SDK generateObject with a schema of these filters) and returns them.
          const res = await fetch("/api/issues/describe", { method: "POST", body: JSON.stringify({ text, fields }) });
          if (!res.ok) throw new Error("Couldn't read that");
          return res.json();
        }}
      />
      <DataTable columns={columns} rows={data?.rows ?? []} noun={["issue", "issues"]} />
    </>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `fields` | `FilterField[]` | – | { id, label, type, icon?, options?, unit?, get? }. type is "option" (pick values), "text" (contains), "number" (at least, at most, exactly) or "date" (within or older than a number of days). |
| `value / defaultValue` | `Filter[]` | – | The filters: { id, field, op, values } for options, { …, text } for text, { …, number } for numbers, { …, days } for dates. |
| `onValueChange` | `(filters: Filter[]) => void` | – | Every add, change and removal. |
| `count / total` | `number` | – | How many rows match, and how many there are: "37 of 128 issues" rolls beside the bar. Leave count out when the list shows its own. |
| `noun` | `[string, string]` | `["result", "results"]` | What a row is called, for the count. |
| `parse` | `(text) => NewFilter[] \| Promise<NewFilter[]>` | – | Turns a sentence into filters. Adds "Filter by “…”" to the panel for searches of three letters or more. Throw, or return none, to say it couldn't. |
| `hotkey` | `string \| null` | `"f"` | Opens the panel from anywhere on the page while nothing is being typed. null turns it off. |
| `locales` | `string` | – | For numbers in chips and the count. |
| `label` | `string` | `"Filters"` | Accessible name of the bar. |
| `matchFilters(rows, filters, fields)` | `function` | – | Keeps the rows every filter allows, reading each field with get (or row[id]). Arrays match an option filter if any value does. |

## States

- **open**: Filter (or F anywhere on the page while nothing is being typed) grows the panel out from under the button: width, height and radius ease from the button's size in 300ms while the list fades in after 60ms. It never covers the bar, and it narrows and shifts to fit the container.
- **step**: Choosing a property slides its values in 16px from the right (240ms) while the properties slide out left, and the panel's height eases to the new list. Back, Backspace or ← in an empty field, or Escape, slides back.
- **search**: The field searches properties and every option's values together; a value match reads Priority › High and adds its filter in one go. Text, number and date properties turn what you type into choices: Contains “north”, At least 20 seats, Last 12 days.
- **add**: The new chip rises 6px into the bar out of a blur (320ms) while Filter and Clear glide aside and the panel folds back. A value for a property already in the bar with the same operator joins that chip instead of making another.
- **flip**: Clicking the operator cycles it (is ↔ is not, contains ↔ doesn't contain, at least → at most → is exactly, within ↔ older than) through TextMorph; the chip's width eases and its neighbours follow.
- **edit**: Clicking a value opens the panel from that chip with its values ticked: ticks draw themselves, "Todo" morphs to "Todo, In progress" and "is" to "is any of", and "2 selected" rolls. A chip with nothing ticked goes when the panel closes.
- **describe**: With parse, any search of three letters or more ends with Filter by “…”. While it runs the row says so beside a spinner; the chips it returns rise in one after another, 70ms apart. If it throws or finds nothing, the row shakes and says it couldn't, and your words stay to edit.
- **remove**: A removed chip fades where it stood (its own element, so nothing waits for it) while the rest glide into the gap; Clear does it for all of them.

## What it handles

Most filter bars open a menu, then a second popover, then drop a chip somewhere, and changing “is” to “is not” means another menu. Here one panel does all of it, the way Linear’s filters work.

- **One panel, property to value**: Filter opens a panel under the button. Pick a property and its values slide in, then the chip lands in the bar.
- **Search finds both at once**: Typing “hig” offers Priority › High straight away. Text, number and date properties turn what you type into choices.
- **Chips you change in place**: Click “is” and it becomes “is not”. Click the value and the panel reopens from the chip with its values ticked.
- **Describe it in a sentence**: Give it parse() and the panel offers “Filter by …”. Your model returns chips, which rise into the bar.
- **Filters are plain data**: They go straight into a query string, and matchFilters() applies them to rows you already have.
- **Keyboard first**: Press F anywhere to open the panel, move with the arrow keys, and every chip is named by its whole sentence for screen readers.

## Replaces

a filter dropdown, filter chips, operator menus, a query builder, a filter matching function, a natural-language search box

## Where it fits

- **Issue trackers and project tools**: Filter by status, priority and assignee, like Linear.
- **CRM and customer lists**: Find customers by plan, seats or when they were last seen.
- **Admin tables and logs**: Narrow a long table with the same chips on every screen.
- **Search by sentence**: Let people type “high priority bugs for Maya” and have a model turn it into filters.

## Notes

- The panel's field is a combobox over a listbox: ↑ and ↓ move the highlight, Enter chooses, → opens a property, ← or Backspace in an empty field goes back, Escape goes back and then closes. Focus stays in the field the whole time and returns to whatever opened the panel.
- Each chip is a group named by its whole sentence ("Status is any of Todo, In progress"). Its operator and value are buttons with their own names, and × removes it. Adds, changes and removals are announced in a status region outside the bar.
- Chips glide only when one arrives or leaves: their places are taken at the click, the new layout is read once, and each one moves by a transform. Nothing runs between changes, and the panel animates its own box once per open and close.
- On narrow containers the chips stay on one line and scroll sideways; the panel fits the container's width. Inputs use 16px text on touch screens so phones don't zoom.
- Reduced motion opens, steps and adds in place; ticks, labels and the count still change.

License: MIT (https://hairlineui.com/license#components)
