component
Search field
A search box with a clear button, an inline error or note and an optional help popover.
Installation
$ npx shadcn@latest add https://www.pokerstudy.ai/r/search-field.jsonProps
Generated from the component source with react-docgen-typescript.
SearchField
| Prop | Type | Default | Description |
|---|---|---|---|
| error | string | null | – | Error to show under the field (marks it invalid). |
| examples | { code: string; text: string; }[] | – | Clickable examples at the top of the help popover. |
| help | ReactNode | – | Help popover body; the ? button appears only when given. |
| helpLabel | string | Search help | – |
| label * | string | – | Screen-reader label for the field. |
| note | string | null | – | A non-error message under the field. |
| onChange * | (next: string) => void | – | – |
| placeholder | string | – | – |
| value * | string | – | – |
Source
components/blocks/search-field.tsx
"use client";
// Search field with a clear button and an optional help popover. Built from
// the React Bits Pro App UI input box and popover (navbar-2), mapped onto this
// app's tokens. Used by the Explorer's hand search.
import { useEffect, useId, useRef, useState, type ReactNode } from "react";
import { CircleHelp, Search, X } from "lucide-react";
export function SearchField({
value,
onChange,
label,
placeholder,
error,
note,
helpLabel = "Search help",
examples,
help,
}: {
value: string;
onChange: (next: string) => void;
/** Screen-reader label for the field. */
label: string;
placeholder?: string;
/** Error to show under the field (marks it invalid). */
error?: string | null;
/** A non-error message under the field. */
note?: string | null;
helpLabel?: string;
/** Clickable examples at the top of the help popover. */
examples?: { code: string; text: string }[];
/** Help popover body; the ? button appears only when given. */
help?: ReactNode;
}) {
const [open, setOpen] = useState(false);
const wrap = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLInputElement>(null);
const helpId = useId();
const msgId = useId();
useEffect(() => {
if (!open) return;
const onDown = (e: PointerEvent) => {
if (wrap.current && !wrap.current.contains(e.target as Node))
setOpen(false);
};
const onKey = (e: KeyboardEvent) => {
if (e.key === "Escape") setOpen(false);
};
document.addEventListener("pointerdown", onDown);
document.addEventListener("keydown", onKey);
return () => {
document.removeEventListener("pointerdown", onDown);
document.removeEventListener("keydown", onKey);
};
}, [open]);
return (
<div ref={wrap} className="relative flex flex-col gap-1.5">
<div className="flex items-center gap-2">
<label
className={`relative flex h-9 min-w-0 flex-1 cursor-text items-center gap-2 rounded-[var(--rb-r-sm,6px)] border bg-background px-2.5 shadow-[0_1px_2px_-1px_rgb(0_0_0/0.12)] transition-[border-color,box-shadow] focus-within:ring-3 focus-within:ring-foreground/8 ${
error
? "border-destructive"
: "border-border hover:border-foreground/30 focus-within:border-foreground/60"
}`}
>
<Search
aria-hidden
className="size-4 shrink-0 text-muted-foreground"
/>
<span className="sr-only">{label}</span>
<input
ref={inputRef}
value={value}
onChange={(e) => onChange(e.target.value)}
placeholder={placeholder}
spellCheck={false}
autoCapitalize="off"
autoComplete="off"
aria-invalid={error ? true : undefined}
aria-describedby={error || note ? msgId : undefined}
className="h-full min-w-0 flex-1 bg-transparent font-mono text-[13px] text-foreground outline-none placeholder:font-sans placeholder:text-muted-foreground"
/>
{value ? (
<button
type="button"
aria-label="Clear search"
onClick={() => {
onChange("");
inputRef.current?.focus();
}}
className="inline-flex size-6 items-center justify-center rounded-[var(--rb-r-xs,4px)] text-muted-foreground hover:bg-accent hover:text-foreground"
>
<X className="size-3.5" />
</button>
) : null}
</label>
{help ? (
<button
type="button"
aria-label={helpLabel}
aria-expanded={open}
aria-controls={helpId}
onClick={() => setOpen((o) => !o)}
className={`inline-flex size-9 shrink-0 items-center justify-center rounded-[var(--rb-r-sm,6px)] border border-border bg-background text-muted-foreground shadow-[0_1px_2px_-1px_rgb(0_0_0/0.12)] transition-colors hover:border-foreground/30 hover:text-foreground ${
open ? "bg-accent text-foreground" : ""
}`}
>
<CircleHelp className="size-4" />
</button>
) : null}
</div>
{error ? (
<p id={msgId} className="text-xs text-destructive">
{error}
</p>
) : note ? (
<p id={msgId} className="text-xs text-muted-foreground">
{note}
</p>
) : null}
{open ? (
<div
id={helpId}
role="dialog"
aria-label={helpLabel}
className="absolute right-0 top-11 z-30 flex max-h-[min(70vh,560px)] w-[min(440px,calc(100vw-2rem))] flex-col gap-4 overflow-y-auto rounded-[var(--rb-r-lg,10px)] border border-border bg-popover p-4 text-popover-foreground shadow-[0_12px_24px_-12px_rgb(0_0_0/0.35)]"
>
{examples?.length ? (
<section className="flex flex-col gap-1.5">
<h3 className="text-[12px] font-medium uppercase tracking-wide text-muted-foreground">
Examples
</h3>
{examples.map(({ code, text }) => (
<button
key={code}
type="button"
onClick={() => {
onChange(code);
setOpen(false);
inputRef.current?.focus();
}}
className="grid grid-cols-[minmax(0,9.5rem)_1fr] items-baseline gap-3 rounded-[var(--rb-r-xs,4px)] px-1 py-0.5 text-left hover:bg-accent"
>
<code className="truncate font-mono text-[12px] text-foreground">
{code}
</code>
<span className="text-[12px] text-muted-foreground">
{text}
</span>
</button>
))}
</section>
) : null}
{help}
</div>
) : null}
</div>
);
}