Skip to content

Combobox ​

A searchable select: type to filter a list, pick one option or several (shown as chips), fully keyboard accessible.

Installation ​

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

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

Usage ​

Type to filter the options; matching ignores case and accents. With one choice the input shows the chosen label; with several, each choice is a chip with a remove button and Backspace removes the last one. Render the options on the server; for very long lists, filter on the server and replace the options.

JavaScript API ​

  • data-bootcn-combobox on the wrapper; data-bootcn-combobox-name is the form field name; data-bootcn-combobox-multiple allows several choices.
  • Options are role="option" elements with data-value; mark initial choices with aria-selected="true".
  • Single choice writes its value to the hidden input marked data-bootcn-combobox-value; multiple choice creates hidden inputs named <name>[].
  • Event bootcn:combobox:change (bubbles), detail.value: a string, or an array for multiple choice.
  • Bootcn.combobox.init(root) / destroy(root).

Accessibility ​

  • WAI-ARIA combobox with a listbox popup: aria-expanded, aria-controls and aria-activedescendant, so focus stays in the input while arrow keys move through the options.
  • Keys: Down/Up move (and open), Alt+Down opens, Alt+Up closes, Home/End, Enter chooses, Escape closes (a second Escape clears the text).
  • The number of results and each added or removed choice are announced in a polite live region.
  • Chip remove buttons have names ("Remove Astro").

Dependencies ​

  • Bootstrap 5.3+ CSS.
  • themes/tokens/tokens.css.
  • The label component.

Source ​

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

html
<div class="bootcn-combobox" data-bootcn-combobox data-bootcn-combobox-name="framework">
  <label
    class="bootcn-label"
    id="bootcn-combobox-framework-label"
    for="bootcn-combobox-framework-input"
    >Framework</label
  >
  <div class="bootcn-combobox__control">
    <input
      class="bootcn-combobox__input"
      id="bootcn-combobox-framework-input"
      type="text"
      role="combobox"
      aria-autocomplete="list"
      aria-expanded="false"
      aria-controls="bootcn-combobox-framework-listbox"
      autocomplete="off"
      placeholder="Search frameworks…"
    />
    <button type="button" class="bootcn-combobox__toggle" tabindex="-1" aria-hidden="true">
      <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="m7 15 5 5 5-5" />
        <path d="m7 9 5-5 5 5" />
      </svg>
    </button>
  </div>
  <ul
    class="bootcn-combobox__listbox"
    id="bootcn-combobox-framework-listbox"
    role="listbox"
    aria-labelledby="bootcn-combobox-framework-label"
    hidden
  >
    <li
      class="bootcn-combobox__option"
      id="bootcn-combobox-framework-astro"
      role="option"
      data-value="astro"
      aria-selected="false"
    >
      <span>Astro</span>
      <svg
        class="bootcn-icon bootcn-combobox__check"
        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="M20 6 9 17l-5-5" />
      </svg>
    </li>
    <li
      class="bootcn-combobox__option"
      id="bootcn-combobox-framework-laravel"
      role="option"
      data-value="laravel"
      aria-selected="true"
    >
      <span>Laravel</span>
      <svg
        class="bootcn-icon bootcn-combobox__check"
        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="M20 6 9 17l-5-5" />
      </svg>
    </li>
    <li
      class="bootcn-combobox__option"
      id="bootcn-combobox-framework-django"
      role="option"
      data-value="django"
      aria-selected="false"
    >
      <span>Django</span>
      <svg
        class="bootcn-icon bootcn-combobox__check"
        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="M20 6 9 17l-5-5" />
      </svg>
    </li>
    <li
      class="bootcn-combobox__option"
      id="bootcn-combobox-framework-rails"
      role="option"
      data-value="rails"
      aria-selected="false"
    >
      <span>Ruby on Rails</span>
      <svg
        class="bootcn-icon bootcn-combobox__check"
        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="M20 6 9 17l-5-5" />
      </svg>
    </li>
    <li
      class="bootcn-combobox__option"
      id="bootcn-combobox-framework-symfony"
      role="option"
      data-value="symfony"
      aria-selected="false"
    >
      <span>Symfony</span>
      <svg
        class="bootcn-icon bootcn-combobox__check"
        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="M20 6 9 17l-5-5" />
      </svg>
    </li>
    <li
      class="bootcn-combobox__option"
      id="bootcn-combobox-framework-express"
      role="option"
      data-value="express"
      aria-selected="false"
    >
      <span>Express</span>
      <svg
        class="bootcn-icon bootcn-combobox__check"
        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="M20 6 9 17l-5-5" />
      </svg>
    </li>
    <li
      class="bootcn-combobox__option"
      id="bootcn-combobox-framework-flask"
      role="option"
      data-value="flask"
      aria-selected="false"
    >
      <span>Flask</span>
      <svg
        class="bootcn-icon bootcn-combobox__check"
        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="M20 6 9 17l-5-5" />
      </svg>
    </li>
    <li
      class="bootcn-combobox__option"
      id="bootcn-combobox-framework-wordpress"
      role="option"
      data-value="wordpress"
      aria-selected="false"
    >
      <span>WordPress</span>
      <svg
        class="bootcn-icon bootcn-combobox__check"
        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="M20 6 9 17l-5-5" />
      </svg>
    </li>
  </ul>
  <p class="bootcn-combobox__empty" hidden>No framework found.</p>
  <p class="visually-hidden" role="status" aria-live="polite" data-bootcn-combobox-status></p>
  <input type="hidden" name="framework" value="" data-bootcn-combobox-value />
