<x-widget.command>
Command palette Blade component for Laravel and Livewire
A command palette: Ctrl+K (Cmd+K on a Mac) opens a search box that finds pages and actions as you type, in groups. With suggest-url it adds your app's own results, and recent keeps the last few choices. Enter opens a page or clicks an action, so wire:click works.
php artisan bladewell:add command
- Also adds
- Icon
- Works with
- Laravel 12 and 13, Livewire 3 and 4, Tailwind CSS 4.1+, PHP 8.3+
Usage
In scripts
window.command is available once the command script has loaded. <x-widget.command id="palette" …>
command.open('palette');
command.close('palette');
// Choosing an item, for your own analytics or shortcuts.
document.addEventListener('command:chosen', (event) => console.log('Chosen:', event.detail.item.textContent.trim()));
Livewire
Inside a Livewire component, an item without href takes wire:click: choosing it by Enter or a click runs the action and closes the palette. Open it from the component with $this->dispatch('command-open', id: 'palette').
<x-widget.command id="palette" label="Search pages and actions">
<x-widget.command.item wire:click="archiveAll" icon="inbox">Archive all read</x-widget.command.item>
<x-widget.command.item href="/settings" icon="settings">Settings</x-widget.command.item>
</x-widget.command>
On the server
The command palette can't stop a request that skips it. Here's what to check on your server, and what to keep cheap.
Security
-
The
suggest-urlendpoint answers anyone who calls it. Scope results to what the person may see, and leave out pages and actions they can't open.OWASP: Insecure direct object references · Laravel: Authorization
Server cost
-
With
suggest-url, each pause in typing is a request. Throttle the route,limit()the results, and consider caching answers briefly, keyed as for search: by the query, the user and the active workspace if results depend on it; the pages and actions you list in the page itself cost nothing.
Examples
Basic
Ctrl+K (Cmd+K on a Mac) or the button opens it. Typing filters by label and keywords, every word in any order: try "new inv". An item with href opens that page; one without is clicked, so give it wire:click or listen for the click, as the script beside this does.
Show code Hide code
<x-widget.command id="palette" label="Search pages and actions">
<x-widget.command.group label="Pages">
<x-widget.command.item href="#dashboard" icon="home" keywords="start overview">Dashboard</x-widget.command.item>
<x-widget.command.item href="#invoices" icon="file" keywords="billing bills">Invoices</x-widget.command.item>
<x-widget.command.item href="#customers" icon="users" keywords="clients people">Customers</x-widget.command.item>
<x-widget.command.item href="#settings" icon="settings" keywords="account profile preferences" shortcut="G S">Settings</x-widget.command.item>
</x-widget.command.group>
<x-widget.command.group label="Actions">
<x-widget.command.item icon="plus" keywords="create add" data-palette-action="New invoice">New invoice</x-widget.command.item>
<x-widget.command.item icon="copy" keywords="share url" data-palette-action="Link copied">Copy page link</x-widget.command.item>
</x-widget.command.group>
</x-widget.command>
<p id="palette-action" role="status" class="text-foreground/70 mt-3 text-sm"></p>
// An item without href is clicked when chosen, by the pointer or Enter: here, it says what it did.
document.addEventListener('click', (event) => {
const item = event.target.closest('[data-palette-action]');
if (item) {
document.getElementById('palette-action').textContent = item.dataset.paletteAction;
}
});
Search your app
suggest-url asks your app as people type (GET ?q=…, JSON), with the same answer the search widget's suggestions take; its results follow the items that match. recent keeps the last five pages chosen, in this browser only, and lists them before anything is typed. trigger="false" leaves out the button: here a link opens it, with data-command-open.
, or press Ctrl+J.
Show code Hide code
<x-widget.command id="product-palette" label="Search products" placeholder="Search products…" :suggest-url="route('products.suggest')" :trigger="false" shortcut="j" recent>
<x-widget.command.group label="Browse">
<x-widget.command.item href="#new-arrivals" icon="star">New arrivals</x-widget.command.item>
<x-widget.command.item href="#on-sale" icon="tag" keywords="discount offers">On sale</x-widget.command.item>
</x-widget.command.group>
</x-widget.command>
<p class="text-sm">
<button type="button" data-command-open="product-palette" class="text-link hover:text-link-hover underline underline-offset-4">Search products</button>, or press Ctrl+J.
</p>
Props
Other attributes, such as autocomplete or data-*, are passed through to the element. class styles the component's outer wrapper.
<x-widget.command>
| Prop | Default | Description |
|---|---|---|
| id |
null
|
The palette's id: anything with data-command-open="{id}" opens it, and so does command.open('{id}'). Made up when left out. |
| label |
'Search'
|
Its name for screen readers, and the trigger button's text. |
| placeholder |
'Type a command or search…'
|
The search box's placeholder. |
| shortcut |
'k'
|
The key that opens it with Ctrl (Cmd on a Mac): Ctrl+K by default. false for none. |
| trigger |
true
|
A button that opens it, styled like a search box, with the shortcut on it. false to open it only by the shortcut or your own data-command-open element. |
| suggest-url |
null
|
Your app's search, asked as people type (GET ?q=…, JSON): the same answer the search widget's suggest-url takes. Its results are listed after the items that match. |
| recent |
false
|
Keeps the last few items chosen, in this browser only (localStorage), and shows them before anything is typed. |
| empty |
'No results'
|
What it says when nothing matches. |
<x-widget.command.group>
| Prop | Default | Description |
|---|---|---|
| label | Required | The heading over the group's items, which also names the group for screen readers. |
<x-widget.command.item>
| Prop | Default | Description |
|---|---|---|
| href |
null
|
A page to open when it's chosen. Without one, choosing it clicks it: give it wire:click, or listen for the click. |
| icon |
null
|
An icon before the label, by name, as the icon widget takes it. |
| keywords |
null
|
More words it's found by, besides its label: "settings account profile". |
| shortcut |
null
|
A key hint shown at the end of the row, such as "G D". Only shown: the palette doesn't bind it. |
| disabled |
false
|
Shown greyed out, but it can't be chosen: the arrow keys pass it by, and Enter and a click do nothing. |
Accessibility
All 2 examples above are checked with axe-core against the WCAG 2.2 A and AA rules, in the light theme and the dark one, both as the page draws and with each popover, dialog, toast and tooltip opened, on every change. A change that fails can't be merged. Where axe can't decide, such as contrast on SVG text, the test measures the colours itself instead of letting it pass.
Automated checks can't judge everything: how it sounds in a screen reader, and how it feels to use from the keyboard, still need a person. Check those on your own pages too.
Source
What bladewell:add command writes to your app with the default namespaces. Prefer to copy by hand? Take these files, plus the ones from
the components it also adds , and the theme and base CSS.
resources/views/components/widget/command/group.blade.php Show
resources/views/components/widget/command/index.blade.php Show
resources/views/components/widget/command/item.blade.php Show
resources/js/widget/command/index.js Show
app/View/Widget/ElementIds.php Show