oakoss/ui
Components

Command

A searchable list of commands, inline or in a dialog.

SourceReact Aria

Installation

npx shadcn@latest add @oakoss/command

This also installs Dialog and Input Group, and the item parts it shares with Dropdown Menu.

Usage

import {
  Command,
  CommandInput,
  CommandItem,
  CommandList,
} from '#/components/ui/overlays/command';

<Command>
  <CommandInput placeholder="Type a command" />
  <CommandList aria-label="Commands" onAction={(key) => run(key)}>
    <CommandItem id="new" keywords={['create']} shortcut="⌘N">
      New file
    </CommandItem>
  </CommandList>
</Command>;

What's typed filters the list, ignoring case and accents, and matches an item's label and its keywords. Items take Dropdown Menu's item props: shortcut, description, intent and href. CommandGroup sections the list under a CommandHeader, each section after the first drawing a line above it.

Examples

In a dialog

CommandDialog puts a command in a modal, named by title and focused on the input when it opens. This demo also opens it with ⌘J or Ctrl + J while it's on the page; the library binds no shortcut itself, so an app picks one that its own keys don't use.

description adds a line under the label, and href makes an item a link.

Accessibility

React Aria links a search field to a menu of results:

  • Focus: focus stays in the input. The highlighted item is the input's aria-activedescendant, so screen readers read it as the arrow keys move.
  • Names: the input is named Search unless you name it with aria-label or aria-labelledby, and the list by its own aria-label. A group is named by its CommandHeader, and a shortcut or description describes its item.
  • Results: items are menuitems, since they run commands. When nothing matches, the list shows emptyLabel.

In Windows High Contrast the highlighted item takes the system's selection colors.

KeyAction
TypingFilters the list
↓, ↑Moves the highlight through the results
EnterRuns the highlighted command
EscapeClears the search, then closes a CommandDialog

API

Command

A div around React Aria's Autocomplete, which links the input to the list.

Props

Prop

Type

Also accepts all React Aria Autocomplete props, such as a controlled inputValue and a filter that replaces the default.

CommandList

Extends React Aria's Menu. It scrolls past 18rem.

Props

Prop

Type

Also accepts all React Aria Menu props, such as onAction and items.

CommandItem

Dropdown Menu's item, with keywords.

Props

Prop

Type

Data attributes

AttributePresent when
data-slotAlways command-item
data-intentAlways: neutral or destructive
data-focusedThe item is highlighted
data-selectedThe item is checked in a selection group
data-selection-modeThe item is in a selection group: single, multiple
data-disabledThe item is disabled

CSS variables

VariableSet byUsed for
--menu-textintent="destructive"The item's text
--menu-subtleintent="destructive"The highlighted item's fill

CommandDialog

Dialog, with no close button and the panel nearer the top from sm up.

Props

Prop

Type

Other parts

PartExtendsdata-slot
CommandInputReact Aria's SearchField, with an InputGroupcommand-input-wrapper
CommandGroupReact Aria's MenuSectioncommand-group
CommandHeaderReact Aria's Header, with insetcommand-label
CommandSeparatorReact Aria's Separatorcommand-separator

CommandInput's input is command-input, and Command is command. CommandInput takes React Aria's SearchField props except value and defaultValue: Command owns what's typed, through inputValue, defaultInputValue and onInputChange.