Skip to content

Command ​

The palette: type to filter, arrows to move, Enter to run. It is a list you place anywhere — in a page, a popover, a sidebar. CommandDialog puts it in a modal opened by a shortcut, and Combobox builds on it to pick a value.

Filtering happens without re-rendering. Each item registers itself as it is built, then hides itself when the query stops matching it: rebuilding the list on every keystroke would lose the DOM the highlight and the scroll position live in. A group hides itself the same way once none of its own items are showing, so an empty heading never sits over nothing.

The arrows never take focus out of the search field. The list is a listbox whose options are pointed at rather than focused, which lets a screen reader read each result as the arrows pass over it while typing keeps working.

sh
ream nebula:add command

Organism · organisms/Command.ts · no dependencies

Usage ​

ts
import { html } from '@c9up/aurora'
import { Command, CommandEmpty, CommandInput, CommandItem, CommandList } from '@c9up/nebula'

Command({
  class: 'rounded-lg border shadow-md',
  children: () => html`
    ${CommandInput({ placeholder: 'Search…' })}
    ${CommandList({ children: () => html`
      ${CommandEmpty({ children: 'No results found.' })}
      ${CommandItem({ value: 'new', children: 'New file', onSelect: () => createFile() })}
      ${CommandItem({ value: 'open', children: 'Open folder', onSelect: () => openFolder() })}
    ` })}
  `,
})

Command, CommandList and CommandGroup take their parts in a thunk — see composition.

Examples ​

Groups, separators, shortcuts ​

ts
import { html } from '@c9up/aurora'
import {
  Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList,
  CommandSeparator, CommandShortcut,
} from '@c9up/nebula'

Command({
  children: () => html`
    ${CommandInput({})}
    ${CommandList({ children: () => html`
      ${CommandEmpty({ children: 'No results found.' })}
      ${CommandGroup({ heading: 'Files', children: () => html`
        ${CommandItem({ value: 'new', textValue: 'New file', children: html`New file${CommandShortcut({ children: '⌘N' })}` })}
        ${CommandItem({ value: 'save', textValue: 'Save', children: html`Save${CommandShortcut({ children: '⌘S' })}` })}
      ` })}
      ${CommandSeparator({})}
      ${CommandGroup({ heading: 'Help', children: () => CommandItem({ value: 'docs', children: 'Documentation' }) })}
    ` })}
  `,
})

CommandShortcut only displays the keys: bind the real shortcut yourself.

What the filter matches ​

An item matches when its text contains the query, ignoring case. The text is children when it is a string; when children is markup — a shortcut beside the label, an icon — give textValue, or the item is matched on its value. keywords adds terms the label does not contain:

ts
CommandItem({ value: 'open', children: 'Open folder', keywords: ['directory'] })

Typing directory finds Open folder. Predictable beats clever: a term you list is found, a fuzzy score nobody can anticipate is not used.

Your own filter ​

filter replaces the built-in one. It receives the item's value, textValue and keywords and the query, and says whether the item stays. defaultFilter is exported, so a filter of your own can prepare its input and hand over — here, one that ignores accents, so cafe finds Café:

ts
import { html } from '@c9up/aurora'
import { Command, defaultFilter } from '@c9up/nebula'

const fold = (text: string) => text.normalize('NFD').replace(/\p{Diacritic}/gu, '')

Command({
  filter: (candidate, query) =>
    defaultFilter({ ...candidate, textValue: fold(candidate.textValue), keywords: candidate.keywords.map(fold) }, fold(query)),
  children: () => html`…`,
})

One handler for every item ​

Each item runs its own onSelect; the palette's onSelect then receives the item's value. Use the second when the items are data:

ts
import { html } from '@c9up/aurora'
import { Command, CommandInput, CommandItem, CommandList } from '@c9up/nebula'

const pages = [
  { value: '/settings', label: 'Settings' },
  { value: '/billing', label: 'Billing' },
]

Command({
  onSelect: (value) => navigate(value),
  children: () => html`
    ${CommandInput({ placeholder: 'Go to…' })}
    ${CommandList({ children: () => pages.map((page) => CommandItem({ value: page.value, children: page.label })) })}
  `,
})

value identifies the item: keep it unique within a palette.

A list that changes ​

Items register as they are built and stay registered for the life of the palette. Build the palette when its list is known — CommandDialog and Combobox build theirs afresh each time they open — rather than feeding a fixed palette a list that changes.

API ​

Command ​

PropTypeDefaultDescription
childrenParts—The input and the list.
filter(candidate: CommandCandidate, query: string) => booleandefaultFilterSays whether an item survives the query.
onSelect(value: string) => void—Called with the value of any item run, after the item's own onSelect.
classReactive<string>—Merged into the root's classes.

CommandInput ​

A search icon and the field.

PropTypeDefaultDescription
placeholderstring'Type a command or search…'The field's placeholder.
classReactive<string>—Merged into the <input>'s classes.

CommandList ​

PropTypeDefaultDescription
childrenParts—CommandEmpty, groups, items, separators.
classReactive<string>—Merged into the list's classes. It scrolls past 300 px.

CommandEmpty ​

Shown only while the query matches nothing.

PropTypeDefaultDescription
childrenSlot—The message.
classReactive<string>—Merged into its classes.

CommandGroup ​

PropTypeDefaultDescription
headingSlot—Shown above the items.
childrenParts—The group's items.
classReactive<string>—Merged into the group's classes.

CommandItem ​

PropTypeDefaultDescription
valuestring—Required. Identifies the item and is what Command's onSelect receives.
childrenSlot—The item's content.
textValuestringchildren when it is a string, else valueWhat the filter matches.
keywordsreadonly string[][]Extra terms that match the item.
disabledbooleanfalseLeaves the item out of the matches: it is hidden and never runs.
onSelect() => void—Runs when the item is chosen.
classReactive<string>—Merged into the item's classes.

CommandSeparator ​

PropTypeDefaultDescription
classReactive<string>—Merged into the rule's classes.

CommandShortcut ​

PropTypeDefaultDescription
childrenSlot—The keys, right-aligned in the item. Display only.
classReactive<string>—Merged into its classes.

defaultFilter ​

defaultFilter(candidate: CommandCandidate, query: string): boolean — the built-in filter. An empty query matches everything; otherwise the item matches when its textValue or one of its keywords contains the query, ignoring case.

CommandCandidate is { readonly value: string; readonly textValue: string; readonly keywords: readonly string[] }.

Accessibility ​

The field is role="combobox" with aria-controls pointing at the list and aria-activedescendant at the highlighted item. The list is role="listbox", each item role="option" with aria-selected="true" on the highlighted one. A group is role="group". Filtered-out items and empty groups are hidden, so they leave the accessibility tree too.

The keyboard never takes focus out of the field. Before the user moves, the first match is highlighted; every change to the query brings the highlight back to the first match, so Enter never runs an item the query has hidden. The pointer moving onto an item highlights it.

KeyAction
ArrowDown, ArrowUpHighlight the next or previous match, wrapping at the ends, and scroll it into view.
EnterRun the highlighted item.

The field's placeholder is English by default; placeholder replaces it, and CommandEmpty says whatever you give it — see languages.