Skip to content
Focsd homeFocsd homeFocsd
Forms

Tag input

A field that turns typed values into removable tags on Enter or a comma, with optional suggestions. To choose only from a list, use Multi-select.

  • Tag: focsd-tag-input
  • Pattern: Text input + list of removable tags (+ optional listbox)
  • Category: Forms

Example

German French Japanese
HTML
<div class="stack">
<focsd-tag-input
label="Skills"
value="Accessibility,CSS"
max="6"
placeholder="Add a skill"></focsd-tag-input>
<focsd-tag-input label="Languages">
<focsd-option value="de">German</focsd-option>
<focsd-option value="fr">French</focsd-option>
<focsd-option value="ja">Japanese</focsd-option>
</focsd-tag-input>
</div>

Sortable

Tags the user can put in order, for example skills by strength.

HTML
<focsd-tag-input
label="Skills, strongest first"
name="skills"
value="Accessibility,CSS,Lit"
sortable></focsd-tag-input>

Usage

Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.

<script type="module">
import '@focsd-dev/components/focsd-tag-input.js';
</script>
<focsd-tag-input label="Skills" value="Accessibility,CSS" max="6" placeholder="Add a skill"></focsd-tag-input>

API

Attributes

AttributePropertyTypeDefault
namenamestring—
disableddisabledbooleanfalse
requiredrequiredbooleanfalse
readonlyreadOnlybooleanfalse
aria-labelhostAriaLabelstring | nullnull
aria-descriptionhostAriaDescriptionstring | nullnull
aria-labelledbyhostAriaLabelledBystring | nullnull
aria-describedbyhostAriaDescribedBystring | nullnull
label-positionlabelPosition'top' | 'start' | 'end' | undefined—
hinthintstring—
errorerror——
warningwarningstring | undefined—
successsuccessstring | undefined—
error-messageerrorMessagestring | undefined—
warning-messagewarningMessagestring | undefined—
warning-patternwarningPatternstring | undefined—
labellabelstring—
valuevaluestring—
separatorseparatorstring,
maxmaxnumber0
placeholderplaceholderstring—
allow-duplicatesallowDuplicatesbooleanfalse
sortablesortablebooleanfalse

Methods

MethodDescription
describedByIds()IDs to add to the inner control's aria-describedby: the hint, then the shown message.
checkValidity()True when the value is valid; otherwise fires invalid and returns false, like native.
reportValidity()Like checkValidity(), and also shows the browser's validation message when invalid.
setCustomValidity(message)Set (non-empty) or clear (empty string) a custom validation error, like native controls.
setValidityState(flags, message, anchor)For subclasses: set the validity flags and message. anchor is the element the browser points its message at.
validate()Show (or clear) the error from the control's own checks: required, type, pattern, min, max and the like. Uses error-message, else the browser's localized message. An error the author set (a server error, say) is left as it is.
clearValidation()Clear the error and warning that validate() and warning-pattern showed (a form reset does).
emitValue(commit)For subclasses: fire the value events, as a native control does on a user edit: input (bubbles, composed), then with commit also change (bubbles, stays in its tree).
idrefText(ids)For subclasses: the text of the elements an ID reference list names, resolved in the host's tree (ID references cannot cross a shadow root), joined by spaces.
add(text)Add the tags in text (split on the separator and new lines). Returns true when any was added.
removeAt(index)Remove the tag at index.

Events

EventDescription
inputFired when a tag is added, removed or moved.
changeFired when a tag is added, removed or moved.

Slots

SlotDescription
(default)focsd-option suggestions (data only).

CSS parts

Style the inside of the component with ::part(), for example focsd-tag-input::part(label).

PartDescription
labelThe label.
layoutThe label and the body (see label position).
bodyThe box with its hint and message.
hintThe author hint.
messageThe shown error, warning or success message.
message-iconThe icon of the message.
fieldThe box holding the tags and the input.
tagsThe list of tags.
tagOne tag.
removeA tag's remove button.
handleA tag's reorder button (sortable only).
controlThe text input.
popupThe suggestions popup.
listboxThe suggestions list.
optionOne suggestion.
statusThe live status (visually hidden).
announcerThe visually hidden status region that announces a new message.

Accessibility

  • Tags are a list; each has a "Remove {tag}" button.
  • Adding, removing, duplicates and the maximum are announced in a polite status.
  • Pasting a list (commas or new lines) adds every item at once.
  • With focsd-option suggestions the input is an APG combobox with aria-activedescendant.
  • The form gets one entry per tag under name, so servers never split a string.

Keyboard

KeyAction
Enter, comma (the `separator`) or TabAdd the typed text as a tag
Shift+Tab (in the input)Move to the tags: the one last visited, or the last tag
Tab / Shift+Tab (on a tag)Move to the input / leave the component: the tags are one tab stop
Backspace (empty input)Move to the last tag
Backspace / Delete on a tagRemove it
Left / RightMove between tags and the input
Home / End (on a tag)First tag / back to the input
Down / UpMove through suggestions
EscapeClose the suggestions

Sortable

  • With sortable, each tag gets a "Reorder {tag}" button before its text. It shares the one tab stop of the remove buttons; the arrow keys move between all tag buttons.
  • Grabbing, each move, the drop and a cancel are announced in the tag input’s polite status.
  • A drop updates values (the form gets the tags in the new order) and fires input and change, after a cancelable focsd-reorder with detail.from, detail.to and detail.value.

Keyboard

KeyAction
Space / Enter (on a handle)Grab the tag, or drop it
Arrow keysMove the grabbed tag one place (Left and Right mirrored right to left)
EscapePut the tag back where it was