Search
A search bar is a pill that holds a query; a search view is what it grows into, a docked panel on desktop and a full-screen view on phones, with recent searches, filters and results as a list. The command palette's text field is the same field.
Install
npx shadcn@latest add @nelumbo/searchSearch bar
A full pill on surface-container-high with no border; it lifts a little on hover and focus. The leading slot holds the search icon or a navigation icon button, trailing slots take icon buttons and an avatar. The clear button springs in as soon as there is text.
Type in the second bar and press Enter.
Search view
Click or tab into the field. The bar's pill morphs into a panel whose height springs with its content, and the leading icon turns into a back arrow. Type to filter, move with ArrowUp and ArrowDown (the highlighted row sits on a raised pill that springs between rows), Enter to open a result, Escape to close; the focus stays in the field. Shift+Delete forgets a recent search, and so does its close button.
Pick a result with the arrow keys and Enter.
Phones
Under 640px the same view opens full screen: the bar grows to fill the screen, the back arrow sits next to the input, the results fill the rest and the page behind stops scrolling. Tab stays inside it. Resize this window below 640px, or open the page on a phone, and open the search above.
Back, Escape or picking a result folds the view back into the bar.
In the command palette
CommandInput is built from the same SearchField: a pill with the search icon and a clear button that springs in, with cmdk's own input inside (type search). Clearing empties cmdk's search whether it is controlled or not.
import { SearchField, searchInputClasses } from "@/components/ui/search"<SearchField size="sm" embedded hasValue={value.length > 0} onClear={() => setValue("")}> <input className={searchInputClasses} value={value} onChange={(e) => setValue(e.target.value)} /></SearchField>Usage
import { SearchBar, SearchView, SearchViewFilters, SearchViewList, SearchViewGroup, SearchViewItem, SearchViewEmpty,} from "@/components/ui/search"<SearchBar label="Search the docs" value={q} onValueChange={setQ} onSubmit={run} /><SearchView value={q} onValueChange={setQ} label="Search contacts" trailing={<IconButton label="Voice" icon="mic" />}> <SearchViewFilters aria-label="Filter by type"> <FilterChip selected={people} onSelectedChange={setPeople}>People</FilterChip> </SearchViewFilters> <SearchViewList aria-label="Results"> <SearchViewGroup heading="Recent searches"> <SearchViewItem icon="history" onSelect={() => setQ("Maya")} onRemove={() => forget("Maya")}> Maya </SearchViewItem> </SearchViewGroup> {results.length === 0 && <SearchViewEmpty>No results</SearchViewEmpty>} </SearchViewList></SearchView>Props
| Prop | Type | Default | Notes |
|---|---|---|---|
SearchBar value / defaultValue / onValueChange | string / string / (value) => void | Controlled or uncontrolled text | |
SearchBar size | "default" | "sm" | "xs" | "default" | 56px, 48px or 40px tall |
SearchBar leading | ReactNode | null | search icon | The search icon, or an IconButton such as menu |
SearchBar trailing / avatar | ReactNode | IconButtons after the clear button; an Avatar at the end | |
SearchBar label | string | placeholder | Names the search landmark and the input; give each bar its own |
SearchBar onSubmit / onClear | (value) => void / () => void | Enter; after the clear button empties the field | |
SearchView open / defaultOpen / onOpenChange | boolean / boolean / (open) => void | Controlled or uncontrolled expansion | |
SearchView value / onValueChange / onSubmit | string / (value) => void | Query text; onSubmit runs on Enter with nothing highlighted | |
SearchView leading / trailing / avatar / size | Like SearchBar; the leading slot turns into a back arrow while open | ||
SearchView closeOnSelect | boolean | true | Close after a result is picked |
SearchViewFilters | div props | A scrolling row for FilterChips; give it an aria-label | |
SearchViewList | div props | The listbox, with the raised pill that springs between rows | |
SearchViewGroup heading | ReactNode | A muted label above a run of options, no rule | |
SearchViewItem onSelect / onRemove | () => void | Enter or click; a remove button and Shift+Delete | |
SearchViewItem icon / leading / description / meta / trailing | Nelumbo ListItem props | ||
SearchViewEmpty | ReactNode | Shown when nothing matches, announced politely | |
SearchField / searchFieldClasses | The shared pill field and its classes, used by Command |