Command palette

One launcher for the whole app: the pages, the things you can do, and your last few issues, searched as you type and opened with Enter. Opened by the search button in the shell's top row and by Ctrl+K (Cmd+K on a Mac) from anywhere. Its pages and actions come from a PHP array, so adding one is a line of config, not a change to a view.

The command palette open, light and dark

The command palette on a phone, light and dark

A block, not a component: copy the views below into your app. The palette is drawn once, in the layout, by both app shells; it assumes the base layout. It needs Alpine and the package's script, so give everything in it a visible link as well: the shell's search button is a link to a search page, and every page in it is in the sidebar.

What it shows

  • Three groups: Recent (your last issues, with their project on a second line), Navigation (the pages, each with an icon and the shortcut it is known by, drawn with kbd) and Actions (New issue, Invite people, Copy link to this page). The footer says which keys do what.

  • Typing filters every group at once, by the label and by more words each item is found by (keywords: "sprints" finds Cycles, "billing" finds Workspace settings). "harb" leaves the two Harbour app issues:

    Typing "harb", light and dark

  • Nothing matches: a line that says what can be found, announced to a screen reader as it appears:

    Nothing matches "lifejackets", light and dark

  • Loading: while the recent issues are still on their way (the Livewire version below fetches them after the page), a spinner takes the place of the magnifying glass and the list is marked busy, with no "Nothing matches" meanwhile:

    The palette loading, light and dark

  • An action that is not a link: Copy link to this page names an event (copy-page-link) instead of a route. The item dispatches it, and the palette's own x-on:copy-page-link copies the address.

Built from

The markup

config/commands.php

<?php

/*
 * What the command palette offers, by route name: adding a page or an action
 * is a line here, not a change to the view. `keywords` are more words each is
 * found by; `shortcut` is drawn beside it, and bound by you if at all; an
 * entry with `event` instead of `route` dispatches that browser event.
 */
return [
    'pages' => [
        ['label' => 'Home', 'route' => 'home', 'icon' => 'home', 'shortcut' => 'G+H'],
        ['label' => 'Inbox', 'route' => 'inbox', 'icon' => 'inbox', 'shortcut' => 'G+I', 'keywords' => 'messages mentions'],
        ['label' => 'My issues', 'route' => 'issues.mine', 'icon' => 'check-circle', 'shortcut' => 'G+M', 'keywords' => 'assigned tasks'],
        ['label' => 'Projects', 'route' => 'projects.index', 'icon' => 'folder', 'keywords' => 'boards'],
        ['label' => 'Cycles', 'route' => 'cycles.index', 'icon' => 'arrow-path', 'keywords' => 'sprints iterations'],
        ['label' => 'Reports', 'route' => 'reports.index', 'icon' => 'chart-bar', 'keywords' => 'charts velocity'],
        ['label' => 'Workspace settings', 'route' => 'settings.workspace', 'icon' => 'cog-6-tooth', 'keywords' => 'members billing'],
        ['label' => 'Notification settings', 'route' => 'settings.notifications', 'icon' => 'bell', 'keywords' => 'email alerts'],
    ],
    'actions' => [
        ['label' => 'New issue', 'route' => 'issues.create', 'icon' => 'plus', 'shortcut' => 'C', 'keywords' => 'create bug task'],
        ['label' => 'Invite people', 'route' => 'settings.members', 'icon' => 'user-plus', 'keywords' => 'team members'],
        ['label' => 'Copy link to this page', 'event' => 'copy-page-link', 'icon' => 'link', 'keywords' => 'share url'],
    ],
];

resources/views/components/command-menu.blade.php

{{--
    The app-wide launcher, once, in the layout. Pages and actions come from
    config/commands.php; $recent is the reader's last few issues, from the
    layout's view composer: an empty list for none, null while they are
    still being fetched, which draws the palette busy. Opened by Ctrl+K or
    Cmd+K, and by any button that dispatches open-command-palette with the
    name "app".
--}}
@props(['recent' => null])

<x-ds::command-palette
    name="app"
    label="Search Pebblework"
    placeholder="Search pages, actions and recent issues…"
    emptyText="Nothing matches. Try a page, an action, or an issue key such as HBR-142."
    :loading="$recent === null"
    x-on:copy-page-link="navigator.clipboard?.writeText(location.href)"
>
    @if (! empty($recent))
        <x-ds::command-group label="Recent">
            @foreach ($recent as $issue)
                <x-ds::command-item
                    :label="$issue['key'].' '.$issue['title']"
                    :href="route('issues.show', $issue['key'])"
                    icon="clock"
                    :keywords="$issue['project']"
                >{{ $issue['project'] }}</x-ds::command-item>
            @endforeach
        </x-ds::command-group>
    @endif

    <x-ds::command-group label="Navigation">
        @foreach (config('commands.pages', []) as $command)
            <x-ds::command-item
                :label="$command['label']"
                :href="isset($command['route']) ? route($command['route']) : null"
                :action="$command['event'] ?? null"
                :icon="$command['icon'] ?? null"
                :shortcut="$command['shortcut'] ?? null"
                :keywords="$command['keywords'] ?? null"
            />
        @endforeach
    </x-ds::command-group>

    <x-ds::command-group label="Actions">
        @foreach (config('commands.actions', []) as $command)
            <x-ds::command-item
                :label="$command['label']"
                :href="isset($command['route']) ? route($command['route']) : null"
                :action="$command['event'] ?? null"
                :icon="$command['icon'] ?? null"
                :shortcut="$command['shortcut'] ?? null"
                :keywords="$command['keywords'] ?? null"
            />
        @endforeach
    </x-ds::command-group>

    <x-slot:footer>
        <span class="flex items-center gap-1.5"><x-ds::kbd :keys="['↑', '↓']" separator="" /> to move</span>
        <span class="flex items-center gap-1.5"><x-ds::kbd>Enter</x-ds::kbd> to open</span>
        <span class="ms-auto flex items-center gap-1.5"><x-ds::kbd>Esc</x-ds::kbd> to close</span>
    </x-slot:footer>
