shadcn.io is not affiliated with official shadcn/ui
Basic Command Menu Block for React
A team workspace's home page with its command menu shown open: eleven pages in three groups, a shortcut on the five most used, matches marked, an empty state that says what to try and a foot with the key hints and a spoken count.
Installation
Overview
Jump to any page from the keyboard with this command menu for React with Base UI. It opens from the team home of Paper Kite, a workspace for projects, documents and people, whose header holds an outline search field with its ⌘ K cap, and the dashboard behind it is drawn under a soft scrim inside a hairline frame with a plus mark on each corner. The menu is the catalog Command: a search field over eleven pages in three groups, Workspace, Analytics and Settings, each with an icon, the letters you typed marked with Search Highlight Text, and a Kbd shortcut on the five most used. An empty search says what to try instead of "No results", and the foot lists the keys and counts the pages, which is also spoken once typing pauses. Choosing a page, or pressing its shortcut while the menu is open, closes the menu, moves focus back to the search field and changes the title of the drawn page, and the status line says where you went. For preview the menu is drawn open in place when the page loads; the search field, or Ctrl or ⌘ K while focus is in the block, opens the real command dialog with the same pages and the same query. For any app with more than a handful of pages.
FAQ
Why is the menu already open, and what do I keep?
So you can see it without a click. On load the menu is the Command drawn inline over a picture of the dashboard, in a plain shell that is not modal, so nothing is trapped, hidden or focused and the page around the preview keeps working. The search field opens the real command dialog, modal, with its focus trap, Escape and focus return, and the inline copy stays closed until a reload. Both show the same Palette component and share the block's state, so what you typed in the copy is in the real dialog. In your app keep the dialog and drop the copy: the "in-place" state, the scrim with its shell and the Escape and blur effects.
Do the shortcuts on the rows work?
Yes, while the menu is open: Alt and the letter, or Option on a Mac, goes to that page and closes the menu. The block reads the physical key (event.code), so Option's special characters never matter, and it ignores the chord when Ctrl or Command is also down. The caps say Alt or Option to match the system, swapped in after the page loads. The keys are not global: they work only while the menu is open, because the app around it isn't part of this block. In your app bind them wherever the page should listen.
How does the search match?
matchesOf keeps the pages whose name or hidden keywords hold what you typed, ignoring case and accents, in their listed order, so "slack" finds Integrations through its keywords. The letters that match in the name are marked. A group with no match disappears, and with nothing left an empty state names what you typed and suggests a page or a setting.
What does choosing a page do, and how do I wire it up?
Here it changes the title in the drawn page's bar and the status line says "Went to Projects. In this demo only." Nothing loads. In your app call your router in pick, for example router.push(page.href), and give each page an href in PAGES.
Is it accessible from the keyboard?
Yes. Type to search, the arrow keys move through the rows, Enter goes to the highlighted page and Escape closes the menu and puts focus on the search field; Ctrl or ⌘ K opens the dialog while focus is inside the block, and only then, because the site's own search owns the key everywhere else. The rows are options of a cmdk listbox; the block turns off its own fuzzy filter and lists the rows itself. The real dialog is named by its title and described by its sentence, and the count is spoken from inside it.
Related
- 333Pro
Global Search Command Menu
Search everythingA company intranet's command menu shown open: one input searches pages, docs, people and tags at once, each type is a group of three matches with a row that opens it in place, and the foot counts the results by type.
- Pro
Navigation Command Menu
Pages and docsA bank back office's navigation menu shown open: 24 pages in three levels, each row the name over its whole path, a search that reads the path by the start of each word, and a tick on the page you are on.
- Pro
Actions Command Menu
Actions and keysA signing tool's document with its actions menu shown open: nine actions in four groups, a key on the five that change the document that runs them while the menu is open, and a drawn lease whose badge and signature follow.
- Pro
Recent Items Command Menu
Search everythingA project hub's command menu shown open on what you opened lately: Today, Yesterday and Earlier buckets before you type, recent matches first when you do, opening a row moves it to the top, and a last row clears the list.
- Pro
Command Palette FAQ
SearchA FAQ for a docs host with a working command palette: a search-field button with its shortcut opens a palette of questions by topic, and choosing one opens its row below.
- 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.