Skip to content

Tag group

A labeled list of removable tags with one tab stop, arrow keys, Delete to remove and Undo.

<form class="cap-tags" data-cap="tag-group" role="group" aria-labelledby="tags-live-l" data-cap-then="#tags-add" id="tags-live" action="#" method="post">
  <span class="cap-tags-label" id="tags-live-l">Tags</span>
  <ul class="cap-tags-list">
    <li class="cap-tag" data-value="field-notes"><span class="cap-tag-name">field notes</span><button type="submit" name="remove-tag" value="field-notes" class="cap-tag-remove" aria-label="Remove field notes"><svg width="12" height="12" viewBox="0 0 16 16" aria-hidden="true" focusable="false"><path d="M4 4l8 8M12 4l-8 8" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" /></svg></button></li>
    <li class="cap-tag" data-value="monitoring"><span class="cap-tag-name">monitoring</span><button type="submit" name="remove-tag" value="monitoring" class="cap-tag-remove" aria-label="Remove monitoring"><svg width="12" height="12" viewBox="0 0 16 16" aria-hidden="true" focusable="false"><path d="M4 4l8 8M12 4l-8 8" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" /></svg></button></li>
    <li class="cap-tag" data-value="quiet-hours"><span class="cap-tag-name">quiet hours</span><button type="submit" name="remove-tag" value="quiet-hours" class="cap-tag-remove" aria-label="Remove quiet hours"><svg width="12" height="12" viewBox="0 0 16 16" aria-hidden="true" focusable="false"><path d="M4 4l8 8M12 4l-8 8" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" /></svg></button></li>
  </ul>
  <span class="cap-tags-empty">No tags yet.</span>
</form>
<div class="cap-field">
  <label class="cap-label" for="tags-add">Add a tag</label>
  <input class="cap-input" id="tags-add" type="text" autocomplete="off" />
</div>
<div class="cap-message" data-cap="message" role="status" aria-label="Results and failures" aria-atomic="false"></div>

Installation

// package.json: "capsomer": "github:DrDustinEdwards/capsomer#v0.152.2"
import "capsomer/tokens.css";
import "capsomer/base.css";
import "capsomer/tag-group.css";
import { enhance } from "capsomer/behavior/tag-group";

enhance();

Usage

<form class="cap-tags" data-cap="tag-group" role="group" aria-labelledby="tags-l" data-cap-then="#tags-add" method="post">
  <span class="cap-tags-label" id="tags-l">Tags</span>
  <ul class="cap-tags-list">
    <li class="cap-tag" data-value="monitoring">
      <span class="cap-tag-name">monitoring</span>
      <button type="submit" name="remove-tag" value="monitoring" class="cap-tag-remove" aria-label="Remove monitoring"><svg aria-hidden="true">...</svg></button>
    </li>
  </ul>
  <span class="cap-tags-empty">No tags yet.</span>
</form>

Examples

Hover on a remove button, and an empty group

<div class="cap-tags" role="group" aria-labelledby="tags-hover-l">
  <span class="cap-tags-label" id="tags-hover-l">Topics</span>
  <ul class="cap-tags-list">
    <li class="cap-tag" data-value="uptime"><span class="cap-tag-name">uptime</span><button type="button" class="cap-tag-remove" aria-label="Remove uptime"><svg width="12" height="12" viewBox="0 0 16 16" aria-hidden="true" focusable="false"><path d="M4 4l8 8M12 4l-8 8" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" /></svg></button></li>
    <li class="cap-tag" data-value="webmentions"><span class="cap-tag-name">webmentions</span><button type="button" class="cap-tag-remove" aria-label="Remove webmentions"><svg width="12" height="12" viewBox="0 0 16 16" aria-hidden="true" focusable="false"><path d="M4 4l8 8M12 4l-8 8" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" /></svg></button></li>
  </ul>
</div>
<div class="cap-tags" role="group" aria-labelledby="tags-empty-l" id="tags-empty">
  <span class="cap-tags-label" id="tags-empty-l">Labels</span>
  <ul class="cap-tags-list"></ul>
  <span class="cap-tags-empty">No labels yet.</span>
</div>

Comfortable density

<div data-density="comfortable">
  <div class="cap-tags" role="group" aria-labelledby="tags-comf-l">
    <span class="cap-tags-label" id="tags-comf-l">Sites</span>
    <ul class="cap-tags-list">
      <li class="cap-tag" data-value="example-com"><span class="cap-tag-name">example.com</span><button type="button" class="cap-tag-remove" aria-label="Remove example.com"><svg width="12" height="12" viewBox="0 0 16 16" aria-hidden="true" focusable="false"><path d="M4 4l8 8M12 4l-8 8" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" /></svg></button></li>
      <li class="cap-tag" data-value="fieldnotes"><span class="cap-tag-name">fieldnotes.example</span><button type="button" class="cap-tag-remove" aria-label="Remove fieldnotes.example"><svg width="12" height="12" viewBox="0 0 16 16" aria-hidden="true" focusable="false"><path d="M4 4l8 8M12 4l-8 8" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" /></svg></button></li>
    </ul>
  </div>
</div>

API reference

Functions

FunctionSignatureDoes
removeTag(group: HTMLElement, tag: HTMLElement): booleanRemoves one tag and moves focus on. Fires a cancelable, bubbling cap-tag-remove on the group first, with { value, label }: an app that refuses the removal calls preventDefault().
enhance(root?: ParentNode): () => voidAttaches to every [data-cap="tag-group"] under root not yet attached. Returns a function that detaches them all.

Parts and data attributes

ClassIs
.cap-tagits own class
.cap-tag-removeits own class
.cap-tagsthe root
.cap-tags-emptya part
.cap-tags-labela part
.cap-tags-lista part
StateRead from
:focus-visiblethe platform
:hoverthe platform

Accessibility

KeyDoes
TabEnters the group at one tag's remove button; the next Tab leaves
Right arrow, Left arrowNext or previous tag, wrapping (swapped right to left)
Home, EndThe first or the last tag
Delete, Backspace, Enter, SpaceRemoves the focused tag; focus moves to the next
zUndo, from the message region, while single-key shortcuts are on
  • The group is named by its label; the tags are a list, so a screen reader says how many there are.
  • Each remove button is named "Remove" and the tag's name; its icon is hidden.
  • The removal is said in the message region, politely, with Undo.
  • Each remove button is at least var(--target) square; 44px on a touch screen.