{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"tag-input-with-max-tag-counter-and-duplicate-warning","type":"registry:component","title":"Tag Input With Max Tag Counter and Duplicate Warning","description":"","dependencies":[],"files":[{"path":"components/we-inc/tag-input-with-max-tag-counter-and-duplicate-warning.tsx","type":"registry:component","content":"import { useRef, useState, type KeyboardEvent } from \"react\";\n\nconst MAX = 5; // the most tags a post may carry\nconst START = [\"sourdough\", \"baking\", \"starter\"];\n\nexport default function TagInputWithMaxCounterAndDuplicateWarning() {\n  const [tags, setTags] = useState<string[]>(START);\n  const [text, setText] = useState(\"baking\");\n  const [warn, setWarn] = useState(\"baking is already on this post.\");\n  const field = useRef<HTMLInputElement>(null);\n  const full = tags.length >= MAX;\n  const left = MAX - tags.length;\n\n  const add = () => {\n    const t = text.trim().toLowerCase().replace(/^#/, \"\");\n    if (!t) return;\n    if (tags.includes(t)) { setWarn(`${t} is already on this post.`); return; }\n    if (full) { setWarn(`Posts can have ${MAX} topics. Remove one to add another.`); return; }\n    setTags([...tags, t]);\n    setText(\"\");\n    setWarn(\"\");\n  };\n  const remove = (t: string) => {\n    setTags(tags.filter((x) => x !== t));\n    setWarn(\"\");\n    field.current?.focus();\n  };\n  const onKey = (e: KeyboardEvent<HTMLInputElement>) => {\n    if (e.key === \"Enter\" || e.key === \",\") { e.preventDefault(); add(); }\n    else if (e.key === \"Backspace\" && text === \"\" && tags.length) remove(tags[tags.length - 1]);\n  };\n\n  return (\n    <section className=\"bg-emerald-950 px-4 py-16\">\n      <div className=\"mx-auto max-w-xl rounded-2xl bg-white p-6 sm:p-8\">\n        <h2 className=\"text-lg font-semibold text-slate-900\">Topics for your post</h2>\n        <p className=\"mt-1 text-sm text-slate-600\">Help other bakers find &ldquo;My rye starter finally doubled&rdquo;.</p>\n        <div className=\"mt-6 flex items-baseline justify-between\">\n          <label htmlFor=\"timx-field\" className=\"text-sm font-medium text-slate-800\">Topics</label>\n          <span className={`text-sm tabular-nums ${full ? \"font-semibold text-amber-700\" : \"text-slate-600\"}`} aria-live=\"polite\">\n            {tags.length} of {MAX}{full ? \", limit reached\" : \"\"}\n          </span>\n        </div>\n        <div onClick={() => field.current?.focus()} className={`mt-2 flex min-h-[52px] flex-wrap items-center gap-2 rounded-lg border px-2.5 py-2 ${warn ? \"border-amber-500 ring-2 ring-amber-100\" : \"border-slate-300 focus-within:border-emerald-600 focus-within:ring-2 focus-within:ring-emerald-100\"}`}>\n          {tags.map((t) => (\n            <span key={t} className={`flex h-8 items-center gap-1 rounded-md pl-2.5 pr-1 text-sm font-medium ${warn.startsWith(t + \" \") ? \"bg-amber-100 text-amber-900 ring-1 ring-amber-400\" : \"bg-emerald-50 text-emerald-900\"}`}>\n              <span className=\"text-emerald-600\" aria-hidden=\"true\">#</span>{t}\n              <button type=\"button\" aria-label={`Remove ${t}`} onClick={() => remove(t)} className=\"grid h-6 w-6 place-items-center rounded hover:bg-black/5 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-emerald-600\">\n                <svg viewBox=\"0 0 20 20\" width=\"12\" height=\"12\" aria-hidden=\"true\"><path d=\"M5 5l10 10M15 5L5 15\" stroke=\"currentColor\" strokeWidth=\"2.2\" strokeLinecap=\"round\" /></svg>\n              </button>\n            </span>\n          ))}\n          <input\n            id=\"timx-field\"\n            ref={field}\n            value={text}\n            onChange={(e) => { setText(e.target.value); setWarn(\"\"); }}\n            onKeyDown={onKey}\n            aria-describedby=\"timx-msg\"\n            aria-invalid={warn ? true : undefined}\n            placeholder={full ? \"Limit reached\" : \"Add a topic\"}\n            className=\"h-8 min-w-[7rem] flex-1 bg-transparent px-1 text-sm text-slate-900 placeholder:text-slate-500 focus:outline-none\"\n          />\n        </div>\n        <p id=\"timx-msg\" role=\"status\" className={`mt-2 flex min-h-[1.25rem] items-center gap-1.5 text-sm ${warn ? \"text-amber-800\" : \"text-slate-600\"}`}>\n          {warn ? (\n            <>\n              <svg viewBox=\"0 0 20 20\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"M10 2l8.5 15h-17z\" fill=\"#f59e0b\" /><path d=\"M10 8v4M10 14.5v.5\" stroke=\"#fff\" strokeWidth=\"1.8\" strokeLinecap=\"round\" /></svg>\n              {warn}\n            </>\n          ) : left > 0 ? `You can add ${left} more.` : \"Remove a topic to add a different one.\"}\n        </p>\n        <div className=\"mt-4 h-1.5 overflow-hidden rounded-full bg-slate-100\" aria-hidden=\"true\">\n          <div className={`h-full rounded-full transition-[width] duration-300 motion-reduce:transition-none ${full ? \"bg-amber-500\" : \"bg-emerald-600\"}`} style={{ width: `${(tags.length / MAX) * 100}%` }} />\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
