React Tag Input: 5 Chip Inputs With Real Keyboard Support
Updated 2026-10-28. 5 inputs.
Build a React tag input as one wrapper that holds a list of chips and a bare text field. Keep the tags in an array in state, add on Enter or comma, remove the last chip when Backspace hits an empty field, and give every remove button an aria-label so the chips stay usable by keyboard.
Start from the basic chip field if people type free words, such as skills on a job post or labels on a note. Pick the autocomplete version when the words should come from a known list, because suggestions stop near-duplicates like design and designs from piling up. The coloured version suits labels that belong to groups, where colour tells people at a glance which group a tag sits in.
Use the counter version when a hard limit matters, for example five topics on a forum post, since it shows the remaining count and refuses a sixth tag with a message rather than silently. The recipients field is the right choice for email: it checks each address as it becomes a chip, marks bad ones in red and keeps them editable instead of throwing them away.
Every piece keeps its tags in one array at the top of the file, so wiring it to a form means reading that array on submit. In the plain HTML versions the same list lives in a hidden input as comma separated text, which a normal form post sends without any script on the server side.
Compare them
| Variant | Built with | Size (gzipped) | Running cost | Browser support |
|---|---|---|---|---|
| Tag Input That Adds on Enter or Comma and Removes With Backspace | CSS and a small script | 1.7 KB | One keydown handler and a short list re-render per added or removed tag. | All current browsers |
| Email Recipient Tag Input That Validates Each Address | CSS and a small script | 2.2 KB | One regular expression test per address on each render of the recipient row. | All current browsers |
| Tag Input With Arrow Key Autocomplete Suggestion List | CSS and a small script | 2.1 KB | Filters an array of fifteen topics on each key, well under a millisecond. | All current browsers |
| Tag Input With Coloured Category Chips | CSS and a small script | 1.9 KB | A plain form submit adds one object to a small array and redraws the chips. | All current browsers |
| Tag Input With Max Tag Counter and Duplicate Warning | CSS and a small script | 2.1 KB | One width transition on a thin meter and a re-render of five chips at most. | All current browsers |
Tag Input That Adds on Enter or Comma and Removes With Backspace
A skills field from a job post form where each word becomes an indigo chip. Enter or a comma turns the typed text into a tag, Backspace in an empty field takes the last one off, and a pasted list like figma, sketch, framer splits into three chips at once. The chips wrap onto new lines on a phone and the whole box stays one tap target.
<section class="tiec">
<div class="tiec-card">
<h2 class="tiec-title">New job post</h2>
<p class="tiec-sub">Junior product designer, Halden Studio</p>
<label for="tiec-field" class="tiec-label">Skills for this role</label>
<div class="tiec-box">
<ul class="tiec-list" aria-label="Added skills"></ul>
<input id="tiec-field" class="tiec-input" aria-describedby="tiec-hint" placeholder="Add another" autocomplete="off">
</div>
<p id="tiec-hint" class="tiec-hint">Press Enter or a comma to add. Backspace in an empty field removes the last skill.</p>
<p class="tiec-sr" aria-live="polite"></p>
<input type="hidden" name="skills" value="product design,figma,remote">
</div>
</section>
<style>
.tiec { --tiec-accent: #4f46e5; --tiec-chip: #eef2ff; --tiec-chip-ink: #312e81; background: #f5f5f4; padding: 72px 16px; font-family: ui-sans-serif, system-ui, sans-serif; color: #1c1917; }
.tiec-card { max-width: 36rem; margin: 0 auto; background: #fff; border: 1px solid #e7e5e4; border-radius: 16px; padding: 28px; box-shadow: 0 1px 2px rgba(0,0,0,.05); }
.tiec-title { margin: 0; font-size: 18px; font-weight: 600; letter-spacing: -.01em; }
.tiec-sub { margin: 4px 0 0; font-size: 14px; color: #57534e; }
.tiec-label { display: block; margin-top: 24px; font-size: 14px; font-weight: 500; }
.tiec-box { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; min-height: 52px; margin-top: 8px; padding: 8px 10px; border: 1px solid #d6d3d1; border-radius: 12px; cursor: text; }
.tiec-box:focus-within { border-color: var(--tiec-accent); box-shadow: 0 0 0 4px #e0e7ff; }
.tiec-list { display: contents; list-style: none; }
.tiec-chip { display: flex; align-items: center; gap: 4px; height: 32px; padding: 0 4px 0 12px; border-radius: 8px; background: var(--tiec-chip); color: var(--tiec-chip-ink); font-size: 14px; font-weight: 500; }
.tiec-x { display: grid; place-items: center; width: 24px; height: 24px; border: 0; border-radius: 6px; background: none; color: #4338ca; cursor: pointer; }
.tiec-x:hover { background: #e0e7ff; }
.tiec-x:focus-visible { outline: 2px solid var(--tiec-accent); }
.tiec-input { flex: 1; min-width: 8rem; height: 32px; border: 0; outline: 0; padding: 0 4px; font: inherit; font-size: 14px; background: transparent; }
.tiec-hint { margin: 8px 0 0; font-size: 12px; color: #57534e; }
.tiec-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
</style>
<script>
(function () {
var root = document.querySelector(".tiec");
if (!root) return;
var list = root.querySelector(".tiec-list"), input = root.querySelector(".tiec-input");
var hidden = root.querySelector("input[type=hidden]"), live = root.querySelector(".tiec-sr");
var tags = hidden.value ? hidden.value.split(",") : [];
function render() {
while (list.firstChild) list.removeChild(list.firstChild);
tags.forEach(function (t) {
var li = document.createElement("li"), b = document.createElement("button");
li.className = "tiec-chip";
li.appendChild(document.createTextNode(t));
b.type = "button"; b.className = "tiec-x"; b.textContent = "×";
b.setAttribute("aria-label", "Remove " + t);
b.addEventListener("click", function () { remove(t); });
li.appendChild(b);
list.appendChild(li);
});
hidden.value = tags.join(",");
}
function addAll(raw) {
raw.split(/[,\n]/).forEach(function (p) {
var t = p.trim().toLowerCase();
if (!t) return;
if (tags.indexOf(t) > -1) { live.textContent = t + " is already in the list"; return; }
tags.push(t); live.textContent = "Added " + t;
});
input.value = ""; render();
}
function remove(t) {
tags = tags.filter(function (x) { return x !== t; });
live.textContent = "Removed " + t; render(); input.focus();
}
input.addEventListener("keydown", function (e) {
if (e.key === "Enter" || e.key === ",") { e.preventDefault(); addAll(input.value); }
else if (e.key === "Backspace" && input.value === "" && tags.length) remove(tags[tags.length - 1]);
});
input.addEventListener("input", function () { if (input.value.indexOf(",") > -1) addAll(input.value); });
input.addEventListener("blur", function () { if (input.value.trim()) addAll(input.value); });
root.querySelector(".tiec-box").addEventListener("click", function () { input.focus(); });
render();
})();
</script>
With reduced motion: Nothing moves here; only the focus ring and chip colours change state.
Running cost: One keydown handler and a short list re-render per added or removed tag.
- Edit `START` for the tags that show on load and `LABEL` for the field name; the plain version reads its starting tags from the hidden input value.
- Change the chip colours through `bg-indigo-50` and `text-indigo-900`, or `--tiec-chip` and `--tiec-chip-ink` in the plain CSS.
- Remove `.toLowerCase()` in `addAll` if tags should keep the capitals people type, such as brand names.
- Add Tab to the keys that create a tag in `onKey` if your form has nothing else to tab to after this field.
Full page for Tag Input That Adds on Enter or Comma and Removes With Backspace
Email Recipient Tag Input That Validates Each Address
A share dialog for an interiors studio where every typed or pasted address becomes a chip. Good addresses get an initial avatar; ones that fail the check turn rose with an alert mark, and clicking one puts it back in the field to fix. Space, comma, semicolon, Tab and Enter all finish an address, and Send stays disabled until every chip is valid.
<section class="eret">
<div class="eret-card">
<div class="eret-top">
<h2 class="eret-title">Share “Q3 site photos”</h2>
<span class="eret-org">Larkhill Interiors</span>
</div>
<div class="eret-row">
<label for="eret-field" class="eret-to">To</label>
<div class="eret-box">
<span class="eret-chips"></span>
<input id="eret-field" class="eret-input" type="email" multiple aria-describedby="eret-msg" placeholder="Add people by email" autocomplete="off">
</div>
</div>
<div class="eret-foot">
<p id="eret-msg" class="eret-msg"></p>
<button type="button" class="eret-send">Send invites</button>
</div>
<p class="eret-sr" aria-live="polite"></p>
</div>
</section>
<style>
.eret { background: #f1f5f9; padding: 64px 16px; font-family: ui-sans-serif, system-ui, sans-serif; color: #0f172a; }
.eret-card { max-width: 42rem; margin: 0 auto; background: #fff; border: 1px solid #e2e8f0; border-radius: 12px; overflow: hidden; }
.eret-top { display: flex; justify-content: space-between; align-items: center; padding: 12px 20px; border-bottom: 1px solid #e2e8f0; }
.eret-title { margin: 0; font-size: 14px; font-weight: 600; }
.eret-org { font-size: 12px; color: #475569; }
.eret-row { display: flex; gap: 12px; align-items: flex-start; padding: 12px 20px; border-bottom: 1px solid #e2e8f0; }
.eret-to { width: 32px; margin-top: 8px; font-size: 14px; color: #475569; }
.eret-box { flex: 1; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; min-height: 40px; cursor: text; min-width: 0; }
.eret-chips { display: contents; }
.eret-chip { display: flex; align-items: center; max-width: 100%; height: 32px; padding-left: 10px; border-radius: 999px; background: #f1f5f9; color: #1e293b; font-size: 14px; }
.eret-chip[data-bad] { background: #fff1f2; color: #9f1239; box-shadow: inset 0 0 0 1px #fda4af; }
.eret-fix { height: 32px; padding: 0; border: 0; background: none; color: inherit; font: inherit; cursor: pointer; text-decoration: underline dotted; text-underline-offset: 3px; }
.eret-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.eret-x { width: 24px; height: 24px; margin: 0 4px 0 2px; border: 0; border-radius: 50%; background: none; color: inherit; cursor: pointer; font-size: 16px; flex-shrink: 0; }
.eret-x:focus-visible, .eret-fix:focus-visible, .eret-send:focus-visible { outline: 2px solid #0369a1; outline-offset: 2px; }
.eret-input { flex: 1; min-width: 10rem; height: 32px; border: 0; outline: 0; font: inherit; font-size: 14px; }
.eret-foot { display: flex; flex-wrap: wrap; gap: 12px; justify-content: space-between; align-items: center; padding: 16px 20px; }
.eret-msg { margin: 0; font-size: 14px; color: #475569; }
.eret-card[data-bad] .eret-msg { color: #be123c; }
.eret-send { height: 40px; padding: 0 20px; border: 0; border-radius: 8px; background: #0369a1; color: #fff; font: inherit; font-size: 14px; font-weight: 500; cursor: pointer; }
.eret-send:disabled { background: #cbd5e1; color: #334155; cursor: not-allowed; }
.eret-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
</style>
<script>
(function () {
var root = document.querySelector(".eret");
if (!root) return;
var VALID = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
var list = ["[email protected]", "[email protected]", "j.okafor@gmail"];
var card = root.querySelector(".eret-card"), chips = root.querySelector(".eret-chips"), input = root.querySelector(".eret-input");
var msg = root.querySelector(".eret-msg"), send = root.querySelector(".eret-send"), live = root.querySelector(".eret-sr");
function render() {
while (chips.firstChild) chips.removeChild(chips.firstChild);
list.forEach(function (a) {
var ok = VALID.test(a), s = document.createElement("span"), x = document.createElement("button");
s.className = "eret-chip";
if (ok) { var l = document.createElement("span"); l.className = "eret-label"; l.textContent = a; s.appendChild(l); }
else {
s.setAttribute("data-bad", "");
var f = document.createElement("button");
f.type = "button"; f.className = "eret-fix eret-label"; f.textContent = a;
f.setAttribute("aria-label", "Fix " + a + ", not a valid address");
f.addEventListener("click", function () { list = list.filter(function (y) { return y !== a; }); input.value = a; render(); input.focus(); });
s.appendChild(f);
}
x.type = "button"; x.className = "eret-x"; x.textContent = "×"; x.setAttribute("aria-label", "Remove " + a);
x.addEventListener("click", function () { list = list.filter(function (y) { return y !== a; }); live.textContent = "Removed " + a; render(); input.focus(); });
s.appendChild(x); chips.appendChild(s);
});
var bad = list.filter(function (a) { return !VALID.test(a); }).length;
card.toggleAttribute("data-bad", bad > 0);
msg.textContent = bad ? bad + (bad === 1 ? " address needs" : " addresses need") + " fixing. Click it to edit." : list.length + " people will get a view link.";
send.disabled = bad > 0 || !list.length;
}
function addAll(raw) {
var parts = raw.split(/[\s,;]+/).filter(Boolean);
parts.forEach(function (p) { if (list.indexOf(p) < 0) list.push(p); });
input.value = "";
var wrong = parts.filter(function (p) { return !VALID.test(p); });
live.textContent = wrong.length ? wrong.join(", ") + " does not look like an email address" : "Added " + parts.join(", ");
render();
}
input.addEventListener("keydown", function (e) {
if (["Enter", ",", ";", " ", "Tab"].indexOf(e.key) > -1 && input.value.trim()) { e.preventDefault(); addAll(input.value); }
else if (e.key === "Backspace" && input.value === "" && list.length) { live.textContent = "Removed " + list.pop(); render(); }
});
input.addEventListener("paste", function (e) { e.preventDefault(); addAll(input.value + " " + e.clipboardData.getData("text")); });
input.addEventListener("blur", function () { if (input.value.trim()) addAll(input.value); });
root.querySelector(".eret-box").addEventListener("click", function (e) { if (e.target === e.currentTarget) input.focus(); });
render();
})();
</script>
With reduced motion: No animation runs; invalid chips simply turn rose with an alert mark.
Running cost: One regular expression test per address on each render of the recipient row.
- Tighten or relax the `VALID` pattern; the loose default only checks for a name, an at sign and a domain with a dot.
- Change the separator keys in the array inside `onKey` if your people paste lists split by line breaks only.
- Swap the initial avatar for a real profile picture by looking each address up in your contacts list.
Tag Input With Arrow Key Autocomplete Suggestion List
A topic picker on a dark reading app. Typing re opens a list of matching topics with the typed letters picked out in teal; Up and Down move the highlight, Enter adds it as a chip, Escape closes the list. Words that are not in the list can still be added with Enter, and chosen topics drop out of the suggestions.
<section class="tiac">
<div class="tiac-wrap">
<h2 class="tiac-title">Follow topics</h2>
<p class="tiac-sub">Pick what shows up in your weekly reading list.</p>
<label for="tiac-field" class="tiac-label">Topics</label>
<div class="tiac-rel">
<div class="tiac-box">
<span class="tiac-chips"></span>
<input id="tiac-field" class="tiac-input" role="combobox" aria-expanded="false" aria-controls="tiac-list" aria-autocomplete="list" autocomplete="off" placeholder="Search topics">
</div>
<ul id="tiac-list" class="tiac-list" role="listbox" aria-label="Suggested topics" hidden></ul>
</div>
<p class="tiac-hint">Arrow keys move through suggestions. Escape closes the list.</p>
<p class="tiac-sr" aria-live="polite"></p>
</div>
</section>
<style>
.tiac { --tiac-accent: #2dd4bf; background: #020617; padding: 64px 16px 200px; font-family: ui-sans-serif, system-ui, sans-serif; color: #fff; }
.tiac-wrap { max-width: 32rem; margin: 0 auto; }
.tiac-title { margin: 0; font-size: 18px; font-weight: 600; }
.tiac-sub { margin: 4px 0 0; font-size: 14px; color: #cbd5e1; }
.tiac-label { display: block; margin-top: 24px; font-size: 14px; font-weight: 500; color: #e2e8f0; }
.tiac-rel { position: relative; margin-top: 8px; }
.tiac-box { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; min-height: 52px; padding: 8px 10px; border: 1px solid #334155; border-radius: 12px; background: #0f172a; }
.tiac-box:focus-within { border-color: var(--tiac-accent); box-shadow: 0 0 0 2px rgba(45,212,191,.3); }
.tiac-chips { display: contents; }
.tiac-chip { display: flex; align-items: center; gap: 4px; height: 32px; padding: 0 4px 0 12px; border: 1px solid rgba(45,212,191,.4); border-radius: 999px; background: rgba(45,212,191,.1); color: #ccfbf1; font-size: 14px; }
.tiac-x { width: 24px; height: 24px; border: 0; border-radius: 999px; background: none; color: #99f6e4; cursor: pointer; font-size: 16px; }
.tiac-x:focus-visible { outline: 2px solid var(--tiac-accent); }
.tiac-input { flex: 1; min-width: 7rem; height: 32px; border: 0; outline: 0; background: transparent; color: #fff; font: inherit; font-size: 14px; }
.tiac-input::placeholder { color: #94a3b8; }
.tiac-list { position: absolute; left: 0; right: 0; top: 100%; margin: 8px 0 0; padding: 4px 0; list-style: none; border: 1px solid #334155; border-radius: 12px; background: #0f172a; box-shadow: 0 20px 40px rgba(0,0,0,.4); }
.tiac-list li { display: flex; align-items: center; height: 40px; padding: 0 16px; font-size: 14px; color: #cbd5e1; cursor: pointer; }
.tiac-list li[aria-selected="true"] { background: rgba(45,212,191,.15); color: #fff; }
.tiac-list b { color: var(--tiac-accent); }
.tiac-hint { margin: 8px 0 0; font-size: 12px; color: #94a3b8; }
.tiac-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
</style>
<script>
(function () {
var root = document.querySelector(".tiac");
if (!root) return;
var OPTIONS = ["react", "react native", "redux", "remix", "rust", "ruby", "rails", "svelte", "solid", "swift", "typescript", "tailwind", "testing", "vite", "vue"];
var tags = ["typescript"], active = 0, matches = [];
var input = root.querySelector(".tiac-input"), list = root.querySelector(".tiac-list");
var chips = root.querySelector(".tiac-chips"), live = root.querySelector(".tiac-sr");
function clear(el) { while (el.firstChild) el.removeChild(el.firstChild); }
function renderChips() {
clear(chips);
tags.forEach(function (t) {
var s = document.createElement("span"), b = document.createElement("button");
s.className = "tiac-chip"; s.appendChild(document.createTextNode(t));
b.type = "button"; b.className = "tiac-x"; b.textContent = "×";
b.setAttribute("aria-label", "Remove " + t);
b.addEventListener("click", function () { remove(t); });
s.appendChild(b); chips.appendChild(s);
});
}
function renderList() {
var q = input.value.trim().toLowerCase();
matches = q ? OPTIONS.filter(function (o) { return o.indexOf(q) === 0 && tags.indexOf(o) < 0; }).slice(0, 6) : [];
clear(list);
matches.forEach(function (m, i) {
var li = document.createElement("li"), b = document.createElement("b");
li.id = "tiac-opt-" + i; li.setAttribute("role", "option");
li.setAttribute("aria-selected", i === active ? "true" : "false");
b.textContent = m.slice(0, q.length);
li.appendChild(b); li.appendChild(document.createTextNode(m.slice(q.length)));
li.addEventListener("mousedown", function (e) { e.preventDefault(); add(m); });
list.appendChild(li);
});
var open = matches.length > 0;
list.hidden = !open;
input.setAttribute("aria-expanded", open);
if (open) input.setAttribute("aria-activedescendant", "tiac-opt-" + active); else input.removeAttribute("aria-activedescendant");
}
function add(t) {
var v = t.trim().toLowerCase();
if (!v || tags.indexOf(v) > -1) return;
tags.push(v); input.value = ""; active = 0;
live.textContent = "Added " + v; renderChips(); renderList(); input.focus();
}
function remove(t) {
tags = tags.filter(function (x) { return x !== t; });
live.textContent = "Removed " + t; renderChips(); renderList(); input.focus();
}
input.addEventListener("input", function () { active = 0; renderList(); });
input.addEventListener("keydown", function (e) {
if (e.key === "ArrowDown" && matches.length) { e.preventDefault(); active = (active + 1) % matches.length; renderList(); }
else if (e.key === "ArrowUp" && matches.length) { e.preventDefault(); active = (active - 1 + matches.length) % matches.length; renderList(); }
else if (e.key === "Enter") { e.preventDefault(); add(matches.length ? matches[active] : input.value); }
else if (e.key === "Escape") { list.hidden = true; input.setAttribute("aria-expanded", "false"); }
else if (e.key === "Backspace" && input.value === "" && tags.length) remove(tags[tags.length - 1]);
});
input.value = "re";
renderChips(); renderList();
})();
</script>
With reduced motion: There is no motion; the highlighted suggestion simply changes colour.
Running cost: Filters an array of fifteen topics on each key, well under a millisecond.
- Replace the `OPTIONS` array with your own vocabulary, or load it from your API before the first render.
- Change `startsWith` to `includes` in the filter to match letters anywhere in a topic instead of at the start.
- Raise `.slice(0, 6)` to show more suggestions at once on tall screens.
Tag Input With Coloured Category Chips
Labels on a studio task card, sorted into three groups that each own a colour: amber for the client, sky for the stage of work and violet for who owns it. A small select picks the group before you type, Enter adds the label, and each chip keeps a coloured dot so the grouping still reads for people who see colour weakly.
<section class="ticc">
<div class="ticc-card">
<p class="ticc-kicker">Task 214</p>
<h2 class="ticc-title">Photograph spring pastry menu</h2>
<ul class="ticc-list" aria-label="Labels"></ul>
<form class="ticc-form">
<label for="ticc-group" class="ticc-sr">Label group</label>
<select id="ticc-group" class="ticc-select">
<option value="client">Client</option>
<option value="stage" selected>Stage</option>
<option value="owner">Owner</option>
</select>
<label for="ticc-field" class="ticc-sr">New label</label>
<input id="ticc-field" class="ticc-input" placeholder="Type a label and press Enter" autocomplete="off">
<button type="submit" class="ticc-btn">Add label</button>
</form>
<p class="ticc-note" aria-live="polite"></p>
</div>
</section>
<style>
.ticc { background: #fffbeb; padding: 64px 16px; font-family: ui-sans-serif, system-ui, sans-serif; color: #1c1917; }
.ticc-card { max-width: 36rem; margin: 0 auto; background: #fff; border-radius: 24px; padding: 28px; box-shadow: 0 1px 0 rgba(0,0,0,.04), 0 12px 32px -12px rgba(120,53,15,.18); }
.ticc-kicker { margin: 0; font-size: 12px; font-weight: 600; letter-spacing: .12em; text-transform: uppercase; color: #92400e; }
.ticc-title { margin: 4px 0 0; font-size: 18px; font-weight: 600; }
.ticc-list { display: flex; flex-wrap: wrap; gap: 8px; margin: 20px 0 0; padding: 0; list-style: none; }
.ticc-chip { display: flex; align-items: center; gap: 8px; height: 32px; padding: 0 4px 0 12px; border-radius: 999px; font-size: 14px; font-weight: 500; box-shadow: inset 0 0 0 1px var(--ring); background: var(--bg); color: var(--ink); }
.ticc-chip[data-group="client"] { --bg: #fef3c7; --ink: #78350f; --ring: #fcd34d; --dot: #f59e0b; }
.ticc-chip[data-group="stage"] { --bg: #e0f2fe; --ink: #0c4a6e; --ring: #7dd3fc; --dot: #0ea5e9; }
.ticc-chip[data-group="owner"] { --bg: #ede9fe; --ink: #4c1d95; --ring: #c4b5fd; --dot: #8b5cf6; }
.ticc-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--dot); }
.ticc-x { width: 24px; height: 24px; border: 0; border-radius: 50%; background: none; color: inherit; cursor: pointer; font-size: 16px; }
.ticc-x:focus-visible, .ticc-btn:focus-visible, .ticc-select:focus-visible { outline: 2px solid #d97706; outline-offset: 2px; }
.ticc-form { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 20px; }
.ticc-select, .ticc-input { height: 44px; border: 1px solid #d6d3d1; border-radius: 12px; padding: 0 12px; font: inherit; font-size: 14px; background: #fff; color: #1c1917; }
.ticc-input { flex: 1; min-width: 12rem; }
.ticc-input:focus { outline: 0; border-color: #d97706; box-shadow: 0 0 0 3px #fde68a; }
.ticc-btn { height: 44px; padding: 0 16px; border: 0; border-radius: 12px; background: #1c1917; color: #fff; font: inherit; font-size: 14px; font-weight: 500; cursor: pointer; }
.ticc-note { min-height: 20px; margin: 8px 0 0; font-size: 12px; color: #57534e; }
.ticc-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
</style>
<script>
(function () {
var root = document.querySelector(".ticc");
if (!root) return;
var NAMES = { client: "Client", stage: "Stage", owner: "Owner" };
var tags = [{ text: "Marlow Bakery", group: "client" }, { text: "In review", group: "stage" }, { text: "Priya", group: "owner" }];
var list = root.querySelector(".ticc-list"), input = root.querySelector(".ticc-input");
var select = root.querySelector(".ticc-select"), note = root.querySelector(".ticc-note");
function render() {
while (list.firstChild) list.removeChild(list.firstChild);
tags.forEach(function (t) {
var li = document.createElement("li"), dot = document.createElement("span"), sr = document.createElement("span"), b = document.createElement("button");
li.className = "ticc-chip"; li.setAttribute("data-group", t.group);
dot.className = "ticc-dot"; dot.setAttribute("aria-hidden", "true");
sr.className = "ticc-sr"; sr.textContent = NAMES[t.group] + ": ";
b.type = "button"; b.className = "ticc-x"; b.textContent = "×"; b.setAttribute("aria-label", "Remove " + t.text);
b.addEventListener("click", function () {
tags = tags.filter(function (x) { return x !== t; });
note.textContent = "Removed " + t.text; render(); input.focus();
});
li.appendChild(dot); li.appendChild(sr); li.appendChild(document.createTextNode(t.text)); li.appendChild(b);
list.appendChild(li);
});
}
root.querySelector(".ticc-form").addEventListener("submit", function (e) {
e.preventDefault();
var t = input.value.trim();
if (!t) return;
if (tags.some(function (x) { return x.text.toLowerCase() === t.toLowerCase(); })) { note.textContent = t + " is already a label"; return; }
tags.push({ text: t, group: select.value });
input.value = ""; note.textContent = "Added " + t + " to " + NAMES[select.value]; render();
});
render();
})();
</script>
With reduced motion: Nothing animates; labels appear and disappear without any transition.
Running cost: A plain form submit adds one object to a small array and redraws the chips.
- Add or rename groups in `GROUPS`; each entry needs a label, a chip class string and a dot class.
- In the plain version, add a matching `.ticc-chip[data-group=...]` rule with its four colour variables.
- Swap the select for a row of radio pills if there are only two or three groups and space allows.
Tag Input With Max Tag Counter and Duplicate Warning
A topic field on a baking forum post that allows five tags. A counter reads 3 of 5 and a thin meter fills as topics are added, turning amber at the limit. Typing a topic that is already there outlines the matching chip in amber and explains the problem under the field instead of ignoring the key press.
<section class="timx" data-max="5">
<div class="timx-card">
<h2 class="timx-title">Topics for your post</h2>
<p class="timx-sub">Help other bakers find “My rye starter finally doubled”.</p>
<div class="timx-head">
<label for="timx-field" class="timx-label">Topics</label>
<span class="timx-count" aria-live="polite"></span>
</div>
<div class="timx-box">
<span class="timx-chips"></span>
<input id="timx-field" class="timx-input" aria-describedby="timx-msg" placeholder="Add a topic" autocomplete="off">
</div>
<p id="timx-msg" class="timx-msg" role="status"></p>
<div class="timx-meter" aria-hidden="true"><div class="timx-fill"></div></div>
</div>
</section>
<style>
.timx { --timx-accent: #059669; --timx-warn: #f59e0b; background: #022c22; padding: 64px 16px; font-family: ui-sans-serif, system-ui, sans-serif; color: #0f172a; }
.timx-card { max-width: 36rem; margin: 0 auto; background: #fff; border-radius: 16px; padding: 28px; }
.timx-title { margin: 0; font-size: 18px; font-weight: 600; }
.timx-sub { margin: 4px 0 0; font-size: 14px; color: #475569; }
.timx-head { display: flex; justify-content: space-between; align-items: baseline; margin-top: 24px; }
.timx-label { font-size: 14px; font-weight: 500; }
.timx-count { font-size: 14px; color: #475569; font-variant-numeric: tabular-nums; }
.timx[data-full] .timx-count { color: #b45309; font-weight: 600; }
.timx-box { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; min-height: 52px; margin-top: 8px; padding: 8px 10px; border: 1px solid #cbd5e1; border-radius: 8px; }
.timx-box:focus-within { border-color: var(--timx-accent); box-shadow: 0 0 0 3px #d1fae5; }
.timx[data-warn] .timx-box { border-color: var(--timx-warn); box-shadow: 0 0 0 3px #fef3c7; }
.timx-chips { display: contents; }
.timx-chip { display: flex; align-items: center; gap: 4px; height: 32px; padding: 0 4px 0 10px; border-radius: 6px; background: #ecfdf5; color: #064e3b; font-size: 14px; font-weight: 500; }
.timx-chip[data-dup] { background: #fef3c7; color: #78350f; box-shadow: inset 0 0 0 1px #fbbf24; }
.timx-chip i { font-style: normal; color: var(--timx-accent); }
.timx-x { width: 24px; height: 24px; border: 0; border-radius: 4px; background: none; color: inherit; cursor: pointer; font-size: 16px; }
.timx-x:focus-visible { outline: 2px solid var(--timx-accent); }
.timx-input { flex: 1; min-width: 7rem; height: 32px; border: 0; outline: 0; font: inherit; font-size: 14px; background: transparent; }
.timx-msg { min-height: 20px; margin: 8px 0 0; font-size: 14px; color: #475569; }
.timx[data-warn] .timx-msg { color: #92400e; }
.timx-meter { height: 6px; margin-top: 16px; border-radius: 999px; background: #f1f5f9; overflow: hidden; }
.timx-fill { height: 100%; border-radius: 999px; background: var(--timx-accent); transition: width .3s; }
.timx[data-full] .timx-fill { background: var(--timx-warn); }
@media (prefers-reduced-motion: reduce) { .timx-fill { transition: none; } }
</style>
<script>
(function () {
var root = document.querySelector(".timx");
if (!root) return;
var MAX = Number(root.getAttribute("data-max")) || 5;
var tags = ["sourdough", "baking", "starter"], dup = "";
var chips = root.querySelector(".timx-chips"), input = root.querySelector(".timx-input");
var count = root.querySelector(".timx-count"), msg = root.querySelector(".timx-msg"), fill = root.querySelector(".timx-fill");
function render(warn) {
while (chips.firstChild) chips.removeChild(chips.firstChild);
tags.forEach(function (t) {
var s = document.createElement("span"), i = document.createElement("i"), b = document.createElement("button");
s.className = "timx-chip"; if (t === dup) s.setAttribute("data-dup", "");
i.textContent = "#"; i.setAttribute("aria-hidden", "true");
b.type = "button"; b.className = "timx-x"; b.textContent = "×"; b.setAttribute("aria-label", "Remove " + t);
b.addEventListener("click", function () { tags = tags.filter(function (x) { return x !== t; }); dup = ""; render(""); input.focus(); });
s.appendChild(i); s.appendChild(document.createTextNode(t)); s.appendChild(b); chips.appendChild(s);
});
var full = tags.length >= MAX, left = MAX - tags.length;
root.toggleAttribute("data-full", full);
root.toggleAttribute("data-warn", !!warn);
input.setAttribute("aria-invalid", warn ? "true" : "false");
input.placeholder = full ? "Limit reached" : "Add a topic";
count.textContent = tags.length + " of " + MAX + (full ? ", limit reached" : "");
msg.textContent = warn || (left > 0 ? "You can add " + left + " more." : "Remove a topic to add a different one.");
fill.style.width = (tags.length / MAX) * 100 + "%";
}
function add() {
var t = input.value.trim().toLowerCase().replace(/^#/, "");
if (!t) return;
if (tags.indexOf(t) > -1) { dup = t; render(t + " is already on this post."); return; }
if (tags.length >= MAX) { render("Posts can have " + MAX + " topics. Remove one to add another."); return; }
tags.push(t); input.value = ""; dup = ""; render("");
}
input.addEventListener("keydown", function (e) {
if (e.key === "Enter" || e.key === ",") { e.preventDefault(); add(); }
else if (e.key === "Backspace" && input.value === "" && tags.length) { tags.pop(); dup = ""; render(""); }
});
input.addEventListener("input", function () { if (dup || root.hasAttribute("data-warn")) { dup = ""; render(""); } });
render("");
})();
</script>
With reduced motion: The small meter under the field jumps to its new length with no easing.
Running cost: One width transition on a thin meter and a re-render of five chips at most.
- Set `MAX` in React, or `data-max` on the section in the plain version, to change the limit.
- Delete the `.replace(/^#/, "")` call if a leading hash should stay part of the stored tag.
- Recolour the warning through `text-amber-800` and `border-amber-500`, or `--timx-warn` in the CSS.
Performance notes
A tag input is cheap to run. Each keystroke updates one string in state and re-renders a short list of chips, which is far below anything a phone would notice. The suggestion list filters a small array in memory on every key, which stays fast up to a few thousand options; past that, filter on the server or debounce the input.
None of these pieces animates anything heavier than a colour or a border on focus, and the chips use plain flex wrapping, so adding a tag never forces more than a small local layout. The plain versions are a few kilobytes of script with no dependencies, and they attach one keydown listener per field rather than one per chip.
Questions
How do I make a tag input in React without a library?
Store the tags in a useState array and the current text in a second piece of state. On keydown, add the trimmed text when the key is Enter or a comma, and drop the last tag when the key is Backspace and the text is empty. Render each tag as a chip with its own remove button.
How do I send the tags with a normal form?
Add a hidden input whose value is the tags joined with commas, or render one hidden input per tag with the same name ending in square brackets. Most servers read either format without extra parsing code.
Should the remove button on each chip be focusable?
Yes. A real button with an aria-label such as Remove design lets keyboard and screen reader users delete one tag in the middle of the list, which Backspace alone cannot do.
How do I stop duplicate tags?
Normalise the text before comparing, usually by trimming spaces and lowering the case, then check whether the array already holds it. Tell the person why nothing was added, either beside the field or in a polite live region.
Can a tag input work with paste?
It can. When the pasted text contains commas or line breaks, split it on those characters and add each piece as its own tag, skipping blanks and duplicates. Two of the pieces here do that for lists of skills and email addresses.
Also in the gallery: React Tag Input with Autocomplete Suggestions, Paste Support and Coloured Category Chips, React Select Dropdown Collection with Searchable Combobox, Multi Select, Async Options and Keyboard Support.
More collections: OTP Input Field in HTML and CSS: 5 Verification Code Inputs, HTML Forms With CSS: 5 Styled Forms to Copy, Tailwind Progress Bars: 5 Progress Bars in React and HTML. All inputs. Build your site free to have a full site built around them.
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App