{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "input-otp",
  "type": "registry:ui",
  "title": "Input OTP",
  "description": "One-time-code field with a monospace box per digit, paste distribution and autofill support.",
  "dependencies": [],
  "registryDependencies": [
    "cx",
    "globals"
  ],
  "files": [
    {
      "path": "components/base/input-otp/input-otp.tsx",
      "content": "\"use client\";\n\nimport {\n  useId,\n  useRef,\n  useState,\n  type ClipboardEvent,\n  type KeyboardEvent,\n  type Ref,\n} from \"react\";\nimport { cx } from \"@/utils/cx\";\n\n/**\n * One-time-code input: one box per digit, side by side.\n *\n * The awkward part of an OTP field is that it looks like N inputs but behaves\n * like one value, and the two views have to stay in step in every direction:\n *\n *   type      fills a box and advances\n *   Backspace clears the box, or steps back when the box is already empty\n *   arrows    move between boxes without changing anything\n *   paste     distributes across the boxes from wherever the caret is\n *   autofill  the OS hands the whole code to the first box at once\n *\n * That last one is why every box carries `autoComplete=\"one-time-code\"` and\n * why the change handler accepts more than a single character: iOS and Chrome\n * both deliver the full code into whichever box has focus, and a field that\n * only accepts one character silently drops five digits of it.\n *\n * Digits are monospace so the boxes stay optically even — in a proportional\n * face a `1` is visibly narrower than a `8`, which makes a row of fixed boxes\n * look mis-set even when it is perfectly aligned.\n */\n\nexport interface InputOtpProps {\n  /** Number of digit boxes. */\n  length?: number;\n  /** Controlled value. Longer strings are truncated to `length`. */\n  value?: string;\n  defaultValue?: string;\n  onChange?: (value: string) => void;\n  /** Fires once the last box is filled. */\n  onComplete?: (value: string) => void;\n  isDisabled?: boolean;\n  isInvalid?: boolean;\n  /** Renders a gap between groups, e.g. `3` gives 000 000. */\n  groupEvery?: number;\n  \"aria-label\"?: string;\n  className?: string;\n  ref?: Ref<HTMLDivElement>;\n}\n\nconst DIGITS_ONLY = /\\D/g;\n\nexport function InputOtp({\n  length = 6,\n  value,\n  defaultValue = \"\",\n  onChange,\n  onComplete,\n  isDisabled = false,\n  isInvalid = false,\n  groupEvery,\n  \"aria-label\": ariaLabel = \"One-time code\",\n  className,\n  ref,\n}: InputOtpProps) {\n  const groupId = useId();\n  const inputsRef = useRef<Array<HTMLInputElement | null>>([]);\n  const [internal, setInternal] = useState(defaultValue.replace(DIGITS_ONLY, \"\").slice(0, length));\n\n  const controlled = value !== undefined;\n  const code = (controlled ? value : internal).replace(DIGITS_ONLY, \"\").slice(0, length);\n\n  const commit = (next: string) => {\n    const clean = next.replace(DIGITS_ONLY, \"\").slice(0, length);\n    if (!controlled) setInternal(clean);\n    onChange?.(clean);\n    if (clean.length === length) onComplete?.(clean);\n  };\n\n  const focusBox = (index: number) => {\n    const target = inputsRef.current[Math.max(0, Math.min(index, length - 1))];\n    target?.focus();\n    target?.select();\n  };\n\n  /** Writes `digits` starting at `index`, which covers typing and autofill. */\n  const writeFrom = (index: number, digits: string) => {\n    const clean = digits.replace(DIGITS_ONLY, \"\");\n    if (clean === \"\") return;\n\n    const chars = code.padEnd(length, \" \").split(\"\");\n    for (let offset = 0; offset < clean.length && index + offset < length; offset += 1) {\n      chars[index + offset] = clean[offset];\n    }\n    commit(chars.join(\"\").trimEnd());\n    focusBox(index + clean.length);\n  };\n\n  const onKeyDown = (event: KeyboardEvent<HTMLInputElement>, index: number) => {\n    if (event.key === \"Backspace\") {\n      event.preventDefault();\n      if (code[index]) {\n        // Clear in place; the caret only steps back on an already-empty box,\n        // which is what makes holding Backspace feel right.\n        const chars = code.padEnd(length, \" \").split(\"\");\n        chars[index] = \" \";\n        commit(chars.join(\"\").trimEnd());\n        return;\n      }\n      const chars = code.padEnd(length, \" \").split(\"\");\n      chars[Math.max(0, index - 1)] = \" \";\n      commit(chars.join(\"\").trimEnd());\n      focusBox(index - 1);\n      return;\n    }\n\n    if (event.key === \"ArrowLeft\") {\n      event.preventDefault();\n      focusBox(index - 1);\n    } else if (event.key === \"ArrowRight\") {\n      event.preventDefault();\n      focusBox(index + 1);\n    }\n  };\n\n  const onPaste = (event: ClipboardEvent<HTMLInputElement>, index: number) => {\n    event.preventDefault();\n    writeFrom(index, event.clipboardData.getData(\"text\"));\n  };\n\n  return (\n    <div\n      ref={ref}\n      role=\"group\"\n      dir=\"ltr\"\n      aria-label={ariaLabel}\n      // No `aria-invalid` here: `group` does not support it. Each box carries\n      // its own, which is where a screen reader looks anyway.\n      className={cx(\"flex items-center gap-2\", className)}\n    >\n      {Array.from({ length }, (_, index) => {\n        const filled = code[index] ?? \"\";\n        const gapBefore = groupEvery !== undefined && index > 0 && index % groupEvery === 0;\n\n        return (\n          <div key={index} className={cx(\"flex items-center\", gapBefore && \"ms-3\")}>\n            <input\n              ref={(node) => {\n                inputsRef.current[index] = node;\n              }}\n              id={`${groupId}-${index}`}\n              // `text` with a numeric mode rather than `number`: a number input\n              // brings spinners, accepts `e` and `-`, and its value is empty\n              // for anything it considers malformed.\n              type=\"text\"\n              inputMode=\"numeric\"\n              autoComplete=\"one-time-code\"\n              // Long enough to accept a full autofilled code in one box.\n              maxLength={length}\n              disabled={isDisabled}\n              aria-label={`Digit ${index + 1} of ${length}`}\n              aria-invalid={isInvalid || undefined}\n              value={filled}\n              onChange={(event) => writeFrom(index, event.target.value)}\n              onKeyDown={(event) => onKeyDown(event, index)}\n              onPaste={(event) => onPaste(event, index)}\n              onFocus={(event) => event.target.select()}\n              className={cx(\n                \"size-12 rounded-2lg text-center font-mono text-title-3-medium tabular-nums\",\n                \"bg-background-primary-default text-text-primary\",\n                \"border border-border-button-default shadow-xs\",\n                \"transition-[background-color,border-color,box-shadow] duration-150 ease outline-none\",\n                \"hover:border-border-button-hover\",\n                \"focus-visible:border-border-focus-ring focus-visible:ring-2 focus-visible:ring-border-focus-ring\",\n                // Red edge as well as the tint. `border-error-default` was\n                // added to theme.css for this: the system had greys only.\n                isInvalid &&\n                  \"border-border-error-default bg-background-tertiary-error text-foreground-icon-error hover:border-border-error-default\",\n                isDisabled &&\n                  \"cursor-not-allowed bg-background-primary-disabled text-text-tertiary shadow-none\",\n              )}\n            />\n          </div>\n        );\n      })}\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/base/input-otp/input-otp.tsx"
    }
  ],
  "meta": {
    "docsUrl": "https://www.boardui.com/components/input-otp",
    "example": "import { InputOtp } from \"@/components/base/input-otp/input-otp\";\n\nexport function Example() {\n  return (\n    <InputOtp\n      aria-label=\"Verification code\"\n      onComplete={(code) => verify(code)}\n    />\n  );\n}",
    "snippets": [
      {
        "label": "controlled",
        "code": "const [code, setCode] = useState(\"\");\n\n<InputOtp\n  value={code}\n  onChange={setCode}\n  onComplete={(value) => verify(value)}\n  aria-label=\"Verification code\"\n/>"
      },
      {
        "label": "options",
        "code": "{/* Four boxes instead of six */}\n<InputOtp length={4} aria-label=\"PIN\" />\n\n{/* Split into groups: 000 000 */}\n<InputOtp groupEvery={3} aria-label=\"Verification code\" />\n\n{/* States */}\n<InputOtp isInvalid aria-label=\"Verification code\" />\n<InputOtp isDisabled defaultValue=\"1234\" aria-label=\"Verification code\" />"
      }
    ]
  }
}
