{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"six-box-otp-input-with-auto-advance-and-paste","type":"registry:component","title":"Six Box OTP Input With Auto Advance and Paste","description":"Free six-box OTP input in HTML, CSS and React: focus moves on as you type, Backspace steps back, paste or SMS autofill fills every box, arrows move between digits.","dependencies":[],"files":[{"path":"components/we-inc/six-box-otp-input-with-auto-advance-and-paste.tsx","type":"registry:component","content":"import { useRef, useState, type ClipboardEvent, type KeyboardEvent } from \"react\";\n\nconst LENGTH = 6; // number of digits in the code\nconst PHONE = \"+44 7700 900 182\";\n\nexport default function SixBoxOtpInput() {\n  const [digits, setDigits] = useState<string[]>([\"4\", \"8\", \"2\", \"\", \"\", \"\"]);\n  const boxes = useRef<(HTMLInputElement | null)[]>([]);\n  const code = digits.join(\"\");\n  const done = code.length === LENGTH;\n\n  const focusAt = (i: number) => boxes.current[Math.max(0, Math.min(LENGTH - 1, i))]?.focus();\n  const setAt = (i: number, v: string) => setDigits((d) => d.map((x, j) => (j === i ? v : x)));\n\n  const onInput = (i: number, raw: string) => {\n    const only = raw.replace(/\\D/g, \"\");\n    if (only.length > 1) return fill(i, only); // autofill can drop a whole code into one box\n    setAt(i, only);\n    if (only) focusAt(i + 1);\n  };\n  const fill = (start: number, text: string) => {\n    const next = [...digits];\n    text.split(\"\").slice(0, LENGTH - start).forEach((c, k) => { next[start + k] = c; });\n    setDigits(next);\n    focusAt(start + text.length);\n  };\n  const onKey = (i: number, e: KeyboardEvent<HTMLInputElement>) => {\n    if (e.key === \"Backspace\" && !digits[i]) { e.preventDefault(); setAt(i - 1, \"\"); focusAt(i - 1); }\n    else if (e.key === \"ArrowLeft\") { e.preventDefault(); focusAt(i - 1); }\n    else if (e.key === \"ArrowRight\") { e.preventDefault(); focusAt(i + 1); }\n  };\n  const onPaste = (i: number, e: ClipboardEvent<HTMLInputElement>) => {\n    e.preventDefault();\n    fill(i, e.clipboardData.getData(\"text\").replace(/\\D/g, \"\"));\n  };\n\n  return (\n    <section className=\"bg-gradient-to-b from-indigo-50 to-white px-4 py-16 sm:py-20\">\n      <form onSubmit={(e) => e.preventDefault()} className=\"mx-auto max-w-md rounded-2xl border border-indigo-100 bg-white p-6 text-center shadow-[0_20px_50px_-24px_rgba(67,56,202,0.35)] sm:p-8\">\n        <div className=\"mx-auto grid h-12 w-12 place-items-center rounded-xl bg-indigo-600 text-white\" aria-hidden=\"true\">\n          <svg viewBox=\"0 0 24 24\" width=\"22\" height=\"22\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\"><rect x=\"6\" y=\"2\" width=\"12\" height=\"20\" rx=\"2.5\" /><path d=\"M10.5 18h3\" /></svg>\n        </div>\n        <h2 className=\"mt-4 text-xl font-semibold tracking-tight text-slate-900\">Check your phone</h2>\n        <p className=\"mt-1 text-sm text-slate-600\">We sent a 6-digit code to <span className=\"font-medium text-slate-800\">{PHONE}</span></p>\n        <fieldset className=\"mt-6\">\n          <legend className=\"sr-only\">Verification code</legend>\n          <div className=\"flex justify-center gap-2 sm:gap-3\">\n            {digits.map((d, i) => (\n              <input\n                key={i}\n                ref={(el) => { boxes.current[i] = el; }}\n                value={d}\n                onChange={(e) => onInput(i, e.target.value)}\n                onKeyDown={(e) => onKey(i, e)}\n                onPaste={(e) => onPaste(i, e)}\n                onFocus={(e) => e.target.select()}\n                inputMode=\"numeric\"\n                autoComplete={i === 0 ? \"one-time-code\" : \"off\"}\n                pattern=\"[0-9]*\"\n                maxLength={LENGTH}\n                aria-label={`Digit ${i + 1} of ${LENGTH}`}\n                className={`h-14 w-11 rounded-xl border text-center text-2xl font-semibold tabular-nums text-slate-900 caret-indigo-600 transition-colors focus:border-indigo-600 focus:outline-none focus:ring-4 focus:ring-indigo-100 sm:w-12 ${d ? \"border-slate-400 bg-white\" : \"border-slate-300 bg-slate-50\"}`}\n              />\n            ))}\n          </div>\n        </fieldset>\n        <button type=\"submit\" disabled={!done} className=\"mt-6 h-11 w-full rounded-xl bg-indigo-600 text-sm font-semibold text-white hover:bg-indigo-700 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-indigo-600 focus-visible:ring-offset-2 disabled:bg-indigo-200 disabled:text-indigo-900\">\n          {done ? \"Verify\" : `Enter ${LENGTH - code.length} more ${LENGTH - code.length === 1 ? \"digit\" : \"digits\"}`}\n        </button>\n        <p className=\"mt-4 text-sm text-slate-600\">Didn&rsquo;t get it? <a href=\"#\" className=\"rounded font-medium text-indigo-700 underline-offset-2 hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-indigo-600\">Send a new code</a></p>\n        <p aria-live=\"polite\" className=\"sr-only\">{done ? \"All six digits entered\" : \"\"}</p>\n      </form>\n    </section>\n  );\n}\n"}]}
