Command palette

A searchable list of actions and destinations that opens over the page on a shortcut: Cmd+K on a Mac, Ctrl+K elsewhere.

Command palette

Needs Alpine and the package's script. See getting started. Without them it stays closed and does nothing, so give every destination in it a visible link too, such as a search page.

Use it

<x-ds::command-palette name="main" @archive="archive($event.detail.value)">
    <x-ds::command-group label="Go to">
        <x-ds::command-item label="Invoices" :href="route('invoices.index')" icon="document-text" keywords="bills receipts" />
        <x-ds::command-item label="Customers" :href="route('customers.index')" icon="users" shortcut="G+C" />
        <x-ds::command-item label="Settings" :href="route('settings')" icon="cog" />
    </x-ds::command-group>

    <x-ds::command-group label="Actions">
        <x-ds::command-item label="New invoice" action="create-invoice" icon="plus" />
        <x-ds::command-item label="Archive this invoice" action="archive" :value="$invoice->id" icon="archive-box">
            Moves it out of the inbox
        </x-ds::command-item>
    </x-ds::command-group>
</x-ds::command-palette>

Put it once in the layout. Typing filters the options (by label, then by keywords), the arrows move, Enter follows the link or dispatches the action, and the palette closes.

Open it from a button

The trigger is yours. A button that says what the shortcut is:

@php $mac = str_contains(request()->userAgent() ?? '', 'Mac OS'); @endphp

<x-ds::button variant="secondary" x-data x-on:click="$dispatch('open-command-palette', { name: 'main' })">
    <x-ds::icon name="magnifying-glass" size="4" />
    Search…
    <x-ds::kbd :keys="$mac ? ['⌘', 'K'] : ['Ctrl', 'K']" :separator="$mac ? '' : '+'" />
</x-ds::button>

open-command-palette and close-command-palette are window events; the palette whose name matches detail.name answers. From Livewire: $this->dispatch('open-command-palette', name: 'main').

Props

command-palette

PropTypeDefaultWhat it does
namestring—Which palette an open-command-palette event opens. Left out, it answers events that name none
shortcutstring | falsemod+kmod is Cmd on a Mac and Ctrl elsewhere; also ctrl, cmd, alt, shift, and a bare key such as /. false for none. One key, one owner: if an expanding search (or a second palette) has the same shortcut, the one that started first on the page takes the press and the other stands down, so give the search / and the palette mod+k
placeholderstringType a command or search…
labelstringCommand paletteThe dialog's, the field's and the list's accessible name
emptyTextstringNo resultsShown and announced when nothing matches
loadingboolfalseA spinner in the field, the list aria-busy, no empty row
filterbooltrueFilter and rank in the browser. false when the server searches

A footer slot draws a row under the list, for hints.

command-group

PropTypeDefaultWhat it does
labelstringrequiredThe heading, and the group's name

command-item

PropTypeDefaultWhat it does
labelstringrequiredWhat it says, and what typing matches first
hrefstring—A link. Stays a real link: Ctrl+press opens a tab, wire:navigate works
actionstring—Or the event it dispatches, bubbling, with { value }
iconstring—A Heroicons name
shortcutstring | array—Drawn with kbd: "G+C" or ['⌘', 'K']. A label only: the palette does not bind it
keywordsstring | array—More words it is found by
disabledboolfalseShown, passed over by the arrows, does nothing
valuestring—The action's detail.value

The item's slot is a second, muted line. Anything else you put on an item (wire:click, @click, wire:key) lands on the option, and Enter clicks it.

Events

EventFromDetail
searchthe palette, debounced 200 ms{ query }
an item's actionthe item, bubbling to the window{ value }

Search on the server (Livewire)

The palette does no fetching. It dispatches search; the component searches and re-renders the options, and the palette keeps up: the filter runs over what arrives and the active option stays active if it is still there.

class CommandSearch extends Component
{
    public string $query = '';

    public function render()
    {
        return view('livewire.command-search', [
            'customers' => strlen($this->query) < 2
                ? collect()
                : Customer::where('name', 'like', "%{$this->query}%")->limit(8)->get(),
        ]);
    }
}
<div>
    <x-ds::command-palette name="main" :filter="false" x-on:search="$wire.set('query', $event.detail.query)">
        <x-ds::command-group label="Customers">
            @foreach ($customers as $customer)
                <x-ds::command-item
                    wire:key="customer-{{ $customer->id }}"
                    :label="$customer->name"
                    :href="route('customers.show', $customer)"
                    wire:navigate
                />
            @endforeach
        </x-ds::command-group>

        <x-slot:footer><span wire:loading>Searching…</span></x-slot:footer>
    </x-ds::command-palette>
</div>

:filter="false", because the server already chose what matches: filtering again in the browser would hide customers it found by email. Give every item a wire:key. Pass :loading="true" while a first, deferred load is pending.

Keyboard

Key
Cmd+K / Ctrl+KOpen, and close again
↓ ↑Next and previous option, wrapping
Home, EndFirst and last option
EnterFollow or run the active option
EscapeEmpty the query; on an empty field, close

Accessibility

A modal dialog on the same layer as modal: focus moves to the field and stays there, Tab is trapped, the page behind does not scroll, and focus goes back where it was on close. The field is an ARIA combobox whose aria-activedescendant names the active option; "No results" is announced. Disabled options are passed over. Opened over another overlay it stacks; see Stacking overlays.

Don't

  • Don't make it the only route. Nobody finds a shortcut they were never shown; keep the links on the page.
  • Don't nest controls in an option. An option is one action.
  • Don't filter in the browser when the server searched (:filter="false").
  • Don't use a bare letter as the shortcut. It would fire on every key typed outside a field.

From the package repo: edit this page on GitHub.

Was this page helpful?