shadcn.io is not affiliated with official shadcn/ui
Expandable Search Navbar Block for React
A navbar for a recipe library whose search button grows a field inside the bar, pushing the links aside on shared grid tracks, with a clear button, an Esc hint and a live count of matching recipes.
Installation
Overview
An expanding-search navbar for React with Base UI, the bar a recipe site keeps above its pages. For Lookoutly, a recipe library, the wordmark leads, four links follow (Browse, Collections, Meal plans, Pantry) and a search button and an Add a recipe Shine Button close the row. The search button grows a field in the bar itself: the links and the field share one grid whose two columns trade places, so the links shrink to nothing as the field takes their width and nothing jumps. The field is an input group with a search icon, an Esc key hint, and, once you type, a count of the twelve recipes that match ("3 of 12") and a clear button. Escape, the button or a click outside folds it back and returns the keyboard to the button; "/" opens it from anywhere on the page that isn't a text field. On a phone the open field covers the bar, and a menu button opens a sheet from the top with the links and Add a recipe. The bar stays one bar high (64 pixels, 56 on a phone) open or closed. It's built from Input Group, Kbd, Sheet, Shine Button, Button and Guides with Lucide icons and your theme's tokens for every colour. For recipe, catalogue and content sites whose search should live in the header without a popup.
FAQ
How do I change the recipes and make the search do something?
Edit RECIPES at the top of the file for the titles the count is taken over, and PAGES for the links. The block counts matches, with no accents or case, as you type; it doesn't list them, because the search that fills a page of results is yours. Wire the field to your router or your index: read query when the user presses Enter (the field sits in a search landmark, ready for a form), and replace countMatches with your own total if it comes from a server.
How does the field push the links aside?
The links and the field are two cells of one grid. Closed, its columns are 1fr 0fr; open, 0fr 1fr, and the column widths transition, which only works because both lists use the same unit (a mix of lengths and fr would jump). Each cell clips along the inline axis, so the links are cut as the column closes, and the cell that is folded away is inert, so it can't be focused or read while it is out of sight. With reduced motion the columns swap at once.
How does it work with a keyboard and a screen reader?
The search button says whether the field is open (aria-expanded) and which landmark it controls (aria-controls), its name changes between "Search recipes" and "Close search", and it declares the "/" key with aria-keyshortcuts on libraries that pass it through. Opening it moves the keyboard into the field a frame later, after the field is free of inert; Escape folds it away and returns the keyboard to the button, wherever it was inside the field. The count is announced in a polite status line ("3 of 12 recipes match"), while the visible count is hidden from screen readers so it isn't read twice. The clear button is named "Clear search" and returns the keyboard to the field.
What happens when I click outside, and does it take my click?
A press anywhere outside the field and its button folds the field away. The click still lands where you pressed: the keyboard goes back to the search button only if nothing else took it, so pressing Add a recipe or a link keeps the keyboard there. What you typed is kept, so opening the field again picks up where you left off.
How does it work on a phone?
Below the md breakpoint the bar shows the wordmark, a search button and a menu button, and nothing wraps at 320 pixels. Opening the search hides the wordmark and the menu button and gives the field the whole bar, with the button turned into a close button in the same place; Escape or that button folds it back. The menu button opens a sheet from the top whose header lines up with the bar, with the four links and Add a recipe (which leaves the bar below the sm breakpoint to make room). Focus moves into the sheet and stays there; Escape, the close button or choosing a link closes it, and focus returns to the menu button.
How does it sit on the page, and can it stick to the top?
The navbar is the page's first block: its + marks and rails line up with every block under it, its top rule falls on the window's top edge, and it draws no bottom border, because the next block's top rule is its bottom edge. To keep the bar in view while the page scrolls, add sticky top-0 z-40 bg-background/80 backdrop-blur to the header. Every link points at #: point them at your pages.
Add a recipe renders the link it's given with render, with nativeButton={false}.
Related
- Pro
Search Overlay Navbar
SearchA navbar for a legal-research database whose field-looking search button opens a panel from the top with a large input, recent searches you can remove, popular topic tags and live results with the matching letters marked.
- Pro
Command Palette Navbar
SearchA navbar for a notes app with a field-looking button and a shortcut that open a command palette: recent notes first, then pages, actions and settings in labelled groups, with the keys that work shown beside each row.
- Pro
A navbar for a support-desk product with a search field whose popup lists help articles and categories in two labelled groups, marks the matching letters, and offers an Ask support link when nothing matches.
- Pro
A navbar for an add-on store with a Categories menu of six categories and their counts, and a search whose popup lists add-ons with an icon tile, drawn stars, an install count and a category.
- /Pro
Top Bar Search Banner
Utility barsA documentation search field in a slim bar: an icon, a labelled input and a slash key hint, with a popup of results that filters as you type.
- Pro
SaaS Marketing Navbar
Mega menusA one-bar marketing navbar for a product roadmap tool: a wordmark, a Product menu of four areas with icons and one-line descriptions, plain links with the current page marked, Sign in and a Shine Button, and a sheet with the same links on phones.