</div>

<div class="mt-4">
  <div
    class="bootcn-combobox"
    data-bootcn-combobox
    data-bootcn-combobox-name="stack"
    data-bootcn-combobox-multiple
  >
    <label class="bootcn-label" id="bootcn-combobox-stack-label" for="bootcn-combobox-stack-input"
      >Your stack</label
    >
    <div class="bootcn-combobox__control">
      <span class="bootcn-combobox__chips"></span>
      <input
        class="bootcn-combobox__input"
        id="bootcn-combobox-stack-input"
        type="text"
        role="combobox"
        aria-autocomplete="list"
        aria-expanded="false"
        aria-controls="bootcn-combobox-stack-listbox"
        autocomplete="off"
        placeholder="Search frameworks…"
      />
      <button type="button" class="bootcn-combobox__toggle" tabindex="-1" aria-hidden="true">
        <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="m7 15 5 5 5-5" />
          <path d="m7 9 5-5 5 5" />
        </svg>
      </button>
    </div>
    <ul
      class="bootcn-combobox__listbox"
      id="bootcn-combobox-stack-listbox"
      role="listbox"
      aria-labelledby="bootcn-combobox-stack-label"
      aria-multiselectable="true"
      hidden
    >
      <li
        class="bootcn-combobox__option"
        id="bootcn-combobox-stack-astro"
        role="option"
        data-value="astro"
        aria-selected="true"
      >
        <span>Astro</span>
        <svg
          class="bootcn-icon bootcn-combobox__check"
          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="M20 6 9 17l-5-5" />
        </svg>
      </li>
      <li
        class="bootcn-combobox__option"
        id="bootcn-combobox-stack-laravel"
        role="option"
        data-value="laravel"
        aria-selected="false"
      >
        <span>Laravel</span>
        <svg
          class="bootcn-icon bootcn-combobox__check"
          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="M20 6 9 17l-5-5" />
        </svg>
      </li>
      <li
        class="bootcn-combobox__option"
        id="bootcn-combobox-stack-django"
        role="option"
        data-value="django"
        aria-selected="true"
      >
        <span>Django</span>
        <svg
          class="bootcn-icon bootcn-combobox__check"
          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="M20 6 9 17l-5-5" />
        </svg>
      </li>
      <li
        class="bootcn-combobox__option"
        id="bootcn-combobox-stack-rails"
        role="option"
        data-value="rails"
        aria-selected="false"
      >
        <span>Ruby on Rails</span>
        <svg
          class="bootcn-icon bootcn-combobox__check"
          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="M20 6 9 17l-5-5" />
        </svg>
      </li>
      <li
        class="bootcn-combobox__option"
        id="bootcn-combobox-stack-symfony"
        role="option"
        data-value="symfony"
        aria-selected="false"
      >
        <span>Symfony</span>
        <svg
          class="bootcn-icon bootcn-combobox__check"
          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="M20 6 9 17l-5-5" />
        </svg>
      </li>
      <li
        class="bootcn-combobox__option"
        id="bootcn-combobox-stack-express"
        role="option"
        data-value="express"
        aria-selected="false"
      >
        <span>Express</span>
        <svg
          class="bootcn-icon bootcn-combobox__check"
          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="M20 6 9 17l-5-5" />
        </svg>
      </li>
      <li
        class="bootcn-combobox__option"
        id="bootcn-combobox-stack-flask"
        role="option"
        data-value="flask"
        aria-selected="false"
      >
        <span>Flask</span>
        <svg
          class="bootcn-icon bootcn-combobox__check"
          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="M20 6 9 17l-5-5" />
        </svg>
      </li>
      <li
        class="bootcn-combobox__option"
        id="bootcn-combobox-stack-wordpress"
        role="option"
        data-value="wordpress"
        aria-selected="false"
      >
        <span>WordPress</span>
        <svg
          class="bootcn-icon bootcn-combobox__check"
          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="M20 6 9 17l-5-5" />
        </svg>
      </li>
    </ul>
    <p class="bootcn-combobox__empty" hidden>No framework found.</p>
    <p class="visually-hidden" role="status" aria-live="polite" data-bootcn-combobox-status></p>
  </div>
