Tag input
Type a word, press Enter or a comma, and it becomes a chip. For skills, labels, recipients and filters. Add data-momo-tags to a normal text field: it stays in the form and sends the tags joined by commas.
Basic
Pasting a, b, c (or several lines) adds them all. Typing the same tag twice shakes the one already there.
Press Enter or a comma after each one.
<div class="form-field">
<label class="form-label" for="tg-skills">Skills</label>
<input class="form-control" id="tg-skills" name="skills" value="Design, CSS" placeholder="Add a skill" data-momo-tags>
<p class="form-hint">Press Enter or a comma after each one.</p>
</div>Keyboard
| Key | Does |
|---|---|
| Enter or , | Adds what you typed |
| Backspace in an empty field | Marks the last chip; press again to remove it |
| ← in an empty field | Moves onto the chips; ← → walk them, Delete removes one |
| ↓ ↑ | Walk the suggestions |
| Esc | Closes the suggestions |
Leaving the field keeps what you typed as a tag. Every add and remove is read out to screen readers.
Suggestions
Point list at a <datalist>. Matches show as you type (or press ↓), and you can still add your own. data-color on an option colors its chip.
<div class="form-field">
<label class="form-label" for="tg-labels">Labels</label>
<input class="form-control" id="tg-labels" name="labels" value="bug" placeholder="Add labels" list="tg-label-list" data-momo-tags>
<datalist id="tg-label-list">
<option value="bug" data-color="red">Bug</option>
<option value="feature" data-color="green">Feature</option>
<option value="design" data-color="violet">Design</option>
<option value="docs" data-color="cyan">Docs</option>
<option value="urgent" data-color="orange">Urgent</option>
<option value="question" data-color="yellow">Question</option>
</datalist>
</div>Only from the list
data-strict accepts suggestions only. data-max sets a limit: the field stops taking more once it is full.
Up to 3 people.
<div class="form-field">
<label class="form-label" for="tg-team">Assign to</label>
<input class="form-control" id="tg-team" name="team" placeholder="Pick up to 3 people" list="tg-people" data-strict data-max="3" data-color="yellow" data-momo-tags>
<datalist id="tg-people">
<option value="sallie">Sallie Wade</option>
<option value="eula">Eula Horton</option>
<option value="lucy">Lucy Fox</option>
<option value="ina">Ina Hogan</option>
<option value="victoria">Victoria Perez</option>
</datalist>
<p class="form-hint">Up to 3 people.</p>
</div>Sizes and states
.form-control-sm / -lg carry over. So do .is-invalid, .is-valid, disabled and required (needs at least one tag).
<div class="form-grid">
<input class="form-control form-control-sm" value="small, tags" aria-label="Small" data-momo-tags>
<input class="form-control is-invalid" value="too long a tag name here" aria-label="Invalid" data-momo-tags>
<input class="form-control" value="locked, tags" aria-label="Disabled" disabled data-momo-tags>
</div>Chips on their own
.tag is the chip by itself, for showing tags on cards and rows. .tag-<color> and .tag-ink color it, a .tag-remove button makes it removable (you handle the click), and .tag-list lines them up.
<ul class="tag-list" aria-label="Filters">
<li class="tag">Design</li>
<li class="tag tag-green"><i class="mi mi-check" aria-hidden="true"></i> Done</li>
<li class="tag tag-violet">Q4</li>
<li class="tag tag-ink">Owner: me</li>
<li class="tag tag-yellow"><span class="tag-text">Due this week</span><button class="tag-remove" type="button" aria-label="Remove Due this week"><i class="mi mi-x" aria-hidden="true"></i></button></li>
</ul>JavaScript
The real field fires input and change, plus momo:tags with detail.tags, detail.added and detail.removed. Form resets put the starting tags back.
const skills = document.querySelector('[name=skills]');
skills.addEventListener('momo:tags', (e) => console.log(e.detail.tags));
Momo.tags.get(skills); // ['Design', 'CSS']
Momo.tags.add(skills, 'Figma');
Momo.tags.remove(skills, 'CSS');
Momo.tags.clear(skills);
Use data-separator for a different separator (for example ;).
Variables
| Variable | Default |
|---|---|
--tag-bg | brand soft |
--tag-color | ink |
--field-border / --field-shadow | line / brand (focus ring) |