Skip to content

Tag group ​

A list of tags (chips) that can be removed, or a set of selectable tags built on native checkboxes.

Installation ​

bash
npx bootcn add tag-group
bash
pnpm dlx bootcn add tag-group
bash
yarn dlx bootcn add tag-group
bash
bunx --bun bootcn add tag-group

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

Usage ​

Use removable tags for values the user added (skills, filters, recipients) and selectable tags for choosing several options. To send removable tags with a form, keep a hidden input per tag and remove it with the tag.

JavaScript API ​

  • Removable tags: data-bootcn-tag-group on the wrapper, a remove button (.bootcn-tag__remove, named "Remove …") in each tag. Click, Backspace or Delete removes it, focus moves to the next tag, and the live region marked data-bootcn-tag-group-status announces it.
  • Event bootcn:tag-group:remove (bubbles), detail.value.
  • Selectable tags need no script: .btn-check checkboxes in a <fieldset>.
  • Bootcn.tagGroup.init() / destroy().

Accessibility ​

  • Removable tags are a list labelled by its heading; each remove button names its tag, and focus never gets lost after a removal.
  • Selectable tags are native checkboxes in a fieldset with a legend, so screen readers announce "checked" and the group name.

Dependencies ​

  • Bootstrap 5.3+ CSS.
  • themes/tokens/tokens.css.

Source ​

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

html
<div class="bootcn-tag-group" data-bootcn-tag-group>
  <p class="bootcn-tag-group__label" id="bootcn-tag-group-skills">Skills</p>
  <ul class="bootcn-tag-group__list" aria-labelledby="bootcn-tag-group-skills">
    <li class="bootcn-tag">
      <span>HTML</span>
      <button type="button" class="bootcn-tag__remove" aria-label="Remove HTML">
        <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="M18 6 6 18" />
          <path d="m6 6 12 12" />
        </svg>
      </button>
    </li>
    <li class="bootcn-tag">
      <span>CSS</span>
      <button type="button" class="bootcn-tag__remove" aria-label="Remove CSS">
        <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="M18 6 6 18" />
          <path d="m6 6 12 12" />
        </svg>
      </button>
    </li>
    <li class="bootcn-tag">
      <span>JavaScript</span>
      <button type="button" class="bootcn-tag__remove" aria-label="Remove JavaScript">
        <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="M18 6 6 18" />
          <path d="m6 6 12 12" />
        </svg>
      </button>
    </li>
    <li class="bootcn-tag">
      <span>PHP</span>
      <button type="button" class="bootcn-tag__remove" aria-label="Remove PHP">
        <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="M18 6 6 18" />
          <path d="m6 6 12 12" />
        </svg>
      </button>
    </li>
    <li class="bootcn-tag">
      <span>Bootstrap</span>
      <button type="button" class="bootcn-tag__remove" aria-label="Remove Bootstrap">
        <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="M18 6 6 18" />
          <path d="m6 6 12 12" />
        </svg>
      </button>
    </li>
  </ul>
  <p class="visually-hidden" role="status" data-bootcn-tag-group-status></p>
</div>

<fieldset class="bootcn-tag-group mt-4">
  <legend class="bootcn-tag-group__label">Interests</legend>
  <div class="bootcn-tag-group__list">
    <input
      type="checkbox"
      class="btn-check"
      id="bootcn-tag-group-design"
      name="interests[]"
      value="design"
      checked
    />
    <label class="bootcn-tag bootcn-tag--selectable" for="bootcn-tag-group-design">
      <svg
        class="bootcn-icon bootcn-tag__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>
      Design
    </label>
    <input
      type="checkbox"
      class="btn-check"
      id="bootcn-tag-group-frontend"
      name="interests[]"
      value="frontend"
      checked
    />
    <label class="bootcn-tag bootcn-tag--selectable" for="bootcn-tag-group-frontend">
      <svg
        class="bootcn-icon bootcn-tag__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>
      Front-end
    </label>
    <input
      type="checkbox"
      class="btn-check"
      id="bootcn-tag-group-backend"
      name="interests[]"
      value="backend"
    />
    <label class="bootcn-tag bootcn-tag--selectable" for="bootcn-tag-group-backend">
      <svg
        class="bootcn-icon bootcn-tag__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>
      Back-end
    </label>
    <input
      type="checkbox"
      class="btn-check"
      id="bootcn-tag-group-devops"
      name="interests[]"
      value="devops"
    />
    <label class="bootcn-tag bootcn-tag--selectable" for="bootcn-tag-group-devops">
      <svg
        class="bootcn-icon bootcn-tag__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>
      DevOps
    </label>
    <input
      type="checkbox"
      class="btn-check"
      id="bootcn-tag-group-writing"
      name="interests[]"
      value="writing"
    />
    <label class="bootcn-tag bootcn-tag--selectable" for="bootcn-tag-group-writing">
      <svg
        class="bootcn-icon bootcn-tag__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>
      Writing
    </label>
  </div>