</x-ds::command-palette>

Wire it up

Once, in the layout

Both shells end with <x-command-menu :recent="$recentIssues" />, inside their root, so there is one palette on every page. $recentIssues comes from the layout's view composer (see the sidebar shell), as a list of key, title and project; null draws the palette busy.

Pages and actions from config

config/commands.php lists them by route name. A page or an action is one line; keywords are more words it is found by, shortcut is drawn beside it, and an entry with event in place of route dispatches that event when chosen. The palette shows a shortcut but does not bind it: to make G then H go home, bind it yourself.

Show each person only what they may use: filter the list where it is read, for example array_filter(config('commands.actions'), fn ($c) => Gate::allows($c['ability'] ?? 'view-app')), with an ability added to the entries that need one.

Opening it

The palette is named app. Ctrl+K and Cmd+K open it (its shortcut, on by default), and so does any button anywhere that dispatches the event, as the shell's search button does:

<x-ds::button x-data x-on:click="$dispatch('open-command-palette', { name: 'app' })">Search</x-ds::button>

From Livewire: $this->dispatch('open-command-palette', name: 'app').

Searching on the server (Livewire)

The view above filters in the browser, over what it was drawn with. To search every issue instead of the last few, make the menu a Livewire component: the palette dispatches search with { query } as the reader types (debounced), the component searches, and the palette keeps up with what comes back. The recent issues are fetched after the page, with wire:init, so the palette is busy until they arrive.

namespace App\Livewire;

use App\Models\Issue;
use Illuminate\Support\Str;
use Livewire\Component;

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

    public bool $ready = false;

    public function load(): void
    {
        $this->ready = true;
    }

    public function render()
    {
        $query = trim($this->query);
        $matches = fn (array $command) => $query === ''
            || Str::contains(Str::lower($command['label'].' '.($command['keywords'] ?? '')), Str::lower($query));

        return view('livewire.command-menu', [
            'pages' => array_filter(config('commands.pages'), $matches),
            'actions' => array_filter(config('commands.actions'), $matches),
            'issues' => match (true) {
                ! $this->ready => null,
                $query === '' => auth()->user()->recentIssues()->with('project')->limit(5)->get(),
                default => Issue::visibleTo(auth()->user())->search($query)->with('project')->limit(8)->get(),
            },
        ]);
    }
}
{{-- resources/views/livewire/command-menu.blade.php --}}
<div wire:init="load">
    <x-ds::command-palette
        name="app"
        label="Search Pebblework"
        placeholder="Search pages, actions and issues…"
        emptyText="Nothing matches. Try a page, an action, or an issue key such as HBR-142."
        :filter="false"
        :loading="$issues === null"
        x-on:search="$wire.set('query', $event.detail.query)"
        x-on:copy-page-link="navigator.clipboard?.writeText(location.href)"
    >
        @if ($issues?->isNotEmpty())
            <x-ds::command-group :label="$query === '' ? 'Recent' : 'Issues'">
                @foreach ($issues as $issue)
                    <x-ds::command-item
                        wire:key="issue-{{ $issue->id }}"
                        :label="$issue->key.' '.$issue->title"
                        :href="route('issues.show', $issue->key)"
                        :icon="$query === '' ? 'clock' : 'document-text'"
                        wire:navigate
                    >{{ $issue->project->name }}</x-ds::command-item>
                @endforeach
            </x-ds::command-group>
        @endif

        @foreach (['Navigation' => $pages, 'Actions' => $actions] as $label => $commands)
            @continue(! $commands)
            <x-ds::command-group :label="$label">
                @foreach ($commands as $command)
                    <x-ds::command-item
                        wire:key="command-{{ $command['label'] }}"
                        :label="$command['label']"
                        :href="isset($command['route']) ? route($command['route']) : null"
                        :action="$command['event'] ?? null"
                        :icon="$command['icon'] ?? null"
                        :shortcut="$command['shortcut'] ?? null"
                    />
                @endforeach
            </x-ds::command-group>
        @endforeach

        <x-slot:footer>
            <span class="flex items-center gap-1.5"><x-ds::kbd :keys="['↑', '↓']" separator="" /> to move</span>
            <span class="flex items-center gap-1.5"><x-ds::kbd>Enter</x-ds::kbd> to open</span>
            <span wire:loading class="ms-auto">Searching…</span>
        </x-slot:footer>
    </x-ds::command-palette>
</div>

In the layout, <livewire:command-menu /> takes the place of <x-command-menu … />, and the view composer no longer needs recentIssues.

  • :filter="false", because the server has chosen what matches: filtering again in the browser would hide an issue found by its description. The pages and actions are matched on the server too, for the same reason.
  • :loading is for the first, deferred load. While a search is under way the old results stay, and wire:loading says "Searching…".
  • Every item has a wire:key, so the palette keeps the active option across a re-render when it is still there.

Phone, keyboard, no script

  • Phone: the palette fills the width, near the top, so the field stays above the keyboard; the list scrolls under it.
  • Keyboard: focus goes to the field and stays there. Up and Down move through the options across the groups, wrapping, and Enter follows or runs the active one. Escape empties the field, and on an empty field closes the palette, with focus back where it was. Ctrl+K or Cmd+K opens it and closes it again.
  • No script: the palette is not there at all; the shell's search button is a link to the search page, and every page in it is also in the sidebar or the top nav.

From the package repo: edit this page on GitHub.

Was this page helpful?