Abdulkader Safi

LaralCN-UI command palette: Cmd+K on a native dialog

The command palette is the second component from PR #7: a searchable list of links and actions on a native dialog element, opened with Cmd/Ctrl+K. Links navigate, and actions dispatch a command-select event you can handle in plain JavaScript, Alpine or Livewire. Building it also exposed an Esc bug that had been sitting in every dialog component in the library.

5 min read

Share
The LaralCN-UI command palette open on its docs page, with grouped items and a search box at the top.

The command palette is the second component from PR #7: a searchable list of links and actions on a native <dialog>, opened with Cmd/Ctrl+K. Links navigate, and actions dispatch a command-select event you can handle in plain JavaScript, Alpine or Livewire. Building it also exposed an Esc bug that had been sitting in every dialog component in the library.

You pass the palette an array of groups. Each item is either a link (it has an href) or an action (it has a value), with an optional shortcut hint on the right:

@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

<x-ui.command :groups="$commands" />

By default that renders a search-style button with a key hint, and the palette itself. Link items render as real <a href> elements, so middle-click and the status bar preview behave like any other link. Action items render as plain divs with a data-value.

The search box filters as you type, with the same fold as the combobox from part 13: case, Latin accents and Arabic harakat are ignored. A group with no matching items hides its heading too, and an empty result shows the empty text. Arrow keys move through the visible items, Home and End jump, and Enter runs the active one.

A native dialog does the hard parts

The palette is a <dialog> opened with showModal(). That one call gives it the top layer (no z-index fights with your navbar), a real focus trap, and Esc handling, all from the browser. The script adds the open and close animation, the filtering, and overflow: hidden on <html> while it is open.

Two details took longer than expected.

The scrim. The obvious class for a dimmed backdrop is bg-black/50. Tailwind v4 compiles that to an oklab() colour with alpha, and Chrome did not paint it inside the dialog's top layer, so the scrim showed as nothing. The overlay uses a legacy rgb() value instead:

// 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';

Direction. The top layer does not rescue a dialog whose ancestor is display: none, so the first time the palette opens it moves itself to <body>. That move loses the dir and lang it inherited from where you placed it, which is the same problem part 9 fixed for the other dialogs. It copies them over before moving:

if (dialog.parentElement !== document.body) {
    if (!dialog.dir) dialog.dir = getComputedStyle(dialog.parentElement).direction;
    var lang = dialog.parentElement.closest('[lang]');
    if (!dialog.lang && lang) dialog.lang = lang.lang;
    document.body.appendChild(dialog);
}

Cmd+K on a Mac, Ctrl K everywhere else

The shortcut prop (default k) binds Cmd+key and Ctrl+key to toggle the palette. Set it to j to use Cmd/Ctrl+J, or to an empty string to disable it. The key hint in the default button is rendered on the server as ⌘K, and on load the script swaps it to Ctrl K on anything that is not a Mac or an iOS device. The hint has dir="ltr" so it does not flip in Arabic.

If you want your own button, the trigger slot replaces the default one:

<x-ui.command :groups="$commands" shortcut="j">
    <x-slot:trigger>
        <x-ui.button variant="outline">Open palette</x-ui.button>
    </x-slot:trigger>
</x-ui.command>

Actions are events, so the backend decides

A component that copies into your app should not know what "Log out" means in your app. So action items do one thing: dispatch a command-select event with the value and the label.

dialog.dispatchEvent(new CustomEvent('command-select', {
    bubbles: true,
    detail: {
        value: item.dataset.value,
        label: item.querySelector('[data-ui-command-label]').textContent.trim(),
    },
}));

It is dispatched once, on the dialog, and it bubbles. Because the dialog sits in <body> by then, the event reaches document and window, and a listener there fires exactly once per selection. Handling it in plain JavaScript:

window.addEventListener('command-select', (event) => {
    const { value, label } = event.detail;
    if (value === 'new-invoice') location.href = '/invoices/create';
});

In a Livewire component, put an Alpine listener on any element and call a method:

<div x-on:command-select.window="$wire.run($event.detail.value)">
    <x-ui.command :groups="$commands" />
</div>

The search box's own input and change events are stopped in the capture phase, the same fix as the combobox, so typing in the palette never reaches a wire:model or form listener around it.

The Esc bug it found

While testing, pressing Esc closed the palette instantly with no exit animation and left the page unable to scroll. The cancel listener that should have animated it out never ran: cancel does not bubble, and the listener was on document in the bubble phase. The palette now listens in the capture phase.

The same mistake turned out to be in dialog, alert-dialog, sheet and the mobile sidebar, and the fix shipped in the same PR. That bug gets its own write-up in part 20.

Where it is

composer require --dev safi/laralcn-ui
php artisan ui:init
php artisan ui:add command

Docs: https://laralcn-ui.abdulkadersafi.com/components/command, Arabic at https://laralcn-ui.abdulkadersafi.com/ar/components/command. GitHub: https://github.com/Abdulkader-Safi/LaralCN-UI. MIT licensed.

Next: the data table, with sorting, filtering, row selection and pagination built from a columns and rows array.

Last updated 1 Dec 2026 · filed under Blade, javascript, shadcn, livewire, laralcn-ui

Every entry on this project

26 build notes, in order

Including the ones where nothing worked. You are on part 14.

Read the series →

FAQ

Frequently asked questions

How do I run a Livewire method from the command palette?

Give the item a value, then listen for the event with Alpine: x-on:command-select.window="$wire.run($event.detail.value)". The event's detail carries value and label.

Can I change or turn off the Cmd/Ctrl+K shortcut?

Yes. Set shortcut="j" for Cmd/Ctrl+J, or shortcut="" to disable the keyboard shortcut entirely.

Does the command palette need Alpine.js?

No. It ships its own inline script and uses a native <dialog>. Alpine is only one way to listen for the command-select event.

Why is the backdrop bg-[rgb(0_0_0/0.5)] instead of bg-black/50?

Tailwind v4 compiles bg-black/50 to an oklab() colour with alpha, which Chrome did not paint inside the dialog's top layer. A legacy rgb() value renders correctly.

Written by

Abdulkader Safi

Software Engineer

Lead engineer at DSRPT, from Lebanon and based in Kuwait. I write about the tools and bugs from real client work, with the numbers I measured.

About me → GitHub LinkedIn

Need this kind of work done on your project?

Start a project →

Keep reading

More from LaralCN-UI: shadcn-style Blade components and blocks you own

All 26 entries →