This page as markdown: /search-input.md · Index for agents: /llms.txt · Everything in one file: /llms-full.txt

SearchInput

Status: stable

Styled search field with a leading magnifier and a clearable X. Soft-square (rounded-sm), the theme's field surface, 2px focus ring; the clear button appears only when there is a value. size md (form), sm (toolbar, matching Pill lg and Button sm) or xs (inset tool-panel field).

Keywords: search, input, filter, query

Usage

Use SearchInput for text queries that filter or search a known content set.

Best practices

Props

Name Type Default Description
size "md" | "sm" | "xs" "md" The rung of the control ladder, named as on Input, Select and Combobox. md is the form field (--control-md, 40px / 32 compact). sm is the toolbar field (--control-sm, 32px / 28 compact), the rung Pill lg and Button sm share, so filters + search + actions in one row are one height. xs is the tool-panel field (--control-xs, 28px / 24 compact) on Input xs's inset panel-field surface. The magnifier, inline padding and clear button scale with the rung and stay vertically centred.
value string — Controlled value of the search field.
defaultValue string — Uncontrolled initial value. The field tracks its own text, so the clear button still appears and empties it.
className string — Sizes and places the whole field — magnifier, input and clear button together (e.g. max-w-80). A width cap here keeps the clear button inside the field.
inputClassName string — Classes for the element alone, for the rare input-only override.
onValueChange (value: string) => void — Called with the next string on every keystroke.
onClear () => void — Called when the clear button is pressed. The component also fires onValueChange('') so a controlled value resets.
clearLabel string "Clear search" Accessible name of the clear button.
placeholder string "Search" Placeholder text shown when the field is empty.
disabled boolean false Dims the control and blocks pointer interaction.
...props ComponentPropsWithRef<"input"> — All native input attributes (name, autoFocus, ref, aria-invalid, …) pass through to the except value/onChange, which are owned by value/onValueChange, className, which sizes the whole field, and the native numeric size attribute, which the size prop replaces.

Tokens used

field-border, field, foreground, muted-foreground, ring, destructive, panel-field, panel-field-hover

Motion

Accessibility

Related

Examples

Controlled

import { SearchInput } from "@sableui/react"

const [q, setQ] = useState("")
<SearchInput value={q} onValueChange={setQ} placeholder="Search posts" />

With onClear

import { SearchInput } from "@sableui/react"

<SearchInput value={q} onValueChange={setQ} onClear={() => console.log("cleared")} />

Uncontrolled, sized as a whole

import { Field, SearchInput } from "@sableui/react"

<Field label="Search posts"><SearchInput defaultValue="roadmap" className="max-w-80" /></Field>

Highlighting matches in the results

import { SearchInput, useMatchHighlight } from "@sableui/react"

const [q, setQ] = useState("")
const results = useMatchHighlight<HTMLUListElement>(q, { selector: "[data-title]" })
const shown = posts.filter((post) => post.title.toLowerCase().includes(q.trim().toLowerCase()))

<SearchInput value={q} onValueChange={setQ} placeholder="Search posts" />
<ul ref={results}>
	{shown.map((post) => (
		<li key={post.id}><span data-title>{post.title}</span></li>
	))}
</ul>

Filters with search

import { Pill, Row, SearchInput } from "@sableui/react"

const [filter, setFilter] = useState("all")
const [q, setQ] = useState("")

<Row gap={2} align="center" justify="between" wrap>
	<Row gap={1}>
		<Pill size="lg" selected={filter === "all"} onClick={() => setFilter("all")}>All 8</Pill>
		<Pill size="lg" selected={filter === "products"} onClick={() => setFilter("products")}>Products 5</Pill>
		<Pill size="lg" selected={filter === "oss"} onClick={() => setFilter("oss")}>Open source 2</Pill>
	</Row>
	<SearchInput size="sm" value={q} onValueChange={setQ} placeholder="Find a project…" aria-label="Find a project" className="max-w-64" />
</Row>

Sizes

import { SearchInput, Stack } from "@sableui/react"

<Stack gap={1}>
	<SearchInput size="md" placeholder="Search posts" aria-label="Search posts" />
	<SearchInput size="sm" placeholder="Find a project…" aria-label="Find a project" />
	<SearchInput size="xs" placeholder="Filter layers" aria-label="Filter layers" />
</Stack>

Disabled

import { SearchInput } from "@sableui/react"

<SearchInput disabled placeholder="Search" />