Skip to content

Command ​

A fast, searchable list of actions: inline, or as a Ctrl/⌘ K command palette in a dialog.

  • Version 1.0.0
  • Bootstrap 5.3+
  • Requires Bootstrap CSS + Bootstrap JS bundle
  • Source on GitHub

Installation ​

bash
npx bootcn add command
bash
pnpm dlx bootcn add command
bash
yarn dlx bootcn add command
bash
bunx --bun bootcn add command

The CLI also installs the design tokens and these components: kbd, button. It prints the <link> and <script> tags to add to your layout.

Usage ​

Group related commands under headings. Typing filters every group (by the item text and data-keywords); empty groups and their separators hide. The dialog version needs the Bootstrap JS bundle; the inline one works without it.

JavaScript API ​

  • data-bootcn-command on the wrapper. Items are role="option" elements with data-value; optional data-href (go to a page) and data-keywords (extra search words).
  • Event bootcn:command:select (bubbles), detail.value and detail.item.
  • For a palette, put the command in a Bootstrap modal with data-bootcn-command-shortcut="k": Ctrl+K / ⌘K toggles it, and the search is cleared and focused each time it opens.
  • Bootcn.command.init(root) / destroy(root).

Accessibility ​

  • The search is a combobox that controls the listbox; arrow keys move the active item (aria-activedescendant), Home/End jump, Enter runs it.
  • Groups are labelled by their headings; the result count is announced.
  • Shortcut hints are hidden from screen readers (they are not real key bindings unless you add them).

Dependencies ​

  • Bootstrap 5.3+ CSS and the JS bundle.
  • themes/tokens/tokens.css.
  • The kbd component.
  • The button component.

Source ​

The exact files the CLI installs (command@1.0.0). View on GitHub · Report an issue

