Search Field
NewSearch with a / shortcut, recent searches and highlighted suggestions.
Press / anywhere to jump here.
Installation
$ pnpm dlx shadcn@latest add @synccn/search-fieldFirst time in this project? Connect synccn with one command.
Usage
import { SearchField } from "@/components/ui/search-field";<SearchField suggestions={pages} onSearch={go} />API reference
Generated from the component's TypeScript types, so it always matches the code.
SearchField
Search box with a "/" shortcut, a clear button, recent searches and live suggestions. onSearch fires on Enter or when a suggestion is picked.
- suggestions
string[]Default
[]Terms matched as the user types.
- recent
string[]Default
[]Recent searches shown before typing.
- onSearch
(term: string) => voidCalled on Enter or when a suggestion is picked.
- placeholder
stringDefault
"Search…"Placeholder and accessible name.
- shortcut
string | falseDefault
"/"Key that focuses the field from anywhere.
- className
stringExtra classes for the root element.
Dependencies
Read from the component's imports when the site builds. The CLI installs all of these for you.
Helpers
npm packages
- clsx for cn()
- tailwind-merge for cn()
- lucide-react for Clock, Search, X
Keyboard
- /Focus the field from anywhere on the page (configurable with shortcut)
- ↓or↑Move through suggestions
- EnterSearch for the highlighted suggestion or the typed text
- EscClear the text, or close if it's already empty
Accessibility
- The shortcut is ignored while the user is typing in another field.
From the source
Read from this component's code when the site builds, so it can't drift.
- ARIA roles: combobox, listbox, option
- ARIA attributes: aria-autocomplete, aria-expanded, aria-label, aria-selected
- Keys handled: ArrowUp, ArrowDown, Enter, Escape
- Closes when you click or tap outside
- Flips to whichever side has room, so it stays on screen