command-palette
A search box for actions and pages, opened with ⌘K or Ctrl+K (hotkey). Type to filter, arrow keys to move, Enter to run, Escape to close. items is [{ id, label, group?, hint?, href? }]; an item with href is followed. It's a modal <dialog> with a combobox and a listbox, so screen readers hear the active item as it moves. open is two-way.
Try it: events appear here (select, openchange).
Source: command-palette.zb, and its registry item.
Install
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/command-palette.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/command-palette.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/command-palette.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/command-palette.json
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/command-palette.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/command-palette.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/command-palette.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/command-palette.json
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/command-palette.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/command-palette.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/command-palette.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/command-palette.json
<link rel="stylesheet" href="https://zubra-js.github.io/zubra-ui/themes/default.css">
<script type="module" src="https://zubra-js.github.io/zubra-ui/cdn/command-palette.js"></script>The compiled install writes src/components/zubra/command-palette.js and the theme, and adds @zubra/runtime to your dependencies. The one-file install writes the same path as a plain custom element that needs nothing installed (or download it and load it with a <script type="module">); it's smaller on a page with one to three components, the shared runtime with more.
Usage
<button type="button" id="open-command-palette">Search (Ctrl K)</button>
<command-palette></command-palette>
<script type="module">
import './components/zubra/command-palette.js';
const element = document.querySelector('command-palette');
element.open = false;
element.items = [
{ id: 'new', label: 'New project', group: 'Actions', hint: 'N' },
{ id: 'invite', label: 'Invite a teammate', group: 'Actions' },
{ id: 'theme', label: 'Switch theme', group: 'Actions' },
{ id: 'home', label: 'Dashboard', group: 'Pages', href: '#dashboard' },
{ id: 'billing', label: 'Billing', group: 'Pages', href: '#billing' },
{ id: 'settings', label: 'Settings', group: 'Pages', href: '#settings' },
];
element.addEventListener('select', (event) => console.log('select', event.detail));
element.addEventListener('openchange', (event) => console.log('openchange', event.detail));
document.querySelector('#open-command-palette').addEventListener('click', () => { element.open = true; });
</script>import './components/zubra/command-palette.js';
import { useState } from 'react';
const items = [
{ id: 'new', label: 'New project', group: 'Actions', hint: 'N' },
{ id: 'invite', label: 'Invite a teammate', group: 'Actions' },
{ id: 'theme', label: 'Switch theme', group: 'Actions' },
{ id: 'home', label: 'Dashboard', group: 'Pages', href: '#dashboard' },
{ id: 'billing', label: 'Billing', group: 'Pages', href: '#billing' },
{ id: 'settings', label: 'Settings', group: 'Pages', href: '#settings' },
];
export function CommandPaletteExample() {
const [open, setOpen] = useState(false);
return (
<>
<button type="button" onClick={() => setOpen(true)}>Search (Ctrl K)</button>
<command-palette
open={open}
items={items}
onselect={(event) => console.log('select', event.detail)}
onopenchange={(event) => setOpen(event.detail)}
></command-palette>
</>
);
}<script setup>
import { ref } from 'vue';
import './components/zubra/command-palette.js';
const open = ref(false);
const items = [
{ id: 'new', label: 'New project', group: 'Actions', hint: 'N' },
{ id: 'invite', label: 'Invite a teammate', group: 'Actions' },
{ id: 'theme', label: 'Switch theme', group: 'Actions' },
{ id: 'home', label: 'Dashboard', group: 'Pages', href: '#dashboard' },
{ id: 'billing', label: 'Billing', group: 'Pages', href: '#billing' },
{ id: 'settings', label: 'Settings', group: 'Pages', href: '#settings' },
];
const onSelect = (event) => console.log('select', event.detail);
</script>
<template>
<button type="button" @click="open = true">Search (Ctrl K)</button>
<command-palette
:open.prop="open"
:items.prop="items"
@select="onSelect"
@openchange="open = $event.detail"
></command-palette>
</template>
<!-- vite.config.js: vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.includes('-') } } }) --><script>
import './components/zubra/command-palette.js';
let open = $state(false);
const items = [
{ id: 'new', label: 'New project', group: 'Actions', hint: 'N' },
{ id: 'invite', label: 'Invite a teammate', group: 'Actions' },
{ id: 'theme', label: 'Switch theme', group: 'Actions' },
{ id: 'home', label: 'Dashboard', group: 'Pages', href: '#dashboard' },
{ id: 'billing', label: 'Billing', group: 'Pages', href: '#billing' },
{ id: 'settings', label: 'Settings', group: 'Pages', href: '#settings' },
];
</script>
<button type="button" onclick={() => (open = true)}>Search (Ctrl K)</button>
<command-palette
{open}
{items}
onselect={(event) => console.log('select', event.detail)}
onopenchange={(event) => (open = event.detail)}
></command-palette>import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import './components/zubra/command-palette.js';
@Component({
selector: 'app-command-palette-example',
schemas: [CUSTOM_ELEMENTS_SCHEMA],
template: `
<button type="button" (click)="open = true">Search (Ctrl K)</button>
<command-palette
[open]="open"
[items]="items"
(select)="onSelect($event)"
(openchange)="open = $any($event).detail"
></command-palette>
`,
})
export class CommandPaletteExample {
open = false;
items = [
{ id: 'new', label: 'New project', group: 'Actions', hint: 'N' },
{ id: 'invite', label: 'Invite a teammate', group: 'Actions' },
{ id: 'theme', label: 'Switch theme', group: 'Actions' },
{ id: 'home', label: 'Dashboard', group: 'Pages', href: '#dashboard' },
{ id: 'billing', label: 'Billing', group: 'Pages', href: '#billing' },
{ id: 'settings', label: 'Settings', group: 'Pages', href: '#settings' },
];
onSelect(event: Event) {
console.log('select', (event as CustomEvent).detail);
}
}API
| Property | Type | Default |
|---|---|---|
open | boolean | false |
itemsproperty only | { id, label, group, hint }[] | [] |
placeholder | string | "Search actions and pages…" |
hotkey | string | "k" |
label | string | "Command palette" |
| Event | When |
|---|---|
openchange | It opened or closed; detail is the new state. |
select | An item was chosen; detail is its id. |
Size
2.6 KB gzipped for the component, plus the runtime, which every Zubra component on a page shares (up to 22.1 KB, all of it). On its own, with the parts of the runtime it uses, it's 19.3 KB.