html
<div class="bootcn-command" data-bootcn-command>
  <div class="bootcn-command__search">
    <svg
      class="bootcn-icon"
      width="16"
      height="16"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="2"
      stroke-linecap="round"
      stroke-linejoin="round"
      aria-hidden="true"
      focusable="false"
    >
      <circle cx="11" cy="11" r="7" />
      <path d="m20 20-3.5-3.5" />
    </svg>
    <input
      class="bootcn-command__input"
      type="text"
      role="combobox"
      aria-expanded="true"
      aria-controls="bootcn-command-inline-list"
      aria-autocomplete="list"
      aria-label="Search commands"
      autocomplete="off"
      placeholder="Type a command or search…"
    />
  </div>
  <div
    class="bootcn-command__list"
    id="bootcn-command-inline-list"
    role="listbox"
    aria-label="Commands"
  >
    <div class="bootcn-command__group" role="group" aria-labelledby="bootcn-command-inline-group-0">
      <div class="bootcn-command__heading" id="bootcn-command-inline-group-0" role="none">
        Suggestions
      </div>
      <div
        class="bootcn-command__item"
        role="option"
        id="bootcn-command-inline-calendar"
        data-value="calendar"
        aria-selected="false"
      >
        <svg
          class="bootcn-icon"
          width="16"
          height="16"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
          focusable="false"
        >
          <rect x="3" y="5" width="18" height="16" rx="2" />
          <path d="M16 3v4M8 3v4M3 11h18" />
        </svg>
        <span>Calendar</span>
      </div>
      <div
        class="bootcn-command__item"
        role="option"
        id="bootcn-command-inline-emoji"
        data-value="emoji"
        aria-selected="false"
      >
        <svg
          class="bootcn-icon"
          width="16"
          height="16"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
          focusable="false"
        >
          <circle cx="12" cy="12" r="9" />
          <path d="M8 14s1.5 2 4 2 4-2 4-2" />
          <path d="M9 9h.01M15 9h.01" />
        </svg>
        <span>Search emoji</span>
      </div>
      <div
        class="bootcn-command__item"
        role="option"
        id="bootcn-command-inline-file"
        data-value="file"
        aria-selected="false"
      >
        <svg
          class="bootcn-icon"
          width="16"
          height="16"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
          focusable="false"
        >
          <path d="M14 3H6a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z" />
          <path d="M14 3v6h6" />
        </svg>
        <span>New file</span>
      </div>
    </div>
    <div class="bootcn-command__separator" aria-hidden="true"></div>
    <div class="bootcn-command__group" role="group" aria-labelledby="bootcn-command-inline-group-1">
      <div class="bootcn-command__heading" id="bootcn-command-inline-group-1" role="none">
        Settings
      </div>
      <div
        class="bootcn-command__item"
        role="option"
        id="bootcn-command-inline-profile"
        data-value="profile"
        aria-selected="false"
      >
        <svg
          class="bootcn-icon"
          width="16"
          height="16"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
          focusable="false"
        >
          <circle cx="12" cy="8" r="4" />
          <path d="M4 21c0-4 4-7 8-7s8 3 8 7" />
        </svg>
        <span>Profile</span>
        <span class="bootcn-command__shortcut" aria-hidden="true">⌘P</span>
      </div>
      <div
        class="bootcn-command__item"
        role="option"
        id="bootcn-command-inline-billing"
        data-value="billing"
        aria-selected="false"
      >
        <svg
          class="bootcn-icon"
          width="16"
          height="16"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
          focusable="false"
        >
          <rect x="2" y="5" width="20" height="14" rx="2" />
          <path d="M2 10h20" />
        </svg>
        <span>Billing</span>
        <span class="bootcn-command__shortcut" aria-hidden="true">⌘B</span>
      </div>
      <div
        class="bootcn-command__item"
        role="option"
        id="bootcn-command-inline-settings"
        data-value="settings"
        aria-selected="false"
      >
        <svg
          class="bootcn-icon"
          width="16"
          height="16"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
          focusable="false"
        >
          <circle cx="12" cy="12" r="3" />
          <path
            d="M19.4 15a1.7 1.7 0 0 0 .3 1.8l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-1.8-.3 1.7 1.7 0 0 0-1 1.5V21a2 2 0 1 1-4 0v-.1a1.7 1.7 0 0 0-1.1-1.5 1.7 1.7 0 0 0-1.8.3l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1a1.7 1.7 0 0 0 .3-1.8 1.7 1.7 0 0 0-1.5-1H3a2 2 0 1 1 0-4h.1a1.7 1.7 0 0 0 1.5-1.1 1.7 1.7 0 0 0-.3-1.8l-.1-.1a2 2 0 1 1 2.8-2.8l.1.1a1.7 1.7 0 0 0 1.8.3H9a1.7 1.7 0 0 0 1-1.5V3a2 2 0 1 1 4 0v.1a1.7 1.7 0 0 0 1 1.5 1.7 1.7 0 0 0 1.8-.3l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.3 1.8V9a1.7 1.7 0 0 0 1.5 1H21a2 2 0 1 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1z"
          />
        </svg>
        <span>Settings</span>
        <span class="bootcn-command__shortcut" aria-hidden="true">⌘S</span>
      </div>
    </div>
  </div>
  <p class="bootcn-command__empty" hidden>No results found.</p>
  <p class="visually-hidden" role="status" aria-live="polite" data-bootcn-command-status></p>
</div>

<button
  type="button"
  class="btn bootcn-button bootcn-button--outline mt-4"
  data-bs-toggle="modal"
  data-bs-target="#bootcn-command-dialog"
>
  Open command palette
  <span class="bootcn-kbd-group" aria-hidden="true"
    ><kbd class="bootcn-kbd">Ctrl</kbd><kbd class="bootcn-kbd">K</kbd></span
  >
</button>

<div
  class="modal fade bootcn-command-dialog"
  id="bootcn-command-dialog"
  tabindex="-1"
  aria-label="Command palette"
  data-bootcn-command-shortcut="k"
