Introducing PrimeReact v11-alpha 🎉Discover Now

CommandMenu

CommandMenu is a command palette component for searching and executing actions.

Navigate↵Select
preview

Installation#

npx shadcn@latest add @primereact/commandmenu

Usage#

import { CommandMenu, CommandMenuEmpty, CommandMenuFooter, CommandMenuGroup, CommandMenuGroupHeader, CommandMenuHeader, CommandMenuInput, CommandMenuItem, CommandMenuList } from '@/components/ui/commandmenu';
<CommandMenu options={items} optionLabel="label" optionValue="value" optionGroupLabel="group" optionGroupChildren="items">
    <CommandMenuHeader>
        <CommandMenuInput placeholder="Search..." />
    </CommandMenuHeader>
    <CommandMenuEmpty>No results found</CommandMenuEmpty>
    <CommandMenuList>
        {({ commandmenu }) =>
            commandmenu?.filteredOptions?.map((group, gi) => (
                <CommandMenuGroup key={gi}>
                    <CommandMenuGroupHeader>{group.group}</CommandMenuGroupHeader>
                    {group.items.map((item, ii) => (
                        <CommandMenuItem key={ii} option={item}>
                            {item.label}
                        </CommandMenuItem>
                    ))}
                </CommandMenuGroup>
            ))
        }
    </CommandMenuList>
    <CommandMenuFooter />
</CommandMenu>

Examples#

Basic#

Quickly search and execute actions from a floating menu.

Navigate↵Select
basic-demo

Filter#

Use the filter prop to customize the filtering of the items.

Navigate↵Select
filter-demo

Controlled#

Navigate↵Select
controlled-demo

With Dialog#

Use the CommandMenu component inside a Dialog component to create a command palette that is displayed in a dialog. useHotKey hook is used to open the dialog with the meta+k shortcut.

with-dialog-demo

With ScrollArea#

Navigate↵Select
with-scrollarea-demo

Custom#

Navigate↵Select
custom-demo

Accessibility#

Screen Reader#

CommandMenu uses combobox role on the input and listbox role on the list. Items use option role with aria-selected state.

Keyboard Support#

KeyFunction
up arrowMoves focus to the previous item.
down arrowMoves focus to the next item.
enterSelects the focused item.
escapeCloses the command menu if used in an overlay.