</div>
css
.bootcn-combobox {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: 20rem;
}

.bootcn-combobox__control {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.25rem;
  min-height: var(--bootcn-control-height);
  padding: 0.1875rem 0.25rem 0.1875rem 0;
  border: var(--bootcn-border-width) solid var(--bootcn-input);
  border-radius: var(--bootcn-radius);
  background-color: transparent;
  box-shadow: var(--bootcn-shadow-sm);
  transition:
    border-color var(--bootcn-transition),
    box-shadow var(--bootcn-transition);
}

.bootcn-combobox__control:focus-within {
  border-color: var(--bootcn-ring);
  box-shadow: 0 0 0 3px var(--bs-focus-ring-color);
}

.bootcn-combobox__input {
  flex: 1 1 6rem;
  min-width: 6rem;
  padding: 0.25rem var(--bootcn-control-padding-x);
  border: 0;
  background: transparent;
  color: var(--bootcn-foreground);
  font-size: 1rem;
  outline: 0;
}

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

@media (min-width: 768px) {
  .bootcn-combobox__input {
    font-size: var(--bootcn-control-font-size);
  }
}

.bootcn-combobox__toggle {
  display: inline-flex;
  flex: none;
  align-items: center;
  padding: 0.25rem;
  border: 0;
  background: transparent;
  color: var(--bootcn-muted-foreground);
}

.bootcn-combobox__chips {
  display: contents;
}

.bootcn-combobox__chip {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  margin-left: 0.375rem;
  padding: 0.125rem 0.25rem 0.125rem 0.5rem;
  border-radius: var(--bootcn-radius-sm);
  background-color: var(--bootcn-secondary);
  color: var(--bootcn-secondary-foreground);
  font-size: 0.75rem;
  font-weight: 500;
}

.bootcn-combobox__chip + .bootcn-combobox__chip {
  margin-left: 0;
}

.bootcn-combobox__chip-remove {
  display: inline-flex;
  padding: 0.125rem;
  border: 0;
  border-radius: var(--bootcn-radius-sm);
  background: transparent;
  color: inherit;
}

.bootcn-combobox__chip-remove:hover {
  background-color: var(--bootcn-accent);
}

.bootcn-combobox__chip-remove:focus-visible {
  outline: 0;
  box-shadow: var(--bootcn-focus-ring);
}

.bootcn-combobox__chip-remove .bootcn-icon {
  width: 0.75rem;
  height: 0.75rem;
}

.bootcn-combobox__listbox,
.bootcn-combobox__empty {
  position: absolute;
  z-index: 1000;
  top: 100%;
  right: 0;
  left: 0;
  margin: 0.25rem 0 0;
  padding: 0.25rem;
  border: var(--bootcn-border-width) solid var(--bootcn-border-color);
  border-radius: var(--bootcn-radius);
  background-color: var(--bootcn-background);
  box-shadow: var(--bootcn-shadow-md);
  font-size: 0.875rem;
}

