{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"otp-input-with-shake-error-and-success-states","type":"registry:component","title":"OTP Input With Shake Error and Success States","description":"","dependencies":[],"files":[{"path":"components/we-inc/otp-input-with-shake-error-and-success-states.tsx","type":"registry:component","content":"import { useRef, useState, type ClipboardEvent, type KeyboardEvent } from \"react\";\n\nconst LENGTH = 6;\nconst DEMO_CODE = \"260418\"; // in a real app the server checks the code; this demo accepts this one\ntype Status = \"idle\" | \"error\" | \"success\";\n\nexport default function OtpInputWithShakeErrorAndSuccess() {\n  const [digits, setDigits] = useState<string[]>(\"260417\".split(\"\"));\n  const [status, setStatus] = useState<Status>(\"error\");\n  const [tries, setTries] = useState(1);\n  const boxes = useRef<(HTMLInputElement | null)[]>([]);\n  const code = digits.join(\"\");\n\n  const focusAt = (i: number) => boxes.current[Math.max(0, Math.min(LENGTH - 1, i))]?.focus();\n  const put = (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    setStatus(\"idle\");\n    focusAt(start + text.length);\n  };\n  const onKey = (i: number, e: KeyboardEvent<HTMLInputElement>) => {\n    if (e.key === \"Backspace\" && !digits[i] && i > 0) { e.preventDefault(); setDigits(digits.map((x, j) => (j === i - 1 ? \"\" : x))); focusAt(i - 1); }\n  };\n  const verify = () => {\n    if (code === DEMO_CODE) { setStatus(\"success\"); return; }\n    setStatus(\"error\");\n    setTries((t) => t + 1);\n    setDigits(Array(LENGTH).fill(\"\"));\n    focusAt(0);\n  };\n\n  const ring =\n    status === \"error\" ? \"border-rose-500 bg-rose-50 text-rose-900 focus:ring-rose-200\"\n    : status === \"success\" ? \"border-emerald-500 bg-emerald-50 text-emerald-900 focus:ring-emerald-200\"\n    : \"border-slate-300 bg-white text-slate-900 focus:border-slate-900 focus:ring-slate-200\";\n\n  return (\n    <section className=\"bg-white px-4 py-16\">\n      <style>{`\n        @keyframes otp-shake-x { 0%,100% { transform: translateX(0) } 20% { transform: translateX(-8px) } 40% { transform: translateX(7px) } 60% { transform: translateX(-5px) } 80% { transform: translateX(3px) } }\n        .otp-shake-row { animation: otp-shake-x 380ms cubic-bezier(.36,.07,.19,.97) both; }\n        @media (prefers-reduced-motion: reduce) { .otp-shake-row { animation: none; } }\n      `}</style>\n      <form onSubmit={(e) => { e.preventDefault(); verify(); }} className=\"mx-auto max-w-md rounded-3xl border border-slate-200 p-6 sm:p-8\">\n        <h2 className=\"text-xl font-semibold tracking-tight text-slate-900\">Verify your email</h2>\n        <p className=\"mt-1 text-sm text-slate-600\">We emailed a code to maya@northpine.studio. Try {DEMO_CODE} in this demo.</p>\n        <fieldset className=\"mt-6 min-w-0\">\n          <legend className=\"sr-only\">Verification code</legend>\n          <div key={status === \"error\" ? `e${tries}` : \"row\"} className={`flex gap-2 ${status === \"error\" ? \"otp-shake-row\" : \"\"}`}>\n            {digits.map((d, i) => (\n              <input\n                key={i}\n                ref={(el) => { boxes.current[i] = el; }}\n                value={d}\n                onChange={(e) => {\n                  const only = e.target.value.replace(/\\D/g, \"\");\n                  if (only.length > 1) return put(i, only);\n                  setDigits(digits.map((x, j) => (j === i ? only : x)));\n                  if (status !== \"idle\") setStatus(\"idle\");\n                  if (only) focusAt(i + 1);\n                }}\n                onKeyDown={(e) => onKey(i, e)}\n                onPaste={(e: ClipboardEvent<HTMLInputElement>) => { e.preventDefault(); put(i, e.clipboardData.getData(\"text\").replace(/\\D/g, \"\")); }}\n                inputMode=\"numeric\"\n                autoComplete={i === 0 ? \"one-time-code\" : \"off\"}\n                maxLength={LENGTH}\n                readOnly={status === \"success\"}\n                aria-label={`Digit ${i + 1} of ${LENGTH}`}\n                aria-invalid={status === \"error\" || undefined}\n                aria-describedby=\"otp-shake-msg\"\n                className={`h-14 w-full min-w-0 rounded-xl border-2 text-center text-xl font-semibold tabular-nums transition-colors focus:outline-none focus:ring-4 ${ring}`}\n              />\n            ))}\n          </div>\n        </fieldset>\n        <p id=\"otp-shake-msg\" role=\"status\" className={`mt-3 flex min-h-[1.5rem] items-center gap-2 text-sm font-medium ${status === \"error\" ? \"text-rose-700\" : \"text-emerald-700\"}`}>\n          {status === \"error\" && (<><svg viewBox=\"0 0 20 20\" width=\"16\" height=\"16\" aria-hidden=\"true\"><circle cx=\"10\" cy=\"10\" r=\"8\" fill=\"#e11d48\" /><path d=\"M7 7l6 6M13 7l-6 6\" stroke=\"#fff\" strokeWidth=\"1.8\" strokeLinecap=\"round\" /></svg>That code didn&rsquo;t match. {Math.max(0, 5 - tries)} tries left.</>)}\n          {status === \"success\" && (<><svg viewBox=\"0 0 20 20\" width=\"16\" height=\"16\" aria-hidden=\"true\"><circle cx=\"10\" cy=\"10\" r=\"8\" fill=\"#059669\" /><path d=\"M6.5 10.5l2.2 2.2 4.8-5\" stroke=\"#fff\" strokeWidth=\"1.8\" fill=\"none\" strokeLinecap=\"round\" strokeLinejoin=\"round\" /></svg>Email verified. You&rsquo;re all set.</>)}\n        </p>\n        <button type=\"submit\" disabled={code.length < LENGTH || status === \"success\"} className=\"mt-4 h-11 w-full rounded-xl bg-slate-900 text-sm font-semibold text-white hover:bg-slate-800 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-slate-900 focus-visible:ring-offset-2 disabled:bg-slate-200 disabled:text-slate-700\">\n          {status === \"success\" ? \"Verified\" : \"Verify code\"}\n        </button>\n      </form>\n    </section>\n  );\n}\n"}]}
