Filter Toolbar
Native faceted filters with removable active chips, a reset control, and values ready for GET submission.
pnpm dlx shadcn@latest add @haze/filter-toolbar---
import { X } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
import Button from "./button.astro";
import Select from "./select.astro";
interface Props {
id: string;
label?: string;
action?: string;
filters?: { key: string; label: string; options: { value: string; label: string }[]; /** Label of the first, unfiltered option. Defaults to "All". */ allLabel?: string }[];
values?: Record<string, string>;
class?: string;
}
const { id, label = "Filter projects", action = "", values = {}, class: className, filters = [
{ key: "status", label: "Status", allLabel: "All statuses", options: [{ value: "open", label: "Open" }, { value: "review", label: "In review" }, { value: "done", label: "Done" }] },
{ key: "owner", label: "Owner", allLabel: "All owners", options: [{ value: "mila", label: "Mila" }, { value: "owen", label: "Owen" }] },
] } = Astro.props;
---
<form id={id} data-haze-filter-toolbar role="group" aria-label={label} method="get" action={action} class={cn("group/filters grid min-w-0 gap-3", className)}>
<div class="flex min-w-0 flex-wrap gap-2">
{filters.map(filter => <div class="grid min-w-28 flex-1 gap-2">
<label for={`${id}-${filter.key}`} class="text-[0.8125rem]/5 font-medium">{filter.label}</label>
<Select id={`${id}-${filter.key}`} name={filter.key} value={values[filter.key] ?? ""} options={[{ value: "", label: filter.allLabel ?? "All" }, ...filter.options]} data-filter-label={filter.label} />
</div>)}
</div>
<ul data-filter-chips aria-label="Active filters" class="hidden min-w-0 flex-wrap gap-2 group-data-[enhanced]/filters:flex empty:!hidden"></ul>
<div class="flex flex-wrap gap-2">
<Button type="submit" size="sm" variant="secondary">Apply filters</Button>
<Button type="reset" size="sm" variant="ghost">Reset</Button>
</div>
<span data-filter-status role="status" class="sr-only"></span>
<template data-filter-chip><li class="flex min-h-8 min-w-0 max-w-full items-center gap-2 rounded-full border border-frame bg-muted ps-3 pe-1.5 text-xs/5 font-medium text-foreground motion-safe:animate-pop"><span data-chip-label class="wrap-anywhere"></span><button type="button" class="grid size-6 shrink-0 cursor-pointer place-items-center rounded-full text-muted-foreground motion-safe:transition-colors motion-safe:duration-150 motion-safe:ease-snappy hover:bg-border hover:text-foreground active:scale-95 motion-reduce:active:scale-100 focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring"><X aria-hidden="true" class="size-3" /></button></li></template>
</form>
<script>
function enhance(form: HTMLFormElement) {
if (form.hasAttribute("data-enhanced")) return;
const fields = [...form.querySelectorAll<HTMLSelectElement>("select")];
const list = form.querySelector<HTMLElement>("[data-filter-chips]")!;
const template = form.querySelector<HTMLTemplateElement>("[data-filter-chip]")!;
const status = form.querySelector<HTMLElement>("[data-filter-status]")!;
let shown: Set<string> | null = null;
const sync = () => {
list.replaceChildren();
const next = new Set<string>();
const values: Record<string, string> = {};
fields.filter(field => field.value).forEach(field => {
values[field.name] = field.value;
const chip = template.content.firstElementChild!.cloneNode(true) as HTMLElement;
const label = `${field.dataset.filterLabel}: ${field.selectedOptions[0].textContent}`;
chip.querySelector<HTMLElement>("[data-chip-label]")!.textContent = label;
next.add(label);
if (shown?.has(label)) chip.classList.remove("motion-safe:animate-pop");
const button = chip.querySelector("button")!;
button.setAttribute("aria-label", `Remove ${label}`);
button.addEventListener("click", () => { field.value = ""; field.focus(); sync(); });
list.append(chip);
});
shown = next;
const count = Object.keys(values).length;
status.textContent = `${count} active ${count === 1 ? "filter" : "filters"}`;
form.dispatchEvent(new CustomEvent("haze:filter-change", { bubbles: true, detail: values }));
};
form.addEventListener("change", sync);
form.addEventListener("reset", () => setTimeout(sync, 0));
sync();
form.setAttribute("data-enhanced", "");
}
const init = () => document.querySelectorAll<HTMLFormElement>("[data-haze-filter-toolbar]").forEach(enhance);
init();
if (!document.documentElement.hasAttribute("data-haze-filter-toolbar-listener")) {
document.documentElement.setAttribute("data-haze-filter-toolbar-listener", "");
document.addEventListener("astro:page-load", init);
}
</script>When to use
- A project collection needs status and owner facets close to its results.
- Active choices should be visible and individually removable.
When not to use
- The task is plain text matching. Use search-field.
Installation
Add the Haze registry to components.json once, then install Filter Toolbar with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/filter-toolbarnpx shadcn@latest add @haze/filter-toolbaryarn dlx shadcn@latest add @haze/filter-toolbarbunx --bun shadcn@latest add @haze/filter-toolbarFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Form ID and input label prefix. |
| label | string | – | Accessible filter group name. |
| action | string | – | GET form destination. Defaults to the current URL. |
| filters | { key: string; label: string; options: { value: string; label: string }[]; allLabel?: string }[] | – | Facet definitions. allLabel names the first, unfiltered option (default "All"). |
| values | Record<string, string> | – | Initial selected value by filter key. |
| class | string | – | Additional classes on the component wrapper. |
Keyboard interactions
- Arrow keys
- Choose a value in a focused native select.
- EnterSpace
- Remove an active chip or reset and apply filters.
Accessibility
- Each select has a visible label and sends its native value with the GET form. Active chips are enhancements built from the chosen labels.
- Removing a chip returns focus to its select. haze:filter-change carries current key/value pairs for a client-filtered collection.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Filter Toolbar" with `npx shadcn@latest add @haze/filter-toolbar`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import FilterToolbar from "@/components/haze/ui/filter-toolbar.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/filter-toolbar.md
Customize with real content
Adapt the Haze filter-toolbar ui for our product. Replace the demo values of these props with our real content: id, label, action, filters, values, class. Keep the layout, the heading level rules and these accessibility details: Each select has a visible label and sends its native value with the GET form. Active chips are enhancements built from the chosen labels. Removing a chip returns focus to its select. haze:filter-change carries current key/value pairs for a client-filtered collection. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using filter-toolbar, check it fits: use it when a project collection needs status and owner facets close to its results.; active choices should be visible and individually removable.. Avoid it when the task is plain text matching. use search-field.. Alternatives: @haze/search-field, @haze/chip-group, @haze/sortable-data-table.
Dependencies
- npm: @lucide/astro
- @haze/button
- @haze/select
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.