.bootcn-combobox__listbox {
  max-height: 15rem;
  overflow-y: auto;
  list-style: none;
}

.bootcn-combobox__empty {
  padding: 1.25rem 0.5rem;
  color: var(--bootcn-muted-foreground);
  text-align: center;
}

.bootcn-combobox__listbox[hidden],
.bootcn-combobox__empty[hidden],
.bootcn-combobox__option[hidden] {
  display: none;
}

.bootcn-combobox__option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.375rem 0.5rem;
  border-radius: var(--bootcn-radius-sm);
  cursor: pointer;
}

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

.bootcn-combobox__check {
  visibility: hidden;
}

.bootcn-combobox__option[aria-selected='true'] .bootcn-combobox__check {
  visibility: visible;
}
js
(function () {
  'use strict';

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

  const ROOT = '[data-bootcn-combobox]';
  const INIT = 'data-bootcn-combobox-initialized';
  const cleanups = new Map();

  // Case- and accent-insensitive matching ("e" finds "é").
  function fold(text) {
    return text.normalize('NFD').replace(/[̀-ͯ]/g, '').toLowerCase();
  }

  function setup(root) {
    const input = root.querySelector('[role="combobox"]');
    const listbox = root.querySelector('[role="listbox"]');
    if (!input || !listbox) return null;
    const toggle = root.querySelector('.bootcn-combobox__toggle');
    const empty = root.querySelector('.bootcn-combobox__empty');
    const status = root.querySelector('[data-bootcn-combobox-status]');
    const single = root.querySelector('[data-bootcn-combobox-value]');
    const chips = root.querySelector('.bootcn-combobox__chips');
    const multiple = root.hasAttribute('data-bootcn-combobox-multiple');
    const name = root.getAttribute('data-bootcn-combobox-name') || '';
    const options = Array.from(listbox.querySelectorAll('[role="option"]'));
    let active = null;

    const labelOf = (option) => option.textContent.trim();
    const visible = () => options.filter((option) => !option.hidden);
    const selected = () =>
      options.filter((option) => option.getAttribute('aria-selected') === 'true');
    const isOpen = () => !listbox.hidden || (empty && !empty.hidden);

    function announce(message) {
      if (status) status.textContent = message;
    }

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

    function filter() {
      const query = fold(input.value.trim());
      options.forEach((option) => {
        option.hidden = query !== '' && !fold(labelOf(option)).includes(query);
      });
      const count = visible().length;
      announce(count === 0 ? 'No results.' : count + (count === 1 ? ' result.' : ' results.'));
      return count;
    }

    function open() {
      const count = filter();
      listbox.hidden = count === 0;
      if (empty) empty.hidden = count !== 0;
      input.setAttribute('aria-expanded', count === 0 ? 'false' : 'true');
      if (!active || active.hidden) setActive(visible()[0] || null);
    }

    function close() {
      listbox.hidden = true;
      if (empty) empty.hidden = true;
      input.setAttribute('aria-expanded', 'false');
      setActive(null);
    }

    function syncValue() {
      if (multiple) {
        root.querySelectorAll('input[data-bootcn-combobox-value]').forEach((el) => el.remove());
        if (chips) chips.textContent = '';
        selected().forEach((option) => {
          const hidden = document.createElement('input');
          hidden.type = 'hidden';
          hidden.name = name + '[]';
          hidden.value = option.getAttribute('data-value') || labelOf(option);
          hidden.setAttribute('data-bootcn-combobox-value', '');
          root.appendChild(hidden);
          if (!chips) return;
          const chip = document.createElement('span');
          chip.className = 'bootcn-combobox__chip';
          chip.textContent = labelOf(option);
          const remove = document.createElement('button');
          remove.type = 'button';
          remove.className = 'bootcn-combobox__chip-remove';
          remove.setAttribute('aria-label', 'Remove ' + labelOf(option));
          remove.setAttribute('data-option', option.id);
          remove.innerHTML =
            '<svg class="bootcn-icon" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true" focusable="false"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg>';
          chip.appendChild(remove);
          chips.appendChild(chip);
        });
      } else {
        const current = selected()[0];
        if (single) single.value = current ? current.getAttribute('data-value') || '' : '';
        input.value = current ? labelOf(current) : '';
      }
    }

    function choose(option) {
      if (multiple) {
        const on = option.getAttribute('aria-selected') !== 'true';
        option.setAttribute('aria-selected', String(on));
        syncValue();
        announce(labelOf(option) + (on ? ' selected.' : ' removed.'));
        input.value = '';
        open();
        setActive(option);
      } else {
        options.forEach((other) => other.setAttribute('aria-selected', String(other === option)));
        syncValue();
        close();
      }
      root.dispatchEvent(
        new CustomEvent('bootcn:combobox:change', {
          bubbles: true,
          detail: {
            value: multiple
              ? selected().map((o) => o.getAttribute('data-value'))
              : option.getAttribute('data-value'),
          },
        }),
      );
    }

    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) {
      const expanded = input.getAttribute('aria-expanded') === 'true';
      switch (event.key) {
        case 'ArrowDown':
          event.preventDefault();
          if (!expanded) open();
          else if (!event.altKey) move(1);
          break;
        case 'ArrowUp':
          event.preventDefault();
          if (event.altKey) close();
          else if (!expanded) {
            open();
            const list = visible();
            setActive(list[list.length - 1] || null);
          } else move(-1);
          break;
        case 'Home':
        case 'End':
          if (!expanded) return;
          event.preventDefault();
          setActive(event.key === 'Home' ? visible()[0] : visible()[visible().length - 1]);
          break;
        case 'Enter':
          if (expanded && active) {
            event.preventDefault();
            choose(active);
          }
          break;
        case 'Escape':
          if (isOpen()) {
            event.preventDefault();
            close();
            if (!multiple) syncValue();
          } else if (input.value) {
            input.value = '';
          }
          break;
        case 'Backspace':
          if (multiple && input.value === '' && selected().length > 0) {
            const last = selected()[selected().length - 1];
            last.setAttribute('aria-selected', 'false');
            syncValue();
            announce(labelOf(last) + ' removed.');
          }
          break;
        default:
          break;
      }
    }

    function onInput() {
      open();
      setActive(visible()[0] || null);
    }

    function onFocusOut(event) {
      if (root.contains(event.relatedTarget)) return;
      close();
      if (multiple) input.value = '';
      else syncValue();
    }

    function onListboxDown(event) {
      // Keep focus in the input when choosing with the pointer.
      event.preventDefault();
      const option = event.target.closest('[role="option"]');
      if (option) choose(option);
    }

    function onToggle(event) {
      event.preventDefault();
      if (isOpen()) close();
      else open();
      input.focus();
    }

    function onChipClick(event) {
      const button = event.target.closest('.bootcn-combobox__chip-remove');
      if (!button) return;
      const option = document.getElementById(button.getAttribute('data-option'));
      if (!option) return;
      option.setAttribute('aria-selected', 'false');
      syncValue();
      announce(labelOf(option) + ' removed.');
      input.focus();
    }

    input.addEventListener('keydown', onKeydown);
    input.addEventListener('input', onInput);
    input.addEventListener('click', open);
    root.addEventListener('focusout', onFocusOut);
    listbox.addEventListener('mousedown', onListboxDown);
    if (toggle) toggle.addEventListener('mousedown', onToggle);
    if (chips) chips.addEventListener('click', onChipClick);
    syncValue();
    close();
    root.setAttribute(INIT, '');

    return function cleanup() {
      input.removeEventListener('keydown', onKeydown);
      input.removeEventListener('input', onInput);
      input.removeEventListener('click', open);
      root.removeEventListener('focusout', onFocusOut);
      listbox.removeEventListener('mousedown', onListboxDown);
      if (toggle) toggle.removeEventListener('mousedown', onToggle);
      if (chips) chips.removeEventListener('click', onChipClick);
      root.removeAttribute(INIT);
    };
  }

  function init(root = document) {
    root.querySelectorAll(ROOT + ':not([' + INIT + '])').forEach((element) => {
      const cleanup = setup(element);
      if (cleanup) cleanups.set(element, cleanup);
    });
  }

  function destroy(root = document) {
    cleanups.forEach((cleanup, element) => {
      if (root === document || root.contains(element)) {
        cleanup();
        cleanups.delete(element);
      }
    });
  }

  ns.combobox = { 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.