Zubra UI
Code for
GitHub

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

Compiled (any project)
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
One file, no dependencies (5.2 KB gzipped)
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
.zb source (to edit it with Zubra)
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
No build: a script tag, straight from this site
HTML
<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

index.html
<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>
Example.jsx
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>
    </>
  );
}
Example.vue
<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('-') } } }) -->
Example.svelte
<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>
example.component.ts
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

Properties (each is also an HTML attribute of the same name, unless marked)
PropertyTypeDefault
openbooleanfalse
items
property only
{ id, label, group, hint }[][]
placeholderstring"Search actions and pages…"
hotkeystring"k"
labelstring"Command palette"
Events
EventWhen
openchangeIt opened or closed; detail is the new state.
selectAn 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.

Theme

Every component reads shadcn/ui's CSS variables, so any shadcn theme works. Pick a look or a colour, or paste a theme's CSS.

Look: colours, fonts, corners and motion

Accent colour