Molecules · Search
Search
A search field inside a search landmark (a region of the page that screen readers can jump straight to). Enter submits, Escape clears, and a one-key shortcut can move focus to it from anywhere on the page.
- Best for
- Searching the content of the page that shows the results, inside a search landmark.
- Not for
- Suggestions that appear as you type, or jumping to an action: use CommandPalette.
This page as Markdown, a plain-text copy for a coding agent (an AI tool that writes code).
Examples
Default
Code
tsx
<div className="max-w-xs w-full"> <Search label="Search the catalogue" /></div>With a shortcut
Press / anywhere on this page to focus it.
Code
tsx
<div className="max-w-xs w-full"> <Search label="Search the manifest" shortcut="/" /> <p className="font-body text-xs text-[var(--color-text-secondary)] mt-3"> Press / anywhere on this page to focus it. </p></div>Submitting
Press Enter to search.
Code
tsx
function SubmittingExample() { const [result, setResult] = useState<string | null>(null) return ( <div className="max-w-xs w-full"> <Search label="Search the depot list" onSearch={query => setResult(query)} /> <p className="font-body text-xs text-[var(--color-text-secondary)] mt-3"> {result ? `Searched for: ${result}` : 'Press Enter to search.'} </p> </div> )}<SubmittingExample />Sizes
Code
tsx
<div className="max-w-xs w-full"> <Search label="Search, small" placeholder="Small" inputSize="sm" /></div><div className="max-w-xs w-full"> <Search label="Search, medium" placeholder="Medium" inputSize="md" /></div><div className="max-w-xs w-full"> <Search label="Search, large" placeholder="Large" inputSize="lg" /></div>Anatomy
| Part | What it is |
|---|---|
| Landmark | A form with role=search, named by label, so people using a screen reader can jump to it. |
| Icon | Decoration only. It's aria-hidden, so screen readers skip it. |
| Field | An Input of type search. The browser's own clear button is hidden, because you can't reach it with the keyboard. |
| Clear button | Only shows while there's text. It puts focus back in the field. |
| Shortcut hint | A Kbd (the keyboard-key badge), aria-hidden, and only shown while the field is empty. Screen readers get the shortcut from aria-keyshortcuts on the field instead. |
Props
- labelstring
- Required. The name screen readers use for the field, and for the search landmark.
- valuestring
- The value, when your page holds it (controlled).
- defaultValuestringDefault ''
- Starting value when the component holds it itself (uncontrolled).
- onValueChange(value: string) => void
- Called whenever the value changes, clearing included.
- onSearch(query: string) => void
- Called on Enter with the value, spaces trimmed from both ends. It isn't called for an empty query.
- placeholderstringDefault 'Search'
- shortcutstring
- One printable key, for example "/". It moves focus to the field from anywhere on the page, except while someone is typing in another text field. While the field is empty, it shows as a Kbd hint.
- inputSize'sm' | 'md' | 'lg'Default 'md'
- classNamestring
- Combined with the root classes. Where Tailwind utility classes clash, the later one wins.
- refRef<HTMLInputElement>
- Passed through to the input element.
Keyboard
| Keys | Result |
|---|---|
| Enter | Submits the query, spaces trimmed. |
| Escape | Clears the field. |
| / | Moves focus to the field, unless another text field has focus. It works for whichever key the shortcut prop names. |
| Tab | Moves between the field and the clear button. |
Do and don't
Do
- Name what's being searched: 'Search the catalogue', not 'Search'.
- Pick a shortcut that isn't a letter people type in forms; '/' is the usual one.
- Keep results on the page that owns the search, not in a popup under the field.
Don't
- Don't add suggestions that appear as you type. That's the command palette's job, and it has a listbox built for it.
- Don't search on every keystroke without saying so. Results that shift while someone types are hard to follow with a screen reader.
- Don't put two search landmarks on one page without giving them different labels.