Refine SearchNew
A "Refine your search" panel above search results where the AI proposes a few filters, each with the count it would leave, and one click applies it as a removable chip.
Refine your search
Finding ways to narrow these results
There are 11 search results sorted by latest update
| Pattern | Job | Status | Updated |
|---|---|---|---|
| Tool Approval | Oversee | Stable | Nov 9 |
| Human in the Loop | Oversee | Stable | Nov 6 |
| Background Tasks | Oversee | Beta | Nov 4 |
| Agent Run Card | Oversee | Beta | Nov 2 |
| Plan Review | Steer | Stable | Oct 30 |
| Agent Triggers | Steer | Beta | Oct 27 |
| Procedure Builder | Steer | Beta | Oct 24 |
| Create Agent | Start | Stable | Oct 20 |
| Agent Setup Pill | Start | Beta | Oct 18 |
| Multi-Agent Trace | Oversee | Beta | Oct 15 |
| Checkpoint Restore | Recover | Stable | Oct 11 |
"use client";
import * as React from "react";
import { AnimatePresence, LayoutGroup, motion, useReducedMotion } from "motion/react";
import { RotateCcw, Search, Sparkles, X } from "lucide-react";
import { cn } from "@/lib/utils";
export interface RefineSearchColumn<Row> {
key: string;
label: string;
/** Renders the cell; defaults to `String(row[key])`. */
render?: (row: Row) => React.ReactNode;
className?: string;
}
/** A filter the AI proposes for the current results. */
export interface RefineProposal<Row> {
id: string;
/** What the filter narrows on, e.g. "Status". Shown on the applied chip. */
field: string;
/** The value, e.g. "Beta". Shown on the proposal pill. */
label: string;
test: (row: Row) => boolean;
}
export interface RefineSearchProps<Row extends { id: string }> {
columns: RefineSearchColumn<Row>[];
rows: Row[];
/** Text the keyword search matches against. */
searchText: (row: Row) => string;
/**
* Proposes filters for the current results. Counts are computed here from
* `results`, not trusted from the model, and proposals that wouldn't narrow
* the set (0 rows, or every row) are dropped.
*/
onPropose: (query: string, results: Row[], applied: RefineProposal<Row>[]) => Promise<RefineProposal<Row>[]>;
initialQuery?: string;
placeholder?: string;
/** Completes "There are N search results sorted by …". */
sortLabel?: string;
/** Most proposals shown at once. */
maxProposals?: number;
className?: string;
}
export function RefineSearch<Row extends { id: string }>({
columns,
rows,
searchText,
onPropose,
initialQuery = "",
placeholder = "Search",
sortLabel = "latest update",
maxProposals = 3,
className,
}: RefineSearchProps<Row>) {
const reduceMotion = useReducedMotion();
const [draft, setDraft] = React.useState(initialQuery);
const [query, setQuery] = React.useState(initialQuery);
const [applied, setApplied] = React.useState<RefineProposal<Row>[]>([]);
/** The last proposal response, tagged with the result set it was for. */
const [response, setResponse] = React.useState<{ key: string; proposals: RefineProposal<Row>[] } | null>(null);
const [dismissed, setDismissed] = React.useState(false);
const requestId = React.useRef(0);
const results = React.useMemo(() => {
const q = query.trim().toLowerCase();
return rows.filter(
(row) => (!q || searchText(row).toLowerCase().includes(q)) && applied.every((f) => f.test(row))
);
}, [rows, query, applied, searchText]);
// Re-propose whenever the result set changes: a new search or a filter
// added/removed. Responses for an older result set are ignored, and the
// panel shows its loading state until the current one arrives.
const key = JSON.stringify([query, applied.map((f) => f.id)]);
React.useEffect(() => {
const id = ++requestId.current;
const appliedKeys = new Set(applied.map((f) => `${f.field}:${f.label}`));
onPropose(query, results, applied)
.then((next) => {
if (id !== requestId.current) return;
setResponse({
key,
proposals: next
.filter((p) => !appliedKeys.has(`${p.field}:${p.label}`))
.filter((p) => {
const n = results.filter(p.test).length;
return n > 0 && n < results.length;
})
.slice(0, maxProposals),
});
})
// A failed request collapses the panel; search keeps working.
.catch(() => {
if (id === requestId.current) setResponse({ key, proposals: [] });
});
// `key` captures query + applied; `results` derives from them.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [key]);
const status = response?.key === key ? "ready" : "loading";
const proposals = status === "ready" ? response!.proposals : [];
function submit(e: React.FormEvent) {
e.preventDefault();
if (draft.trim() === query.trim()) return;
setQuery(draft.trim());
setDismissed(false);
}
function apply(p: RefineProposal<Row>) {
setApplied((prev) => [...prev, p]);
}
function remove(id: string) {
setApplied((prev) => prev.filter((f) => f.id !== id));
}
function reset() {
setApplied([]);
setDraft("");
setQuery("");
setDismissed(false);
}
const showPanel = !dismissed && (status === "loading" || proposals.length > 0);
const transition = reduceMotion ? { duration: 0 } : { type: "spring" as const, bounce: 0.15, duration: 0.4 };
return (
<div className={cn("w-full overflow-hidden rounded-2xl border bg-card text-sm", className)}>
<LayoutGroup>
<div className="flex flex-col gap-3 p-3">
{/* Search */}
<form onSubmit={submit} role="search" className="flex h-10 items-stretch overflow-hidden rounded-lg border bg-background focus-within:ring-2 focus-within:ring-ring/40">
<input
value={draft}
onChange={(e) => setDraft(e.target.value)}
placeholder={placeholder}
aria-label="Search"
className="min-w-0 flex-1 bg-transparent px-3 outline-none placeholder:text-muted-foreground"
/>
<button
type="submit"
aria-label="Run search"
className="flex w-10 shrink-0 items-center justify-center border-l text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
>
<Search className="size-4" />
</button>
</form>
{/* Applied filters */}
<AnimatePresence initial={false}>
{applied.length > 0 && (
<motion.div
initial={{ height: 0, opacity: 0 }}
animate={{ height: "auto", opacity: 1 }}
exit={{ height: 0, opacity: 0 }}
transition={{ duration: reduceMotion ? 0 : 0.18 }}
className="-mb-1 overflow-hidden"
>
<ul aria-label="Applied filters" className="flex flex-wrap items-center gap-1.5 pb-1">
<AnimatePresence initial={false} mode="popLayout">
{applied.map((f) => (
<motion.li
key={f.id}
layoutId={`refine-${f.id}`}
transition={transition}
exit={{ opacity: 0, scale: 0.9 }}
className="flex h-7 items-center gap-1.5 rounded-md border bg-background pl-2.5 pr-1 text-xs"
>
<Sparkles className="size-3 shrink-0 text-muted-foreground" aria-label="Suggested by AI" />
<span className="text-muted-foreground">{f.field}</span>
<span>{f.label}</span>
<button
type="button"
onClick={() => remove(f.id)}
aria-label={`Remove filter: ${f.field} ${f.label}`}
className="flex size-5 items-center justify-center rounded text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
>
<X className="size-3" />
</button>
</motion.li>
))}
</AnimatePresence>
<li>
<button
type="button"
onClick={reset}
className="flex h-7 items-center gap-1 rounded-md px-2 text-xs text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
>
<RotateCcw className="size-3" aria-hidden />
Reset
</button>
</li>
</ul>
</motion.div>
)}
</AnimatePresence>
{/* AI proposals */}
<AnimatePresence initial={false}>
{showPanel && (
<motion.section
key="refine"
aria-labelledby="refine-search-title"
aria-busy={status === "loading"}
initial={{ height: 0, opacity: 0 }}
animate={{ height: "auto", opacity: 1 }}
exit={{ height: 0, opacity: 0 }}
transition={{ duration: reduceMotion ? 0 : 0.2 }}
className="overflow-hidden rounded-lg bg-muted/60"
>
<div className="flex flex-col gap-2.5 px-3 py-3">
<div className="flex items-center gap-1.5">
<Sparkles
className={cn(
"size-3.5 shrink-0 text-muted-foreground",
status === "loading" && !reduceMotion && "animate-pulse"
)}
aria-hidden
/>
<h3 id="refine-search-title" className="text-xs font-medium">
Refine your search
</h3>
<button
type="button"
onClick={() => setDismissed(true)}
aria-label="Hide suggestions"
className="ml-auto flex size-6 items-center justify-center rounded text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
>
<X className="size-3.5" />
</button>
</div>
<div className="flex min-h-7 flex-wrap gap-1.5">
{status === "loading" ? (
<span role="status" className="flex flex-wrap gap-1.5">
<span className="sr-only">Finding ways to narrow these results</span>
{[64, 84, 72].map((w) => (
<span
key={w}
aria-hidden
style={{ width: w }}
className={cn("h-7 rounded-md border bg-background/60", !reduceMotion && "animate-pulse")}
/>
))}
</span>
) : (
proposals.map((p, i) => {
const count = results.filter(p.test).length;
return (
<motion.button
key={p.id}
layoutId={`refine-${p.id}`}
type="button"
onClick={() => apply(p)}
initial={{ opacity: 0, y: 4 }}
animate={{ opacity: 1, y: 0 }}
transition={{ ...transition, delay: reduceMotion ? 0 : i * 0.05 }}
title={`${p.field}: ${p.label}`}
aria-label={`Refine by ${p.field} ${p.label}, ${count} results`}
className="flex h-7 items-center gap-1.5 rounded-md border bg-background px-2.5 text-xs transition-colors hover:bg-accent"
>
<span>{p.label}</span>
<span className="text-muted-foreground tabular-nums">({count})</span>
<Search className="size-3 text-muted-foreground" aria-hidden />
</motion.button>
);
})
)}
</div>
</div>
</motion.section>
)}
</AnimatePresence>
</div>
</LayoutGroup>
{/* Result count */}
<p className="border-t px-4 py-2.5 text-xs text-muted-foreground" aria-live="polite">
There {results.length === 1 ? "is" : "are"}{" "}
<span className="font-medium text-foreground tabular-nums">{results.length}</span> search{" "}
{results.length === 1 ? "result" : "results"} sorted by {sortLabel}
</p>
{/* Results */}
<div className="max-h-72 overflow-auto border-t">
<table className="w-full border-collapse whitespace-nowrap" aria-busy={status === "loading"}>
<thead className="sticky top-0 z-[1] bg-card">
<tr className="border-b text-left text-xs text-muted-foreground">
{columns.map((c) => (
<th key={c.key} scope="col" className={cn("px-4 py-2 font-medium", c.className)}>
{c.label}
</th>
))}
</tr>
</thead>
<tbody>
<AnimatePresence initial={false}>
{results.map((row) => (
<motion.tr
key={row.id}
layout={reduceMotion ? false : "position"}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: reduceMotion ? 0 : 0.18 }}
className="border-b last:border-b-0"
>
{columns.map((c) => (
<td key={c.key} className={cn("px-4 py-2.5", c.className)}>
{c.render ? c.render(row) : String((row as Record<string, unknown>)[c.key] ?? "")}
</td>
))}
</motion.tr>
))}
</AnimatePresence>
</tbody>
</table>
{results.length === 0 && (
<div className="flex flex-col items-center gap-2 px-4 py-10 text-center">
<p className="text-muted-foreground">No results match this search.</p>
<button
type="button"
onClick={reset}
className="text-xs font-medium underline-offset-2 hover:underline"
>
Reset search
</button>
</div>
)}
</div>
</div>
);
}
A UX spec for this pattern — written for agents implementing or reusing it, not the code.
# Refine Search
## Summary
A "Refine your search" panel between a search field and its results, where the AI proposes a few filters that would usefully narrow the current result set — each shown as a pill with the number of results it would leave ("Beta (6)"). One click applies a proposal as an ordinary, removable filter; the results and the proposals update together. The user keeps searching the way they already do; the AI only points out the facets worth cutting on.
## When to use
- Search over large, faceted datasets (regulations, documents, records, a pattern catalogue) where a typical query returns too many results and the right facet to narrow on isn't obvious.
- When the filter UI is rich but buried ("Add filters" menus with many fields), so people rarely discover which facet would help.
- When the best next facet depends on the query and the results — something a static facet sidebar can't know.
## When not to use
- Result sets that are already short enough to scan.
- When there is a fixed, well-known facet sidebar people already use; don't duplicate it with an AI panel.
- To rewrite the query itself or answer the question — use a describe-style or natural-language interface instead.
## Anatomy
- **Search field** — the existing keyword search, with its submit button.
- **Applied filters** — removable chips for proposals the user accepted, each marked as AI-suggested, plus a Reset action.
- **Refine panel** — a soft, inset container with a sparkle and the title "Refine your search", and a dismiss (×) button.
- **Proposal pills** — a value label, the resulting count in parentheses, and a trailing search icon that says "this runs a search". Two to three at most.
- **Loading state** — skeleton pills in the panel while proposals are generated; the panel keeps its height.
- **Result summary** — "There are 22 search results sorted by latest update", above the table.
## Behavior
- Proposals are requested after each search and after every filter is added or removed, for the result set as it now stands. Stale responses are dropped.
- Counts are computed by the product from the real results, never taken from the model. Proposals that would leave zero rows, or every row, are not shown.
- Clicking a pill moves it into the applied filters (a short shared-layout move from panel to chip row), filters the results, and the panel reloads with proposals for the narrower set.
- Applied chips are ordinary filters: removing one restores the wider set and its proposals. Reset clears filters and query.
- If no proposal is worth showing, or the request fails, the panel collapses quietly — the search itself never depends on it.
- Dismissing the panel hides it until the next new search.
## Content guidelines
- Title: "Refine your search" — an invitation, not "AI suggestions".
- Pill labels use the dataset's own vocabulary and values ("Active", "Proposed", "Beta"), not sentences. Keep them to one or two words; the field name goes on the applied chip and the tooltip.
- Always show the count. It's what lets someone pick a refinement without trying each one.
- The result summary states the count and the sort order in plain words.
## Accessibility
- The search is a `role="search"` form; the panel is a labelled `<section>` with `aria-busy` while loading and a `role="status"` message ("Finding ways to narrow these results").
- Each pill is a `<button>` labelled with field, value and count ("Refine by Status Beta, 6 results"); the trailing icon is decorative.
- Applied chips are a labelled list; each remove button names its filter. The AI marker has a text label ("Suggested by AI").
- The result summary is `aria-live="polite"`, so applying or removing a filter announces the new count.
- Respect `prefers-reduced-motion`: drop the shared-layout move, stagger and skeleton pulse.
## Related patterns
- **Describe Filters** — Use it instead when people can say what they want in their own words and the AI should build the filters.
- **Natural Language Query** — Use it instead when people want to see and edit the query behind the results.
- **Follow-up List** — Use it instead for suggested next questions after an answer, rather than narrowing a result set.
- **Confidence Indicator** — Use it as well when a proposal matches by meaning and the model can say how sure it is.
Do’s and don’ts
Do use it for
- Search over large, faceted datasets (regulations, documents, records, a pattern catalogue) where a typical query returns too many results and the right facet to narrow on isn't obvious.
- When the filter UI is rich but buried ("Add filters" menus with many fields), so people rarely discover which facet would help.
- When the best next facet depends on the query and the results — something a static facet sidebar can't know.
Don’t use it for
- Result sets that are already short enough to scan.
- When there is a fixed, well-known facet sidebar people already use; don't duplicate it with an AI panel.
- To rewrite the query itself or answer the question — use a describe-style or natural-language interface instead.
Related patterns
Describe Filters
Use it instead when people can say what they want in their own words and the AI should build the filters.
Natural Language Query
Use it instead when people want to see and edit the query behind the results.
Confidence Indicator
Use it as well when a proposal matches by meaning and the model can say how sure it is.
