{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"tag-input-with-arrow-key-autocomplete-suggestion-list","type":"registry:component","title":"Tag Input With Arrow Key Autocomplete Suggestion List","description":"","dependencies":[],"files":[{"path":"components/we-inc/tag-input-with-arrow-key-autocomplete-suggestion-list.tsx","type":"registry:component","content":"import { useRef, useState, type KeyboardEvent } from \"react\";\n\n// The known topics people can pick from. Free text is still allowed with Enter.\nconst OPTIONS = [\"react\", \"react native\", \"redux\", \"remix\", \"rust\", \"ruby\", \"rails\", \"svelte\", \"solid\", \"swift\", \"typescript\", \"tailwind\", \"testing\", \"vite\", \"vue\"];\n\nexport default function TagInputWithArrowKeyAutocomplete() {\n  const [tags, setTags] = useState<string[]>([\"typescript\"]);\n  const [text, setText] = useState(\"re\");\n  const [open, setOpen] = useState(true);\n  const [active, setActive] = useState(0);\n  const [note, setNote] = useState(\"\");\n  const field = useRef<HTMLInputElement>(null);\n\n  const matches = OPTIONS.filter((o) => o.startsWith(text.trim().toLowerCase()) && !tags.includes(o)).slice(0, 6);\n  const showList = open && text.trim() !== \"\" && matches.length > 0;\n\n  const add = (t: string) => {\n    const v = t.trim().toLowerCase();\n    if (!v || tags.includes(v)) return;\n    setTags([...tags, v]);\n    setText(\"\");\n    setActive(0);\n    setNote(`Added ${v}`);\n    field.current?.focus();\n  };\n  const remove = (t: string) => {\n    setTags(tags.filter((x) => x !== t));\n    setNote(`Removed ${t}`);\n    field.current?.focus();\n  };\n\n  const onKey = (e: KeyboardEvent<HTMLInputElement>) => {\n    if (e.key === \"ArrowDown\" && matches.length) { e.preventDefault(); setOpen(true); setActive((a) => (a + 1) % matches.length); }\n    else if (e.key === \"ArrowUp\" && matches.length) { e.preventDefault(); setActive((a) => (a - 1 + matches.length) % matches.length); }\n    else if (e.key === \"Enter\") { e.preventDefault(); add(showList ? matches[active] : text); }\n    else if (e.key === \"Escape\") setOpen(false);\n    else if (e.key === \"Backspace\" && text === \"\" && tags.length) remove(tags[tags.length - 1]);\n  };\n\n  return (\n    <section className=\"bg-slate-950 px-4 pb-48 pt-16\">\n      <div className=\"mx-auto max-w-lg\">\n        <h2 className=\"text-lg font-semibold text-white\">Follow topics</h2>\n        <p className=\"mt-1 text-sm text-slate-300\">Pick what shows up in your weekly reading list.</p>\n        <label htmlFor=\"tiac-field\" className=\"mt-6 block text-sm font-medium text-slate-200\">Topics</label>\n        <div className=\"relative mt-2\">\n          <div onClick={() => field.current?.focus()} className=\"flex min-h-[52px] flex-wrap items-center gap-2 rounded-xl border border-slate-700 bg-slate-900 px-2.5 py-2 focus-within:border-teal-400 focus-within:ring-2 focus-within:ring-teal-400/30\">\n            {tags.map((t) => (\n              <span key={t} className=\"flex h-8 items-center gap-1 rounded-full border border-teal-400/40 bg-teal-400/10 pl-3 pr-1 text-sm text-teal-100\">\n                {t}\n                <button type=\"button\" aria-label={`Remove ${t}`} onClick={() => remove(t)} className=\"grid h-6 w-6 place-items-center rounded-full text-teal-200 hover:bg-teal-400/20 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-teal-300\">\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=\"tiac-field\"\n              ref={field}\n              role=\"combobox\"\n              aria-expanded={showList}\n              aria-controls=\"tiac-list\"\n              aria-autocomplete=\"list\"\n              aria-activedescendant={showList ? `tiac-opt-${active}` : undefined}\n              value={text}\n              onChange={(e) => { setText(e.target.value); setOpen(true); setActive(0); }}\n              onKeyDown={onKey}\n              autoComplete=\"off\"\n              placeholder=\"Search topics\"\n              className=\"h-8 min-w-[7rem] flex-1 bg-transparent px-1 text-sm text-white placeholder:text-slate-400 focus:outline-none\"\n            />\n          </div>\n          {showList && (\n            <ul id=\"tiac-list\" role=\"listbox\" aria-label=\"Suggested topics\" className=\"absolute inset-x-0 top-full z-10 mt-2 overflow-hidden rounded-xl border border-slate-700 bg-slate-900 py-1 shadow-xl shadow-black/40\">\n              {matches.map((m, i) => (\n                <li\n                  key={m}\n                  id={`tiac-opt-${i}`}\n                  role=\"option\"\n                  aria-selected={i === active}\n                  onMouseDown={(e) => { e.preventDefault(); add(m); }}\n                  onMouseEnter={() => setActive(i)}\n                  className={`flex h-10 cursor-pointer items-center justify-between px-4 text-sm ${i === active ? \"bg-teal-400/15 text-white\" : \"text-slate-300\"}`}\n                >\n                  <span><strong className=\"font-semibold text-teal-300\">{m.slice(0, text.trim().length)}</strong>{m.slice(text.trim().length)}</span>\n                  {i === active && <kbd className=\"rounded border border-slate-600 px-1.5 text-[11px] text-slate-300\">Enter</kbd>}\n                </li>\n              ))}\n            </ul>\n          )}\n        </div>\n        <p className=\"mt-2 text-xs text-slate-400\">Arrow keys move through suggestions. Escape closes the list.</p>\n        <p aria-live=\"polite\" className=\"sr-only\">{showList ? `${matches.length} suggestions. ${note}` : note}</p>\n      </div>\n    </section>\n  );\n}\n"}]}
