shadcn.io is not affiliated with official shadcn/ui
Snippet Library Command Menu Block for React
A code editor's snippet library as a command menu shown open over the editor: a language switch, a pane that follows the highlighted snippet with highlighted code and Copy, and Enter inserts it into the buffer.
Installation
Overview
Insert a piece of code you have written before with this snippet library command menu for React with Base UI. It opens from the editor of Snipwell, a code editor, whose header holds an outline field with its ⌘ K cap, and the editor is drawn under a soft scrim inside a hairline frame with a plus mark on each corner. The menu is the catalog Command split in two: a list of nine snippets in TypeScript, CSS and SQL with a language ToggleGroup over it, and a pane that follows the highlighted row and shows the snippet with the catalog SyntaxText and a Copy button that really copies it. Enter inserts the highlighted snippet into the editor's buffer and closes the menu; Ctrl or ⌘ Enter inserts it and keeps the menu open so several can go in, and the drawn editor behind the menu gains the lines, numbered from where they start. On a phone the pane is dropped and each row carries its own Copy button. For preview the menu is drawn open in place when the page loads; the field, or Ctrl or ⌘ K while focus is in the block, opens the real command dialog with the same snippets, language and query. For editors, notebooks and anything with a library of reusable code.
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 and its pane drawn inline under a picture of the editor, in a plain shell that is not modal, so nothing is trapped, hidden or focused and the page around the preview keeps working. The header's 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 and share the block's state, so the buffer you added to in the copy is the one the dialog opens with. 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.
How does the pane follow the highlight?
Through each library's own event: the Ark Command reports the highlighted value, cmdk takes it as a controlled value, and React Aria has no highlight event, so the block reads the search field's aria-activedescendant and finds the row from it. The pane has a fixed size, so a longer snippet never moves the list. Its code is the catalog SyntaxText, coloured by weight and tone rather than hue so it follows any theme, and its Copy button copies exactly the code shown.
What do Enter and Ctrl or ⌘ Enter do?
Enter inserts the highlighted snippet under the last line of the buffer, after a blank line, and closes the menu with focus on the field. Ctrl or ⌘ Enter does the same and leaves the menu open (the block listens for the chord while focus is inside the menu and stops the command's own Enter from also firing), so you can add a second or third. The status line says which snippet went in and at which line.
How do the language switch and the search work?
The switch has four chips, one always chosen: All, TS, CSS and SQL. It narrows the list and starts it over, and the count under the list follows it. The search reads the title, what the snippet does, a few words per snippet and its code, without accents or case, so abort finds the fetch snippet and HAVING the duplicates one.
What stays local, and how do I wire it?
Almost all of it. The snippets and the buffer are typed state, nothing is saved or run, and the clipboard is the only thing the browser really does, when you press Copy. In your app load SNIPPETS from the user's library and replace withSnippet with your editor's insert at the cursor.
Is it accessible from the keyboard?
Yes. Type to search, the arrow keys move through the snippets, Enter inserts, and Escape closes the menu and puts focus on the field; Tab reaches the language switch and Copy, and the code in the pane is a named, focusable region. Ctrl or ⌘ K opens the dialog while focus is inside the block, and only then. The rows are options of a cmdk listbox; the block turns off its own fuzzy filter and lists the rows itself.
Related
- Pro
Code Actions Command Menu
Actions and keysAn editor extension's quick fixes as a command menu shown open over the editor: a small code view with flagged lines and numbered line buttons, and a list of only the fixes that apply to the chosen line.
- #**>Pro
Markdown Formatting Command Menu
Edit and insertA notes editor's formatting menu shown open over a note: a real textarea with a sample sits above seven rows that format its selection or insert a block, and a preview and a mono line show the result.
- Pro
Reply Templates Command Menu
Edit and insertA help desk's reply library as a command menu shown open over a ticket: choosing a reply with slots turns the menu into a short form, a textarea previews the filled reply, and Enter inserts it.
- Pro
Icon Picker Command Menu
Visual pickersA page builder's icon picker as a command menu shown open over a block's settings: forty icons drawn at the chosen size and stroke, two sliders that restyle every row, and Enter copies the JSX.
- Pro
Help Center Command Menu
Pages and docsA messaging app's help centre as a command menu shown open beside an article pane: articles and video tutorials filter as you type, the pane shows the highlighted one's first paragraph, three headings and related reading, and a contact support row is never filtered out.
- Pro
Basic Command Menu
Search everythingA 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.