</fieldset>
css
.bootcn-tag-group {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.bootcn-tag-group__label {
  float: none;
  width: auto;
  margin: 0 0 0.5rem;
  padding: 0;
  font-size: 0.875rem;
  font-weight: 500;
}

.bootcn-tag-group__list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.bootcn-tag {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  min-height: 1.75rem;
  padding: 0.125rem 0.25rem 0.125rem 0.625rem;
  border: var(--bootcn-border-width) solid var(--bootcn-border-color);
  border-radius: var(--bootcn-radius-pill);
  background-color: var(--bootcn-secondary);
  color: var(--bootcn-secondary-foreground);
  font-size: 0.8125rem;
  font-weight: 500;
}

.bootcn-tag__remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.25rem;
  height: 1.25rem;
  border: 0;
  border-radius: 9999px;
  background: transparent;
  color: var(--bootcn-muted-foreground);
}

.bootcn-tag__remove:hover {
  background-color: var(--bootcn-accent);
  color: var(--bootcn-foreground);
}

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

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

/* Selectable tags: a .btn-check checkbox before each label. */
.bootcn-tag--selectable {
  padding-right: 0.75rem;
  background-color: transparent;
  color: var(--bootcn-foreground);
  cursor: pointer;
  user-select: none;
}

.bootcn-tag--selectable:hover {
  background-color: var(--bootcn-accent);
}

.bootcn-tag__check {
  display: none;
  width: 0.875rem;
  height: 0.875rem;
}

.btn-check:checked + .bootcn-tag--selectable {
  border-color: var(--bootcn-primary);
  background-color: var(--bootcn-primary);
  color: var(--bootcn-primary-foreground);
}

.btn-check:checked + .bootcn-tag--selectable .bootcn-tag__check {
  display: inline-block;
}

.btn-check:focus-visible + .bootcn-tag--selectable {
  box-shadow: var(--bootcn-focus-ring);
}
js
(function () {
  'use strict';

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

  const ROOT = '[data-bootcn-tag-group]';
  let listening = false;

  // Removing a tag keeps focus in the group: on the next tag's button, else
  // the previous one, else the group's label.
  function remove(button) {
    const tag = button.closest('.bootcn-tag');
    const root = button.closest(ROOT);
    if (!tag || !root) return;
    const buttons = Array.from(root.querySelectorAll('.bootcn-tag__remove'));
    const index = buttons.indexOf(button);
    const name = tag.textContent.trim();
    tag.remove();
    const rest = Array.from(root.querySelectorAll('.bootcn-tag__remove'));
    const next = rest[index] || rest[index - 1];
    if (next) next.focus();
    else {
      const label = root.querySelector('.bootcn-tag-group__label');
      if (label) {
        label.setAttribute('tabindex', '-1');
        label.focus();
      }
    }
    const status = root.querySelector('[data-bootcn-tag-group-status]');
    if (status) status.textContent = name + ' removed.';
    root.dispatchEvent(
      new CustomEvent('bootcn:tag-group:remove', { bubbles: true, detail: { value: name } }),
    );
  }

  function onClick(event) {
    const button = event.target.closest && event.target.closest(ROOT + ' .bootcn-tag__remove');
    if (button) remove(button);
  }

  function onKeydown(event) {
    if (event.key !== 'Backspace' && event.key !== 'Delete') return;
    const button = event.target.closest && event.target.closest(ROOT + ' .bootcn-tag__remove');
    if (!button) return;
    event.preventDefault();
    remove(button);
  }

  function init() {
    if (listening) return;
    document.addEventListener('click', onClick);
    document.addEventListener('keydown', onKeydown);
    listening = true;
  }

  function destroy() {
    if (!listening) return;
    document.removeEventListener('click', onClick);
    document.removeEventListener('keydown', onKeydown);
    listening = false;
  }

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