Skip to Content
CodeUI

CommandMenu

@codeui/styles v0.3.0

A search field over a list of commands in plain HTML, with the open menu written by hand.

Load @codeui/styles/commandmenu.css, or all.css. The entry includes the Dialog styles that draw the panel. The Solid component is documented in CommandMenu.

Demo

command-menu/basic.html
<div role="dialog" aria-labelledby="cm-title" tabindex="-1" class="cui-dialog-panel cui-commandmenu-content" data-cui="commandmenu-content" data-motion="none" data-expanded>  <h2 id="cm-title" class="cui-commandmenu-title" data-cui="commandmenu-title">Search themes</h2>  <div class="cui-commandmenu-search" data-cui="commandmenu-search">    <svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" aria-hidden="true">      <circle cx="7" cy="7" r="4.25" />      <path d="m10.25 10.25 3.25 3.25" />    </svg>    <input type="text" role="combobox" placeholder="Search themes" autocomplete="off" spellcheck="false" aria-labelledby="cm-title" aria-expanded="true" aria-controls="cm-listbox" aria-autocomplete="list" aria-activedescendant="theme-dracula" class="cui-commandmenu-search-input" data-cui="commandmenu-search-input" />    <span aria-hidden="true" class="cui-commandmenu-hint" data-cui="commandmenu-hint">↑↓ preview · ↵ apply · esc cancel</span>  </div>  <ul role="listbox" id="cm-listbox" aria-labelledby="cm-title" class="cui-commandmenu-listbox" data-cui="commandmenu-listbox">    <li role="presentation" class="cui-commandmenu-group" data-cui="commandmenu-group">      <span class="cui-commandmenu-group-label" data-cui="commandmenu-group-label">Themes</span>    </li>    <li role="option" id="theme-vercel" aria-selected="false" aria-disabled="false" aria-labelledby="theme-vercel-label" class="cui-commandmenu-item" data-cui="commandmenu-item">      <span aria-hidden="true" class="cui-commandmenu-leading" data-cui="commandmenu-leading">        <span style="width: 16px; height: 16px; border-radius: 50%; background: linear-gradient(135deg, #0a0a0a 50%, #52a8ff 50%); box-shadow: inset 0 0 0 1px rgb(127 127 127 / 35%)"></span>      </span>      <div id="theme-vercel-label" class="cui-commandmenu-item-label" data-cui="commandmenu-item-label">Vercel</div>      <div aria-hidden="true" class="cui-commandmenu-item-indicator" data-cui="commandmenu-item-indicator">        <svg class="cui-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">          <path d="M20 6 9 17l-5-5" />        </svg>      </div>    </li>    <li role="option" id="theme-dracula" aria-selected="false" aria-disabled="false" aria-labelledby="theme-dracula-label" class="cui-commandmenu-item" data-cui="commandmenu-item" data-highlighted>      <span aria-hidden="true" class="cui-commandmenu-leading" data-cui="commandmenu-leading">        <span style="width: 16px; height: 16px; border-radius: 50%; background: linear-gradient(135deg, #282a36 50%, #ff79c6 50%); box-shadow: inset 0 0 0 1px rgb(127 127 127 / 35%)"></span>      </span>      <div id="theme-dracula-label" class="cui-commandmenu-item-label" data-cui="commandmenu-item-label">Dracula</div>    </li>    <li role="option" id="theme-nord" aria-selected="false" aria-disabled="false" aria-labelledby="theme-nord-label" class="cui-commandmenu-item" data-cui="commandmenu-item">      <span aria-hidden="true" class="cui-commandmenu-leading" data-cui="commandmenu-leading">        <span style="width: 16px; height: 16px; border-radius: 50%; background: linear-gradient(135deg, #2e3440 50%, #88c0d0 50%); box-shadow: inset 0 0 0 1px rgb(127 127 127 / 35%)"></span>      </span>      <div id="theme-nord-label" class="cui-commandmenu-item-label" data-cui="commandmenu-item-label">Nord</div>    </li>    <li role="option" id="theme-solarized" aria-selected="false" aria-disabled="false" aria-labelledby="theme-solarized-label" class="cui-commandmenu-item" data-cui="commandmenu-item">      <span aria-hidden="true" class="cui-commandmenu-leading" data-cui="commandmenu-leading">        <span style="width: 16px; height: 16px; border-radius: 50%; background: linear-gradient(135deg, #fdf6e3 50%, #b58900 50%); box-shadow: inset 0 0 0 1px rgb(127 127 127 / 35%)"></span>      </span>      <div id="theme-solarized-label" class="cui-commandmenu-item-label" data-cui="commandmenu-item-label">Solarized light</div>    </li>  </ul></div>

The panel is fixed to the viewport. The demo frames are narrower than 768px, so it sits at the top of the frame with the hint hidden, as on a phone.

Markup

PartElementClassdata-cui
Contentdivcui-dialog-panel cui-commandmenu-contentcommandmenu-content
Titleh2cui-commandmenu-titlecommandmenu-title
Searchdivcui-commandmenu-searchcommandmenu-search
Search inputinputcui-commandmenu-search-inputcommandmenu-search-input
Hintspancui-commandmenu-hintcommandmenu-hint
Listboxulcui-commandmenu-listboxcommandmenu-listbox
Grouplicui-commandmenu-groupcommandmenu-group
Group labelspancui-commandmenu-group-labelcommandmenu-group-label
Itemlicui-commandmenu-itemcommandmenu-item
Leadingspancui-commandmenu-leadingcommandmenu-leading
Item labeldivcui-commandmenu-item-labelcommandmenu-item-label
Item descriptiondivcui-commandmenu-item-descriptioncommandmenu-item-description
Item indicatordivcui-commandmenu-item-indicatorcommandmenu-item-indicator
Shortcutspancui-commandmenu-shortcutcommandmenu-shortcut
Emptydivcui-commandmenu-emptycommandmenu-empty
Overlaydivcui-commandmenu-overlaycommandmenu-overlay

The content needs the Dialog panel classes too: cui-dialog-panel draws the floating surface, its shadow and radius. The command menu class then fixes the panel at 640px wide, 16px above the bottom edge, and turns off the Dialog animation. Below 768px of viewport width it moves 8px from the top edge, hides the hint and sets the search text to 16px.

The title is visually hidden. It names the dialog, the search input and the listbox. The listbox scrolls after six rows.

The command menu has no variant attributes.

Variants

Groups and row details

command-menu/groups.html
<div role="dialog" aria-labelledby="cm-title" tabindex="-1" class="cui-dialog-panel cui-commandmenu-content" data-cui="commandmenu-content" data-motion="none" data-expanded>  <h2 id="cm-title" class="cui-commandmenu-title" data-cui="commandmenu-title">Commands</h2>  <div class="cui-commandmenu-search" data-cui="commandmenu-search">    <svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" aria-hidden="true">      <circle cx="7" cy="7" r="4.25" />      <path d="m10.25 10.25 3.25 3.25" />    </svg>    <input type="text" role="combobox" placeholder="Search commands" autocomplete="off" spellcheck="false" aria-labelledby="cm-title" aria-expanded="true" aria-controls="cm-listbox" aria-autocomplete="list" aria-activedescendant="export-png" class="cui-commandmenu-search-input" data-cui="commandmenu-search-input" />    <span aria-hidden="true" class="cui-commandmenu-hint" data-cui="commandmenu-hint">↑↓ preview · ↵ apply · esc cancel</span>  </div>  <ul role="listbox" id="cm-listbox" aria-labelledby="cm-title" class="cui-commandmenu-listbox" data-cui="commandmenu-listbox">    <li role="presentation" class="cui-commandmenu-group" data-cui="commandmenu-group">      <span class="cui-commandmenu-group-label" data-cui="commandmenu-group-label">Export</span>    </li>    <li role="option" id="export-png" aria-selected="false" aria-disabled="false" aria-labelledby="export-png-label" class="cui-commandmenu-item" data-cui="commandmenu-item" data-highlighted>      <div id="export-png-label" class="cui-commandmenu-item-label" data-cui="commandmenu-item-label">Export as PNG</div>      <span class="cui-commandmenu-shortcut" data-cui="commandmenu-shortcut">⌘ E</span>    </li>    <li role="option" id="export-svg" aria-selected="false" aria-disabled="false" aria-labelledby="export-svg-label" class="cui-commandmenu-item" data-cui="commandmenu-item">      <div id="export-svg-label" class="cui-commandmenu-item-label" data-cui="commandmenu-item-label">Export as SVG</div>    </li>    <li role="option" id="copy-link" aria-selected="false" aria-disabled="false" aria-labelledby="copy-link-label" class="cui-commandmenu-item" data-cui="commandmenu-item">      <div id="copy-link-label" class="cui-commandmenu-item-label" data-cui="commandmenu-item-label">Copy share link</div>      <span class="cui-commandmenu-shortcut" data-cui="commandmenu-shortcut">⌘ ⇧ C</span>    </li>    <li role="presentation" class="cui-commandmenu-group" data-cui="commandmenu-group">      <span class="cui-commandmenu-group-label" data-cui="commandmenu-group-label">Themes</span>    </li>    <li role="option" id="vercel" aria-selected="false" aria-disabled="false" aria-labelledby="vercel-label" class="cui-commandmenu-item" data-cui="commandmenu-item">      <span aria-hidden="true" class="cui-commandmenu-leading" data-cui="commandmenu-leading">        <span style="width: 16px; height: 16px; border-radius: 50%; background: linear-gradient(135deg, #0a0a0a 50%, #52a8ff 50%); box-shadow: inset 0 0 0 1px rgb(127 127 127 / 35%)"></span>      </span>      <div id="vercel-label" class="cui-commandmenu-item-label" data-cui="commandmenu-item-label">Vercel</div>      <div aria-hidden="true" class="cui-commandmenu-item-indicator" data-cui="commandmenu-item-indicator">        <svg class="cui-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">          <path d="M20 6 9 17l-5-5" />        </svg>      </div>    </li>    <li role="option" id="dracula" aria-selected="false" aria-disabled="false" aria-labelledby="dracula-label" class="cui-commandmenu-item" data-cui="commandmenu-item">      <span aria-hidden="true" class="cui-commandmenu-leading" data-cui="commandmenu-leading">        <span style="width: 16px; height: 16px; border-radius: 50%; background: linear-gradient(135deg, #282a36 50%, #ff79c6 50%); box-shadow: inset 0 0 0 1px rgb(127 127 127 / 35%)"></span>      </span>      <div id="dracula-label" class="cui-commandmenu-item-label" data-cui="commandmenu-item-label">Dracula</div>    </li>    <li role="option" id="legacy" aria-selected="false" aria-disabled="true" data-disabled aria-labelledby="legacy-label" class="cui-commandmenu-item" data-cui="commandmenu-item">      <span aria-hidden="true" class="cui-commandmenu-leading" data-cui="commandmenu-leading">        <span style="width: 16px; height: 16px; border-radius: 50%; background: linear-gradient(135deg, #ffffff 50%, #999999 50%); box-shadow: inset 0 0 0 1px rgb(127 127 127 / 35%)"></span>      </span>      <div id="legacy-label" class="cui-commandmenu-item-label" data-cui="commandmenu-item-label">Legacy light</div>    </li>    <li role="presentation" class="cui-commandmenu-group" data-cui="commandmenu-group">      <span class="cui-commandmenu-group-label" data-cui="commandmenu-group-label">Editor / Font</span>    </li>    <li role="option" id="jetbrains" aria-selected="false" aria-disabled="false" aria-labelledby="jetbrains-label" aria-describedby="jetbrains-desc" class="cui-commandmenu-item" data-cui="commandmenu-item">      <div id="jetbrains-label" class="cui-commandmenu-item-label" data-cui="commandmenu-item-label">JetBrains Mono</div>      <div id="jetbrains-desc" class="cui-commandmenu-item-description" data-cui="commandmenu-item-description">Monospace with ligatures</div>      <div aria-hidden="true" class="cui-commandmenu-item-indicator" data-cui="commandmenu-item-indicator">        <svg class="cui-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">          <path d="M20 6 9 17l-5-5" />        </svg>      </div>    </li>    <li role="option" id="fira" aria-selected="false" aria-disabled="false" aria-labelledby="fira-label" aria-describedby="fira-desc" class="cui-commandmenu-item" data-cui="commandmenu-item">      <div id="fira-label" class="cui-commandmenu-item-label" data-cui="commandmenu-item-label">Fira Code</div>      <div id="fira-desc" class="cui-commandmenu-item-description" data-cui="commandmenu-item-description">Monospace with ligatures</div>    </li>  </ul></div>
  • A group is a li with role="presentation" before its items. Write a section as a prefix, as in "Editor / Font".
  • The leading part is a 20px box before the label for an icon or swatch. Give every row of a group the box when one row has it, so the labels line up.
  • The description shows after the label in the hint color, up to 40% of the row.
  • The shortcut is key text at the end of the row. Bind the keys yourself.
  • The item indicator is a check in the brand color that marks the value in use, such as the applied theme.

Overlay

cui-commandmenu-overlay is a fixed, transparent layer over the page that catches outside clicks. It has no scrim, so the content behind the menu stays visible as the preview.

States

Highlighted

data-highlighted on an item gives it the field fill, and its leading visual takes the primary text color. It is neutral, never an accent fill. Point aria-activedescendant on the search input at the highlighted item.

Disabled

data-disabled and aria-disabled="true" on an item show it at 40% opacity with a not-allowed cursor.

Empty

command-menu/empty.html
<div role="dialog" aria-labelledby="cm-title" tabindex="-1" class="cui-dialog-panel cui-commandmenu-content" data-cui="commandmenu-content" data-motion="none" data-expanded>  <h2 id="cm-title" class="cui-commandmenu-title" data-cui="commandmenu-title">Search themes</h2>  <div class="cui-commandmenu-search" data-cui="commandmenu-search">    <svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" aria-hidden="true">      <circle cx="7" cy="7" r="4.25" />      <path d="m10.25 10.25 3.25 3.25" />    </svg>    <input type="text" role="combobox" value="kiwi" placeholder="Search themes" autocomplete="off" spellcheck="false" aria-labelledby="cm-title" aria-expanded="true" aria-controls="cm-listbox" aria-autocomplete="list" class="cui-commandmenu-search-input" data-cui="commandmenu-search-input" />    <span aria-hidden="true" class="cui-commandmenu-hint" data-cui="commandmenu-hint">↑↓ preview · ↵ apply · esc cancel</span>  </div>  <ul role="listbox" id="cm-listbox" aria-labelledby="cm-title" class="cui-commandmenu-listbox" data-cui="commandmenu-listbox"></ul>  <div role="status" class="cui-commandmenu-empty" data-cui="commandmenu-empty">No theme matches “kiwi”. Clear the search to see every theme.</div></div>

Show the empty part while no item matches, with role="status" so the change is announced. Say what is missing and how to get results back.

Open and closed

Add the content to the page to open the menu and remove it to close. It opens and closes without animation, because people open it from the keyboard many times a session.

What you supply

  • Opening from your own shortcut, such as ⌘ K, and closing on Escape and outside click.
  • role="dialog" on the content, labelled by the title, and focus on the search input when it opens.
  • role="combobox" on the input with aria-controls, aria-expanded and aria-activedescendant, and role="listbox" and role="option" on the list.
  • Filtering, moving data-highlighted with the arrow keys and the pointer, and Enter to run the highlighted command.
  • The check for the current value, and skipping disabled items.

The command menu entry exports class names, not a style props helper. Use the class names above, or commandMenuStyles from @codeui/styles/commandmenu.

On this page