<u-tags>
<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
id
on<u-tags>
andfor
attribute on<label>
to connect - MDN Web Docs: <data> (HTMLDataElement) / <input> (HTMLInputElement) / <datalist> (HTMLDatalistElement) / <option> (HTMLOptionElement)
Example
<label for="my-tags"> Choose flavor of ice cream </label> <u-tags id="my-tags"> <data>Coconut</data> <data>Banana</data> <data>Pineapple</data> <data>Orange</data> <input list="my-list" /> <u-datalist id="my-list"> <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; inset: 100% -1px auto; border: 1px solid; background: white; padding: .5em } </style>
Install
bash
npm add -S @u-elements/u-tags
bash
pnpm add -S @u-elements/u-tags
bash
yarn add -S @u-elements/u-tags
bash
bun add -S @u-elements/u-tags
html
<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-
id
must be identical to value offor
attribute on associated<label>
data-added
prefixes announcements about additions. Defaults to"Added"
data-removed
prefixes announcements about removals. Defaults to"Removed"
data-empty
announces no selected items. Defaults to"No selected"
data-found
announces where to find amount of selected items. Defaults to"Navigate left to find %d selected"
data-of
separates "number of total" in announcements. Defaults to"of"
- DOM interface:
UHTMLTagsElement
extendsHTMLElement
UHTMLTagsElement.control
returnsHTMLInputElement
contained in<u-tags>
UHTMLTagsElement.labels
returnsNodeListOf<HTMLLabelElement>
UHTMLTagsElement.items
returnsNodeListOf<HTMLDataElement>
<data>
- Attributes: all global HTML attributes such as
id
,class
,data-
value
optionally specify the machine-readable translation of the text content.
- DOM interface:
HTMLDataElement
HTMLDataElement.value
string reflecting thevalue
HTML 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-added="La til" data-remove="Trykk for å fjerne" data-removed="Fjernet" data-empty="Ingen valgte" data-found="Naviger til venstre for å finne %d valgte" data-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"> <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; 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