>
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="bootcn-command" data-bootcn-command>
        <div class="bootcn-command__search">
          <svg
            class="bootcn-icon"
            width="16"
            height="16"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
            aria-hidden="true"
            focusable="false"
          >
            <circle cx="11" cy="11" r="7" />
            <path d="m20 20-3.5-3.5" />
          </svg>
          <input
            class="bootcn-command__input"
            type="text"
            role="combobox"
            aria-expanded="true"
            aria-controls="bootcn-command-dialog-list"
            aria-autocomplete="list"
            aria-label="Search commands"
            autocomplete="off"
            placeholder="Type a command or search…"
          />
        </div>
        <div
          class="bootcn-command__list"
          id="bootcn-command-dialog-list"
          role="listbox"
          aria-label="Commands"
        >
          <div
            class="bootcn-command__group"
            role="group"
            aria-labelledby="bootcn-command-dialog-group-0"
          >
            <div class="bootcn-command__heading" id="bootcn-command-dialog-group-0" role="none">
              Suggestions
            </div>
            <div
              class="bootcn-command__item"
              role="option"
              id="bootcn-command-dialog-calendar"
              data-value="calendar"
              aria-selected="false"
            >
              <svg
                class="bootcn-icon"
                width="16"
                height="16"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                aria-hidden="true"
                focusable="false"
              >
                <rect x="3" y="5" width="18" height="16" rx="2" />
                <path d="M16 3v4M8 3v4M3 11h18" />
              </svg>
              <span>Calendar</span>
            </div>
            <div
              class="bootcn-command__item"
              role="option"
              id="bootcn-command-dialog-emoji"
              data-value="emoji"
              aria-selected="false"
            >
              <svg
                class="bootcn-icon"
                width="16"
                height="16"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                aria-hidden="true"
                focusable="false"
              >
                <circle cx="12" cy="12" r="9" />
                <path d="M8 14s1.5 2 4 2 4-2 4-2" />
                <path d="M9 9h.01M15 9h.01" />
              </svg>
              <span>Search emoji</span>
            </div>
            <div
              class="bootcn-command__item"
              role="option"
              id="bootcn-command-dialog-file"
              data-value="file"
              aria-selected="false"
            >
              <svg
                class="bootcn-icon"
                width="16"
                height="16"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                aria-hidden="true"
                focusable="false"
              >
                <path d="M14 3H6a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z" />
                <path d="M14 3v6h6" />
              </svg>
              <span>New file</span>
            </div>
          </div>
          <div class="bootcn-command__separator" aria-hidden="true"></div>
          <div
            class="bootcn-command__group"
            role="group"
            aria-labelledby="bootcn-command-dialog-group-1"
          >
            <div class="bootcn-command__heading" id="bootcn-command-dialog-group-1" role="none">
              Settings
            </div>
            <div
              class="bootcn-command__item"
              role="option"
              id="bootcn-command-dialog-profile"
              data-value="profile"
              aria-selected="false"
            >
              <svg
                class="bootcn-icon"
                width="16"
                height="16"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                aria-hidden="true"
                focusable="false"
              >
                <circle cx="12" cy="8" r="4" />
                <path d="M4 21c0-4 4-7 8-7s8 3 8 7" />
              </svg>
              <span>Profile</span>
              <span class="bootcn-command__shortcut" aria-hidden="true">⌘P</span>
            </div>
            <div
              class="bootcn-command__item"
              role="option"
              id="bootcn-command-dialog-billing"
              data-value="billing"
              aria-selected="false"
            >
              <svg
                class="bootcn-icon"
                width="16"
                height="16"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                aria-hidden="true"
                focusable="false"
              >
                <rect x="2" y="5" width="20" height="14" rx="2" />
                <path d="M2 10h20" />
              </svg>
              <span>Billing</span>
              <span class="bootcn-command__shortcut" aria-hidden="true">⌘B</span>
            </div>
            <div
              class="bootcn-command__item"
              role="option"
              id="bootcn-command-dialog-settings"
              data-value="settings"
              aria-selected="false"
            >
              <svg
                class="bootcn-icon"
                width="16"
                height="16"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                aria-hidden="true"
                focusable="false"
              >
                <circle cx="12" cy="12" r="3" />
                <path
                  d="M19.4 15a1.7 1.7 0 0 0 .3 1.8l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-1.8-.3 1.7 1.7 0 0 0-1 1.5V21a2 2 0 1 1-4 0v-.1a1.7 1.7 0 0 0-1.1-1.5 1.7 1.7 0 0 0-1.8.3l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1a1.7 1.7 0 0 0 .3-1.8 1.7 1.7 0 0 0-1.5-1H3a2 2 0 1 1 0-4h.1a1.7 1.7 0 0 0 1.5-1.1 1.7 1.7 0 0 0-.3-1.8l-.1-.1a2 2 0 1 1 2.8-2.8l.1.1a1.7 1.7 0 0 0 1.8.3H9a1.7 1.7 0 0 0 1-1.5V3a2 2 0 1 1 4 0v.1a1.7 1.7 0 0 0 1 1.5 1.7 1.7 0 0 0 1.8-.3l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.3 1.8V9a1.7 1.7 0 0 0 1.5 1H21a2 2 0 1 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1z"
                />
              </svg>
              <span>Settings</span>
              <span class="bootcn-command__shortcut" aria-hidden="true">⌘S</span>
            </div>
          </div>
        </div>
        <p class="bootcn-command__empty" hidden>No results found.</p>
        <p class="visually-hidden" role="status" aria-live="polite" data-bootcn-command-status></p>
      </div>
    </div>
  </div>
