shadcn.io is not affiliated with official shadcn/ui
Search Input with Recent Searches Form Block for React
A catalogue search form with a clear button, a slash shortcut, Escape to clear, results marked as matches in a held-height region, an empty state with a way back and recent searches as removable chips.
Installation
Overview
Give a search field every affordance people expect, with this search input form for React with Base UI. A compact header sits over a hairline frame with a plus mark on each corner, built on a real search landmark: a search band, a band of recent searches, a results band and an action foot. The field is an Input Group with a leading icon, a clear button that appears with text and a Kbd hint for the slash key, which hides on touch; the slash key focuses the field while focus is inside the block and Escape clears it. A search runs on Enter or the one Shine Button, never as you type: under two characters it says so then; otherwise the matching titles of fourteen appear in a scrolling region of fixed height with each match marked by Search Highlight Text, or an Empty state names what matched nothing and offers a way back. Each search joins the recent ones, which are chips you can run again or remove. It's built from Input Group, Kbd, Search Highlight Text, Empty, Badge, Button, Shine Button and Guides, with a CSS entrance only. For a library, a docs site, an admin list or any catalogue with a search box.
FAQ
How do I search my own data?
Replace TITLES with your records and change matches, which compares what was typed with the title and the creator after removing accents and case, so "resume" finds "Résumé". The results, the marks, the count and the empty state all follow from it. For a large catalogue run the search on your server in the submit handler, keep the results in the same region and keep its fixed height so the panel never moves.
Why does it search on Enter and not as you type?
A search that runs on Enter is a form: it works with a keyboard, a screen reader and a phone's Search key, it can be a plain GET request to your search page, and it needs no debounce. The cost is one more press; for a live filter of a list that is already on the page, choose the table search block instead. Under two characters the field says so on Enter, never while you are typing.
How do the slash key and Escape work?
While focus is inside the block, pressing the slash key (when you are not typing in a field) moves focus to the search field, and its hint hides on touch screens, which have no key for it. Escape in the field clears the text and the results and puts the block back to its prompt. Both are scoped to the block, so they never take over the page's own shortcuts.
How do the recent searches work, and where are they kept?
Each search goes to the front of a list of at most five, without repeats; a chip runs it again and its X removes it, moving focus to the next chip, or to the field when none is left. They live in the page's state and are gone on reload. To keep them, store the list in localStorage in a try and catch (it can be refused), read it in an effect after mount and keep a way to clear it.
How are results announced, and is the region accessible?
One polite status line in the foot says what a search found ("3 results for “ham”") or why it did not run, and nothing is announced while you type. The results are a list in a region named Search results that you can scroll with the keyboard; on a phone the recent chips scroll sideways.
How do I connect it to react-hook-form or TanStack Form?
A search box rarely needs a form library: give the field a name (it is q), keep the form a GET request to your search page and read the value in the submit handler. If you do use react-hook-form, register the field with Controller and call your search from handleSubmit. The two-character rule belongs in the submit handler, so it speaks on Enter and not while typing.
Related
- Pro
A workspace's search as a panel in the page: a field in the first band, results grouped as Pages, Documentation, Team and Settings with their counts, arrow keys that walk them, Enter that opens one locally, and an empty state that names the query.
- Pro
Text Input with Address Preview Form
Text fieldsA form panel with one required project-name field that shows the web address it will get as you type, flags a name that is taken or past 50 characters, and offers the next free address.
- Pro
Searchable Catalogue Table
Sort and searchA library catalogue table with a search field and an availability menu: typing keeps the titles whose name or author holds the words and marks them, the foot says how many match, and an empty state under the head clears the search.
- Pro
URL Input with Paste Cleanup Form
Text fieldsA website field with a fixed https:// in front that cleans what you paste, then reads the address with the browser's URL parser into Host, Path and Secure cells.
- Pro
Email and Password Form
Text fieldsThe sign-in pair done well: an email named for password managers, a password with a reveal button and a Caps Lock warning, one generic failure that never says which half was wrong, and a quiet band with the demo account.
- Pro
Minimal Single Question Form
Text fieldsThe calmest form: one page with a mark, a heading that asks one question, one field with a hidden label and one full-width button, with the answer or the one problem shown under the field.