Tags
A field that turns what is typed into values, each a chip in the well.
On this page
A tags field turns what is typed into values, each a chip in the well. It is an input group with input-group-wrap and the tags class: the well, its states and the concentric chips are the input group's, and the field after the chips takes the next value. The tags behavior turns the text into chips and moves focus between them.
Tags
<div class="field max-w(420) mx-auto"> <label for="labels">Labels</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="design"> design<input type="hidden" name="labels" value="design"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove design"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </span> <span class="chip" data-value="research"> research<input type="hidden" name="labels" value="research"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove research"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </span> <span class="chip" data-value="accessibility"> accessibility<input type="hidden" name="labels" value="accessibility"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove accessibility"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </span> <input class="input-group-field" id="labels" placeholder="Add a label" autocomplete="off" aria-describedby="labels-hint"> </div> <p class="field-hint" id="labels-hint">Press Enter or type a comma after each label.</p></div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import XIcon from '@master/icons-react/native/16/soft/x'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="field max-w(420) mx-auto"> <label htmlFor="labels">Labels</label> <div className="tags input-group input-group-wrap"> <span className="chip" data-value="design"> design<input type="hidden" name="labels" value="design" /><button type="button" className="chip-remove" tabIndex={-1} aria-label="Remove design"> <XIcon className="icon" /> </button> </span> <span className="chip" data-value="research"> research<input type="hidden" name="labels" value="research" /><button type="button" className="chip-remove" tabIndex={-1} aria-label="Remove research"> <XIcon className="icon" /> </button> </span> <span className="chip" data-value="accessibility"> accessibility<input type="hidden" name="labels" value="accessibility" /><button type="button" className="chip-remove" tabIndex={-1} aria-label="Remove accessibility"> <XIcon className="icon" /> </button> </span> <input className="input-group-field" id="labels" placeholder="Add a label" autoComplete="off" aria-describedby="labels-hint" /> </div> <p className="field-hint" id="labels-hint">Press Enter or type a comma after each label.</p> </div> )}<script> import { attach } from '@master/ui/behaviors' import XIcon from '@master/icons-svelte/native/16/soft/x'</script><div class="field max-w(420) mx-auto" {@attach attach}> <label for="labels">Labels</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="design"> design<input type="hidden" name="labels" value="design"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove design"> <XIcon class="icon" /> </button> </span> <span class="chip" data-value="research"> research<input type="hidden" name="labels" value="research"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove research"> <XIcon class="icon" /> </button> </span> <span class="chip" data-value="accessibility"> accessibility<input type="hidden" name="labels" value="accessibility"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove accessibility"> <XIcon class="icon" /> </button> </span> <input class="input-group-field" id="labels" placeholder="Add a label" autocomplete="off" aria-describedby="labels-hint"> </div> <p class="field-hint" id="labels-hint">Press Enter or type a comma after each label.</p></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import XIcon from '@master/icons-vue/native/16/soft/x'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="field max-w(420) mx-auto"> <label for="labels">Labels</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="design"> design<input type="hidden" name="labels" value="design"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove design"> <XIcon class="icon" /> </button> </span> <span class="chip" data-value="research"> research<input type="hidden" name="labels" value="research"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove research"> <XIcon class="icon" /> </button> </span> <span class="chip" data-value="accessibility"> accessibility<input type="hidden" name="labels" value="accessibility"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove accessibility"> <XIcon class="icon" /> </button> </span> <input class="input-group-field" id="labels" placeholder="Add a label" autocomplete="off" aria-describedby="labels-hint"> </div> <p class="field-hint" id="labels-hint">Press Enter or type a comma after each label.</p> </div></template>#What it adds to the input group
- A value arrives. A new chip grows out of the place the caret stood in, a short scale from 80% with the quick duration, instead of appearing. With reduced motion it only fades in.
- Room for a word. The field keeps at least
--tags-field-min, 6rem by default, and takes a line of its own below that, so the caret is never squeezed into a sliver at the end of a line. - A value the keys are on wears the focus ring as a whole, since Backspace there removes all of it, and its cross takes the pressed tone. The ring is the chip's: a chip with a
chip-removedraws it wherever it stands. - A value that is refused marks the field
aria-invalid="true", which the input group draws as the danger edge until the text changes.
Without @starting-style a new value appears at once.
#The behavior
tags takes the root, the input group with the tags class, and returns the function that detaches it.
import { tags } from '@master/ui/behaviors/tags.js'const detach = tags(document.querySelector('.tags'), { max: 10 })- Adding. Enter, and a delimiter, a comma by default, turn the field's text into a value: a chip is written before the field and the field is emptied. Pasted text with delimiters, line breaks or tabs in it becomes as many values, and what follows the last delimiter stays in the field.
- Refusing. A value that is empty, already there, past
maxor refused byvalidateis not added. The field keeps the text, so nothing typed is lost, and saysaria-invalid="true"until it changes. - Removing. A chip's remove button removes it on a click, and focus returns to the field.
- The form. A chip carries a hidden input with its value when the chips already carry one with a
name, or whennameis given, so a form submits every value under that name. A form's reset puts back the values the field started with. - Events. The root dispatches
inputandchangeeach time a value is added or removed. The function it returns has avalue, the values in order; set it to replace them, which dispatches nothing.
| Option | Default | |
|---|---|---|
delimiters | [','] | Keys that add the value as Enter does; [] leaves only Enter |
max | none | How many values the field holds at most |
validate | none | A function that says whether a value may be added |
name | the chips' | The name of the hidden input a new chip carries |
removeLabel | Remove <value> | The accessible name of a chip's remove button, in the page's language |
draw | none | Takes over writing the chips, for a renderer that writes them from its own list |
A chip stands for its data-value, or for its text. Without the behavior the field is a text input and its chips are fixed: a form still submits the hidden inputs they carry.
#Suggestions
A <datalist> gives the field the browser's own suggestions with no script of its own: the reader picks one, then presses Enter to add it. When the values have to come from a list, use a combobox with several values instead.
Suggestions
<div class="field max-w(420) mx-auto"> <label for="recipients">Recipients</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="ada@example.com"> ada@example.com<input type="hidden" name="to" value="ada@example.com"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove ada@example.com"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </span> <input class="input-group-field" id="recipients" type="email" list="recipient-suggestions" placeholder="Add a recipient" autocomplete="off"> </div> <datalist id="recipient-suggestions"> <option value="grace@example.com"></option> <option value="linus@example.com"></option> <option value="margaret@example.com"></option> </datalist></div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import XIcon from '@master/icons-react/native/16/soft/x'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="field max-w(420) mx-auto"> <label htmlFor="recipients">Recipients</label> <div className="tags input-group input-group-wrap"> <span className="chip" data-value="ada@example.com"> ada@example.com<input type="hidden" name="to" value="ada@example.com" /><button type="button" className="chip-remove" tabIndex={-1} aria-label="Remove ada@example.com"> <XIcon className="icon" /> </button> </span> <input className="input-group-field" id="recipients" type="email" list="recipient-suggestions" placeholder="Add a recipient" autoComplete="off" /> </div> <datalist id="recipient-suggestions"> <option value="grace@example.com"></option> <option value="linus@example.com"></option> <option value="margaret@example.com"></option> </datalist> </div> )}<script> import { attach } from '@master/ui/behaviors' import XIcon from '@master/icons-svelte/native/16/soft/x'</script><div class="field max-w(420) mx-auto" {@attach attach}> <label for="recipients">Recipients</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="ada@example.com"> ada@example.com<input type="hidden" name="to" value="ada@example.com"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove ada@example.com"> <XIcon class="icon" /> </button> </span> <input class="input-group-field" id="recipients" type="email" list="recipient-suggestions" placeholder="Add a recipient" autocomplete="off"> </div> <datalist id="recipient-suggestions"> <option value="grace@example.com"></option> <option value="linus@example.com"></option> <option value="margaret@example.com"></option> </datalist></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import XIcon from '@master/icons-vue/native/16/soft/x'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="field max-w(420) mx-auto"> <label for="recipients">Recipients</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="ada@example.com"> ada@example.com<input type="hidden" name="to" value="ada@example.com"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove ada@example.com"> <XIcon class="icon" /> </button> </span> <input class="input-group-field" id="recipients" type="email" list="recipient-suggestions" placeholder="Add a recipient" autocomplete="off"> </div> <datalist id="recipient-suggestions"> <option value="grace@example.com"></option> <option value="linus@example.com"></option> <option value="margaret@example.com"></option> </datalist> </div></template>#Keyboard
The field is the one tab stop. The chips are reached from it, and their remove buttons are out of the tab order with tabindex="-1". While an input method composes, Enter belongs to it.
| Keys | Action |
|---|---|
| Enter, Comma | Turn the field's text into a value; Enter in an empty field is left to the form |
| Backspace | In an empty field, move to the last value; on a value, remove it and move to the one before |
| Delete | On a value, remove it and move to the one after |
| ArrowLeft, ArrowRight | From the start of the field, move to the last value; between values, move to the one before or after, and past the last back to the field. The other way around from right to left |
| Home, End | On a value, move to the first value, or back to the field |
| Escape | On a value, return to the field |
#Accessibility
- Label the field, with a
<label>oraria-label. Say how a value is added in a hint under it: a comma or Enter is not obvious. - Name each remove button with the value it removes, in the page's language:
removeLabeldoes that for the chips the behavior writes. - A value that is refused leaves the text in the field and marks it invalid; say why in the field's hint or error, as for any field.
#React, Svelte and Vue
The tags component writes the chips from the value and attaches the behavior, which turns what is typed into values. Copy it into the project with npx master-ui add tags; React, Svelte and Vue says how the components are built and used.
#Classes
| Class | Description |
|---|---|
tags | On an input group with input-group-wrap: the values are its chips and the field takes the next one, keeping room for a word before it wraps to a line of its own |
It is used with input-group, input-group-wrap, input-group-field, and a chip with a chip-remove for each value.
#Custom properties
| Property | Default | Description |
|---|---|---|
--tags-field-min | 6rem | The narrowest the field becomes before it wraps to a line of its own |
#Related
- InputText fields cut into the surface, with labels, hints, errors and groups.
- ChipA small pill that is a filter or a removable value.
- ComboboxA text field that opens a listbox of matches.