</div>
css
.bootcn-command {
  display: flex;
  flex-direction: column;
  max-width: 28rem;
  overflow: hidden;
  border: var(--bootcn-border-width) solid var(--bootcn-border-color);
  border-radius: var(--bootcn-radius-lg);
  background-color: var(--bootcn-background);
  color: var(--bootcn-foreground);
  box-shadow: var(--bootcn-shadow-md);
  font-size: 0.875rem;
}

.bootcn-command__search {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0 0.75rem;
  border-bottom: var(--bootcn-border-width) solid var(--bootcn-border-color);
  color: var(--bootcn-muted-foreground);
}

.bootcn-command__input {
  flex: 1 1 auto;
  min-width: 0;
  height: 2.75rem;
  border: 0;
  background: transparent;
  color: var(--bootcn-foreground);
  font-size: 0.875rem;
  outline: 0;
}

.bootcn-command__input::placeholder {
  color: var(--bootcn-muted-foreground);
}

.bootcn-command__list {
  max-height: 18rem;
  overflow-y: auto;
  padding: 0.25rem;
}

.bootcn-command__heading {
  padding: 0.375rem 0.5rem;
  color: var(--bootcn-muted-foreground);
  font-size: 0.75rem;
  font-weight: 500;
}

.bootcn-command__item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.375rem 0.5rem;
  border-radius: var(--bootcn-radius-sm);
  cursor: pointer;
  user-select: none;
}

.bootcn-command__item .bootcn-icon {
  flex: none;
  color: var(--bootcn-muted-foreground);
}

.bootcn-command__item.is-active {
  background-color: var(--bootcn-accent);
  color: var(--bootcn-accent-foreground);
}

.bootcn-command__shortcut {
  margin-left: auto;
  color: var(--bootcn-muted-foreground);
  font-size: 0.75rem;
  letter-spacing: 0.1em;
}

.bootcn-command__separator {
  height: 1px;
  margin: 0.25rem -0.25rem;
  background-color: var(--bootcn-border-color);
}

.bootcn-command__empty {
  margin: 0;
  padding: 1.5rem;
  color: var(--bootcn-muted-foreground);
  text-align: center;
}

.bootcn-command [hidden] {
  display: none !important;
}

/* The palette dialog: the command fills the modal content. */
.bootcn-command-dialog .modal-content {
  border: 0;
  background: transparent;
}

