<u-tags>
DEPRECATED
Please use u-combobox instead
<u-tags> extends <input> with multiselect abilities. While multiselect combobox does not exist as a HTML element or ARIA pattern, <u-tags> adhere closely to HTML conventions while providing a thoroughly tested and accessible user experience.
Quick intro:
- Use
<data>as direct child elements - these are the removable tags - Use
<input>and optionally<u-datalist>to allow adding and suggesting tags - Use
data-*attributes to translate screen reader announcements - Use matching
idon<input>andforattribute on<label>to connect - MDN Web Docs: <data> (HTMLDataElement) / <input> (HTMLInputElement) / <datalist> (HTMLDatalistElement) / <option> (HTMLOptionElement)
Example
<label for="my-input">
Choose flavor of ice cream
</label>
<u-tags>
<data>Coconut</data>
<data>Banana</data>
<data>Pineapple</data>
<data>Orange</data>
<input id="my-input" list="my-list" />
<u-datalist id="my-list" data-sr-singular="%d flavor" data-sr-plural="%d flavours">
<u-option>Coconut</u-option>
<u-option>Strawberries</u-option>
<u-option>Chocolate</u-option>
<u-option>Vanilla</u-option>
<u-option>Licorice</u-option>
<u-option>Pistachios</u-option>
<u-option>Mango</u-option>
<u-option>Hazelnut</u-option>
</u-datalist>
</u-tags>
<style>
/* Styling just for example: */
u-tags { border: 1px solid; display: flex; flex-wrap: wrap; gap: .5em; padding: .5em; position: relative }
u-option[selected] { font-weight: bold }
u-datalist { position: absolute z-index: 9; inset: 100% -1px auto; border: 1px solid; background: white; padding: .5em }
</style>
Install
bash
npm add -S @u-elements/u-tagsbash
pnpm add -S @u-elements/u-tagsbash
yarn add @u-elements/u-tagsbash
bun add -S @u-elements/u-tagshtml
<script type="module" src="https://unpkg.com/@u-elements/u-tags@latest/dist/u-tags.js"></script>Attributes and props
<u-tags>
- Attributes: all global HTML attributes such as
id,class,data-data-sr-addedprefixes announcements about additions. Defaults to"Added"data-sr-removedprefixes announcements about removals. Defaults to"Removed"data-sr-removeannounces ability to remove. Defaults to"Press to remove"data-sr-emptyannounces no selected items. Defaults to"No selected"data-sr-foundannounces where to find amount of selected items. Defaults to"Navigate left to find %d selected"data-sr-ofseparates "number of total" in announcements. Defaults to"of"
- DOM interface:
UHTMLTagsElementextendsHTMLElementUHTMLTagsElement.controlreturnsHTMLInputElementcontained in<u-tags>UHTMLTagsElement.itemsreturnsNodeListOf<HTMLDataElement>
<data>
- Attributes: all global HTML attributes such as
id,class,data-valueoptionally specify the machine-readable translation of the text content.
- DOM interface:
HTMLDataElementHTMLDataElement.valuestring reflecting thevalueHTML attribute.
Events
In addition to the usual events supported by HTML elements, the <u-tags> elements dispatches a custom tags event before change, allowing you to keep track of and prevent additions and removal:
js
myUTags.addEventListener('tags', (event) => {
event.detail.action // String 'add' or 'remove'
event.detail.item // HTMLDataElement to add or remove
event.preventDefault() // Optionally prevent action
})Styling
<u-tags> renders as display: block, while <data> renders as display: inline-block with a ::after element to render the removal ×.
Example: Norwegian
<label for="my-norwegian-tags">
Velg type iskrem
</label>
<u-tags
data-sr-added="La til"
data-sr-remove="Trykk for å fjerne"
data-sr-removed="Fjernet"
data-sr-empty="Ingen valgte"
data-sr-found="Naviger til venstre for å finne %d valgte"
data-sr-of="av"
id="my-norwegian-tags"
>
<data>Kokkos</data>
<data>Banan</data>
<data>Ananas</data>
<data>Appelsin</data>
<input list="my-norwegian-list" />
<u-datalist id="my-norwegian-list" data-sr-singular="%d smak" data-sr-plural="%d smaker">
<u-option>Kokkos</u-option>
<u-option>Jordbær</u-option>
<u-option>Sjokolade</u-option>
<u-option>Vanilje</u-option>
<u-option>Lakris</u-option>
<u-option>Pistasj</u-option>
<u-option>Mango</u-option>
<u-option>Hasselnøtt</u-option>
</u-datalist>
</u-tags>
<style>
/* Styling just for example: */
u-tags { border: 1px solid; display: flex; flex-wrap: wrap; gap: .5em; padding: .5em; position: relative }
u-option[selected] { font-weight: bold }
u-datalist { position: absolute; z-index: 9; inset: 100% -1px auto; border: 1px solid; background: white; padding: .5em }
</style>
Accessibility
| Screen reader | <u-tags> |
|---|---|
| VoiceOver (Mac) + Chrome | ✅ |
| VoiceOver (Mac) + Edge | ✅ |
| VoiceOver (Mac) + Firefox | ✅ |
| VoiceOver (Mac) + Safari | ✅ |
| VoiceOver (iOS) + Safari | ✅ |
| Jaws (PC) + Chrome | ✅ |
| Jaws (PC) + Edge | ✅ |
| Jaws (PC) + Firefox | ✅ |
| NVDA (PC) + Chrome | ✅ |
| NVDA (PC) + Edge | ✅ |
| NVDA (PC) + Firefox | ✅ needs focus mode to announce tag removal |
| Narrator (PC) + Chrome | ✅ |
| Narrator (PC) + Edge | ✅ |
| Narrator (PC) + Firefox | ✅ |
| TalkBack (Android) + Chrome | ✅ |
| TalkBack (Android) + Firefox | ✅ |
| TalkBack (Android) + Samsung Internet | ✅ |
Specifications
- DOM interface: HTMLElement
- HTML Standard: The <div> element
- DOM interface: HTMLDataElement
- HTML Standard: The <data> element
Changelog
- 0.1.6: Fix focus issue hwen using keyboard navigation on
<data> - 0.1.5: Fix issue where
<data>value renderedundefined