Command Palette
Command list that opens as a dialog, with fuzzy, ranked search and shortcut hints.
<arc-command-palette>Overview
CommandPalette is a modal dialog with a search field, a list of commands and a footer of key hints. You open it by setting open, usually from a Cmd+K handler or a button. The palette doesn't bind any shortcut itself.
Commands are <arc-command-item> children. An item's text content is its label, and it can add an icon, a shortcut hint, keywords that are searched but never shown, and a description that's shown as a second line and also searched. Search is fuzzy and ranked: "cmdpal" finds "Command Palette", terms can come in any order, and the best matches come first with the matched characters marked. Only the top max-results (50 by default) are rendered.
Picking an item fires arc-select with its value, or its label if it has no value, and closes the palette. Escape and a click on the backdrop close it too. Each of these fires a cancelable arc-close, so preventDefault() keeps the palette open. Setting open to false from code closes it without the event. For a search field that's always on screen, use CommandBar.
Guidelines
Do
- Write labels as short actions, such as "Open file" or "Toggle theme"
- Add `shortcut` to items that have a key binding
- Give items a `value` when two of them could share a label, so your `arc-select` handler can tell them apart
Don't
- Don't put forms or other interactive content in an item. The palette renders its own copy of each item from its text, so anything else is lost
Features
- Opens with a scale-in transition. The search input takes focus and the query clears on every open
- Arrow Up and Arrow Down move the highlight, Enter selects and Escape closes
- Body scroll is locked while it's open, and focus returns to where it was when it closes
- When nothing matches, the empty state repeats the query
- The backdrop is `::backdrop`, which can't be a CSS part. Style it with `--palette-backdrop`
Preview
Usage
This component needs JavaScript. Use the Web Component or a framework wrapper.
<arc-button onclick="document.querySelector('#palette').open = true">Open Palette</arc-button>
<arc-command-palette id="palette">
<arc-command-item shortcut="⌘O">Open File</arc-command-item>
<arc-command-item shortcut="⌘S">Save File</arc-command-item>
</arc-command-palette>import { Button, CommandItem, CommandPalette } from '@arclux/arc-ui-react';
export default function Example() {
return (
<>
<Button onclick="document.querySelector('#palette').open = true">Open Palette</Button>
<CommandPalette id="palette">
<CommandItem shortcut="⌘O">Open File</CommandItem>
<CommandItem shortcut="⌘S">Save File</CommandItem>
</CommandPalette>
</>
);
}<script setup>
import { Button, CommandItem, CommandPalette } from '@arclux/arc-ui-vue';
</script>
<template>
<Button onclick="document.querySelector('#palette').open = true">Open Palette</Button>
<CommandPalette id="palette">
<CommandItem shortcut="⌘O">Open File</CommandItem>
<CommandItem shortcut="⌘S">Save File</CommandItem>
</CommandPalette>
</template><script>
import { Button, CommandItem, CommandPalette } from '@arclux/arc-ui-svelte';
</script>
<Button onclick="document.querySelector('#palette').open = true">Open Palette</Button>
<CommandPalette id="palette">
<CommandItem shortcut="⌘O">Open File</CommandItem>
<CommandItem shortcut="⌘S">Save File</CommandItem>
</CommandPalette>import { Component } from '@angular/core';
import { Button, CommandItem, CommandPalette } from '@arclux/arc-ui-angular';
@Component({
imports: [Button, CommandItem, CommandPalette],
template: `
<arc-button onclick="document.querySelector('#palette').open = true">Open Palette</arc-button>
<arc-command-palette id="palette">
<arc-command-item shortcut="⌘O">Open File</arc-command-item>
<arc-command-item shortcut="⌘S">Save File</arc-command-item>
</arc-command-palette>
`,
})
export class MyComponent {}import { Button, CommandItem, CommandPalette } from '@arclux/arc-ui-solid';
export default function Example() {
return (
<>
<Button onclick="document.querySelector('#palette').open = true">Open Palette</Button>
<CommandPalette id="palette">
<CommandItem shortcut="⌘O">Open File</CommandItem>
<CommandItem shortcut="⌘S">Save File</CommandItem>
</CommandPalette>
</>
);
}import { Button, CommandItem, CommandPalette } from '@arclux/arc-ui-preact';
export default function Example() {
return (
<>
<Button onclick="document.querySelector('#palette').open = true">Open Palette</Button>
<CommandPalette id="palette">
<CommandItem shortcut="⌘O">Open File</CommandItem>
<CommandItem shortcut="⌘S">Save File</CommandItem>
</CommandPalette>
</>
);
}API
placeholderstring'Type a command...'- Placeholder text displayed in the search input when the query is empty.
max-resultsnumber50- How many ranked results to render. Truncation happens after ranking, so what survives is the best of the set. Raise it for a short command list; the default suits a large one.
openbooleanfalse- Controls whether the palette is visible. When set to true, the dialog animates in, the search input auto-focuses, and body scroll is locked. Set to false to close.
Events
arc-selectdetail:{ value: string, item: { label: string, shortcut: string, value: string } }- Fired when a command item is selected.
detail.valueis the item'svalue, falling back to its label. arc-close- Fired when the palette closes
CommandItem
<arc-command-item>One command in a CommandPalette. Its text content is the label.
labelselectionValue- What arc-select reports. Falls back to the label so an item that never sets
valuebehaves as it always did. shortcutstring''- Keyboard shortcut hint
iconstring''- Name of the icon to display before the item label.
keywordsstring''- Extra space-separated terms the search filter matches against but never displays, e.g. keywords="dialog popup" on a Modal item.
descriptionstring''- Secondary line shown under the label and matched by search. Use it for the sentence that tells two similar results apart; a docs site can put the matching passage here so a query finds page content rather than only page titles.
valuestring''- Stable identifier carried on the arc-select detail. Defaults to the label, which is fine until two items share one; give anything a handler must act on its own value rather than matching against display text.
CommandGroup
<arc-command-group>Puts CommandItems under a small uppercase heading. Items in groups still filter and move as one flat list, groups are ordered by their best match, and a heading hides when none of its items match.
headingstring''- Heading text displayed above the group’s items.