--- title: "command" description: "Command palette on a native dialog: opens with Cmd\/Ctrl+K, filters grouped items as you type, navigates to links or dispatches a command-select event." category: "Navigation" --- # command Command palette on a native dialog: opens with Cmd/Ctrl+K, filters grouped items as you type, navigates to links or dispatches a command-select event. ## Installation ```bash php artisan ui:add command ``` ## Dependencies - composer: `gehrisandro/tailwind-merge-laravel` ## Usage ```blade @php $commands = [ 'Pages' => [ ['label' => 'Profile', 'href' => '/profile', 'shortcut' => '⌘P'], ['label' => 'Billing', 'href' => '/billing'], ], 'Actions' => [ ['label' => 'New invoice', 'value' => 'new-invoice'], ['label' => 'Log out', 'value' => 'logout'], ], ]; @endphp {{-- Your own trigger, a different shortcut (empty disables it) --}} Open palette {{-- Livewire: listen with Alpine and call the component, e.g. x-on:command-select.window="$wire.run($event.detail.value)" on any element, or in a script block: window.addEventListener('command-select', e => $wire.run(e.detail.value)) --}} ``` ## Example The demo rendered on the docs page. ```blade @php // In a real app these come from the controller. $commands = [ 'Suggestions' => [ ['label' => 'Calendar', 'value' => 'calendar'], ['label' => 'Search emoji', 'value' => 'emoji'], ['label' => 'Calculator', 'value' => 'calculator'], ], 'Settings' => [ ['label' => 'Profile', 'value' => 'profile', 'shortcut' => '⌘P'], ['label' => 'Billing', 'value' => 'billing', 'shortcut' => '⌘B'], ['label' => 'Settings', 'value' => 'settings', 'shortcut' => '⌘S'], ], 'Pages' => [ ['label' => 'Components', 'href' => '/components'], ], ]; @endphp

Press ⌘K or click the button.

``` ## Source Copy this file to the path shown above it. `resources/views/components/ui/command.blade.php` ```blade @props([ 'groups' => [], 'shortcut' => 'k', 'placeholder' => 'Search...', 'searchPlaceholder' => 'Type a command or search...', 'empty' => 'No results found.', 'label' => 'Command palette', ]) @php // A command palette on a native dialog element: top layer, focus trap and // Esc come from the platform. groups is ['Heading' => [item, ...]] where // an item is ['label' => ..., 'href' => ...] (navigates) or // ['label' => ..., 'value' => ...] (dispatches a bubbling command-select // event on the dialog, which reaches window), plus an optional 'shortcut'. $id = 'ui-command-' . bin2hex(random_bytes(4)); $shortcut = strtolower((string) $shortcut); // Not bg-black/50: Chrome does not paint oklab() with alpha in the dialog // top layer, so the scrim would render as nothing. Legacy rgb() works. $overlay = 'absolute inset-0 bg-[rgb(0_0_0/0.5)] opacity-0 transition-opacity duration-200 group-data-[state=open]/command:opacity-100'; $panel = \TailwindMerge\Laravel\Facades\TailwindMerge::merge( 'absolute left-1/2 top-[15%] w-full max-w-[calc(100%-2rem)] -translate-x-1/2 scale-95 overflow-hidden rounded-lg border bg-popover text-popover-foreground opacity-0 shadow-lg transition-all duration-200 group-data-[state=open]/command:scale-100 group-data-[state=open]/command:opacity-100 sm:max-w-lg', $attributes->get('class'), ); $item = 'flex cursor-default select-none items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-none data-[active=true]:bg-accent data-[active=true]:text-accent-foreground'; @endphp
except('class') }}> @isset($trigger) {{ $trigger }} @else @endisset
@foreach ($groups as $heading => $items)
{{ $heading }}
@foreach ($items as $entry) @php($tag = empty($entry['href']) ? 'div' : 'a') <{{ $tag }} id="{{ $id }}-{{ $loop->parent->index }}-{{ $loop->index }}" role="option" data-ui-command-item @if ($tag === 'a') href="{{ $entry['href'] }}" tabindex="-1" @else data-value="{{ $entry['value'] ?? $entry['label'] }}" @endif class="{{ $item }}"> {{ $entry['label'] }} @if (!empty($entry['shortcut'])) {{ $entry['shortcut'] }} @endif @endforeach
@endforeach
@once @endonce ``` ## Theme tokens `--input`, `--popover`, `--popover-foreground`, `--accent`, `--accent-foreground`, `--muted`, `--muted-foreground`, `--border`, `--ring`, `--radius` Pass `:groups` as `['Heading' => [['label' => ..., 'href' => ...], ['label' => ..., 'value' => ...]]]`; items with `href` navigate, items with `value` dispatch a bubbling `command-select` event (detail `{ value, label }`) that reaches `window`. `shortcut` (default `k`, empty to disable) binds Cmd/Ctrl+key, an optional `trigger` slot replaces the default search button, and `placeholder`, `search-placeholder`, `empty` and `label` set the copy.