.bootcn-command-dialog .bootcn-command {
  max-width: none;
  box-shadow: var(--bootcn-shadow-lg);
}
js
(function () {
  'use strict';

  const ns = (window.Bootcn = window.Bootcn || {});
  if (ns.command) return; // already loaded: idempotent

  const ROOT = '[data-bootcn-command]';
  const INIT = 'data-bootcn-command-initialized';
  const cleanups = new Map();
  let listening = false;

  function fold(text) {
    return text.normalize('NFD').replace(/[̀-ͯ]/g, '').toLowerCase();
  }

  function setup(root) {
    const input = root.querySelector('.bootcn-command__input');
    const list = root.querySelector('[role="listbox"]');
    if (!input || !list) return null;
    const empty = root.querySelector('.bootcn-command__empty');
    const status = root.querySelector('[data-bootcn-command-status]');
    const items = Array.from(list.querySelectorAll('[role="option"]'));
    let active = null;

    const visible = () => items.filter((item) => !item.hidden && !item.closest('[hidden]'));

    function setActive(item) {
      if (active) {
        active.classList.remove('is-active');
        active.setAttribute('aria-selected', 'false');
      }
      active = item;
      if (item) {
        item.classList.add('is-active');
        item.setAttribute('aria-selected', 'true');
        input.setAttribute('aria-activedescendant', item.id);
        item.scrollIntoView({ block: 'nearest' });
      } else {
        input.removeAttribute('aria-activedescendant');
      }
    }

    function filter() {
      const query = fold(input.value.trim());
      items.forEach((item) => {
        const text = fold(item.textContent + ' ' + (item.getAttribute('data-keywords') || ''));
        item.hidden = query !== '' && !text.includes(query);
      });
      list.querySelectorAll('[role="group"]').forEach((group) => {
        group.hidden = !group.querySelector('[role="option"]:not([hidden])');
      });
      // A separator shows only between two visible groups.
      list.querySelectorAll('.bootcn-command__separator').forEach((separator) => {
        const prev = separator.previousElementSibling;
        const next = separator.nextElementSibling;
        separator.hidden = !(prev && !prev.hidden && next && !next.hidden);
      });
      const count = visible().length;
      list.hidden = count === 0;
      if (empty) empty.hidden = count !== 0;
      input.setAttribute('aria-expanded', String(count !== 0));
      if (status)
        status.textContent =
          count === 0 ? 'No results.' : count + (count === 1 ? ' result.' : ' results.');
      setActive(visible()[0] || null);
    }

    function run(item) {
      root.dispatchEvent(
        new CustomEvent('bootcn:command:select', {
          bubbles: true,
          detail: { value: item.getAttribute('data-value'), item: item },
        }),
      );
      const href = item.getAttribute('data-href');
      const dialog = root.closest('.modal');
      if (dialog && window.bootstrap) window.bootstrap.Modal.getOrCreateInstance(dialog).hide();
      if (href) window.location.href = href;
    }

    function move(step) {
      const list = visible();
      if (list.length === 0) return;
      const index = list.indexOf(active);
      setActive(list[(index + step + list.length) % list.length]);
    }

    function onKeydown(event) {
      if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
        event.preventDefault();
        move(event.key === 'ArrowDown' ? 1 : -1);
      } else if (event.key === 'Home' || event.key === 'End') {
        event.preventDefault();
        const list = visible();
        setActive(event.key === 'Home' ? list[0] : list[list.length - 1]);
      } else if (event.key === 'Enter' && active) {
        event.preventDefault();
        run(active);
      }
    }

    function onPointer(event) {
      const item = event.target.closest('[role="option"]');
      if (!item) return;
      if (event.type === 'mousemove') {
        if (item !== active) setActive(item);
        return;
      }
      event.preventDefault();
      run(item);
    }

    function reset() {
      input.value = '';
      filter();
    }

    input.addEventListener('input', filter);
    input.addEventListener('keydown', onKeydown);
    list.addEventListener('mousemove', onPointer);
    list.addEventListener('click', onPointer);
    root.setAttribute(INIT, '');
    filter();

    const cleanup = function () {
      input.removeEventListener('input', filter);
      input.removeEventListener('keydown', onKeydown);
      list.removeEventListener('mousemove', onPointer);
      list.removeEventListener('click', onPointer);
      root.removeAttribute(INIT);
    };
    cleanup.reset = reset;
    cleanup.input = input;
    return cleanup;
  }

  // Dialog palettes: reset and focus the search when shown; Ctrl/⌘ + key opens.
  function onShown(event) {
    const root = event.target.querySelector && event.target.querySelector(ROOT);
    const entry = root && cleanups.get(root);
    if (!entry) return;
    entry.reset();
    entry.input.focus();
  }

  function onShortcut(event) {
    if (!(event.ctrlKey || event.metaKey) || event.altKey || event.shiftKey) return;
    const key = event.key.toLowerCase();
    const dialog = document.querySelector('[data-bootcn-command-shortcut="' + key + '"]');
    if (!dialog || !window.bootstrap) return;
    event.preventDefault();
    window.bootstrap.Modal.getOrCreateInstance(dialog).toggle();
  }

  function init(root = document) {
    root.querySelectorAll(ROOT + ':not([' + INIT + '])').forEach((element) => {
      const cleanup = setup(element);
      if (cleanup) cleanups.set(element, cleanup);
    });
    if (!listening) {
      document.addEventListener('shown.bs.modal', onShown);
      document.addEventListener('keydown', onShortcut);
      listening = true;
    }
  }

  function destroy(root = document) {
    cleanups.forEach((cleanup, element) => {
      if (root === document || root.contains(element)) {
        cleanup();
        cleanups.delete(element);
      }
    });
    if (root === document && listening) {
      document.removeEventListener('shown.bs.modal', onShown);
      document.removeEventListener('keydown', onShortcut);
      listening = false;
    }
  }

  ns.command = { init: init, destroy: destroy };

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', () => init());
  } else {
    init();
  }
})();

Released under the MIT License. Not affiliated with Bootstrap or shadcn/ui.