{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "input-otp",
  "type": "registry:ui",
  "title": "Input Otp",
  "description": "The input-otp primitive.",
  "dependencies": [],
  "registryDependencies": [
    "https://ui.klarhimmel.com/r/theme.json",
    "https://ui.klarhimmel.com/r/utils.json"
  ],
  "files": [
    {
      "path": "src/ui/input-otp.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/ui/input-otp.tsx",
      "content": "import { useId, useRef, useState, type ClipboardEvent, type KeyboardEvent } from 'react';\nimport { cn } from '../lib/utils';\n\nexport interface InputOtpProps {\n  /** How many characters. Default 6. */\n  length?: number;\n  value?: string;\n  defaultValue?: string;\n  onValueChange?: (value: string) => void;\n  /** Called once every box is filled. */\n  onComplete?: (value: string) => void;\n  /** Digits only, or letters and digits. */\n  type?: 'numeric' | 'alphanumeric';\n  /** Submits the whole code with a form (a hidden input). */\n  name?: string;\n  disabled?: boolean;\n  readOnly?: boolean;\n  invalid?: boolean;\n  /** Names the group. Required unless aria-labelledby points at a label. */\n  'aria-label'?: string;\n  'aria-labelledby'?: string;\n  'aria-describedby'?: string;\n  /** The name of box `n` of `of`: default \"Digit 1 of 6\" (or \"Character\" for alphanumeric). Pass it translated. */\n  boxLabel?: (n: number, of: number) => string;\n  className?: string;\n}\n\n/**\n * A code split into boxes that behave as one field. Typing moves forward, Backspace clears and moves back, arrows move,\n * pasting from any box fills from there, and the first box asks the browser for the one-time-code autofill. The whole code\n * is one value, also in a form.\n */\nexport function InputOtp({\n  length = 6, value, defaultValue = '', onValueChange, onComplete, type = 'numeric', name, disabled, readOnly, invalid, boxLabel, className, ...aria\n}: InputOtpProps) {\n  const id = useId();\n  const clean = (s: string) => (type === 'numeric' ? s.replace(/\\D/g, '') : s.replace(/[^a-zA-Z0-9]/g, '')).slice(0, length);\n  const [own, setOwn] = useState(clean(defaultValue));\n  /** The code is one string without gaps: box i shows its character i, boxes after the end are empty. */\n  const text = clean(value ?? own);\n  const boxes = useRef<(HTMLInputElement | null)[]>([]);\n  const focus = (i: number) => boxes.current[Math.max(0, Math.min(length - 1, i))]?.focus();\n  const set = (next: string) => {\n    setOwn(next);\n    onValueChange?.(next);\n    if (next.length === length) onComplete?.(next);\n  };\n  /** Writes `t` starting at box `from` (never past the end of what is typed), keeping the rest. */\n  const put = (from: number, t: string) => {\n    const add = clean(t).slice(0, length - Math.min(from, text.length));\n    if (!add) return;\n    const at = Math.min(from, text.length);\n    set(clean(text.slice(0, at) + add + text.slice(at + add.length)));\n    focus(at + add.length);\n  };\n  const remove = (i: number) => set(text.slice(0, i) + text.slice(i + 1));\n  const onKey = (i: number, e: KeyboardEvent<HTMLInputElement>) => {\n    if (e.key === 'Backspace' && i >= text.length) {\n      e.preventDefault();\n      if (i > 0) {\n        remove(i - 1);\n        focus(i - 1);\n      }\n    } else if (e.key === 'Delete') {\n      e.preventDefault();\n      remove(i);\n    } else if (e.key === 'ArrowLeft') {\n      e.preventDefault();\n      focus(i - 1);\n    } else if (e.key === 'ArrowRight') {\n      e.preventDefault();\n      focus(i + 1);\n    } else if (e.key === 'Home') {\n      e.preventDefault();\n      focus(0);\n    } else if (e.key === 'End') {\n      e.preventDefault();\n      focus(length - 1);\n    }\n  };\n  const label = boxLabel ?? ((n, of) => `${type === 'numeric' ? 'Digit' : 'Character'} ${n} of ${of}`);\n  return (\n    <div role=\"group\" aria-invalid={invalid || undefined} className={cn('flex gap-2', className)} {...aria}>\n      {Array.from({ length }, (_, i) => text[i] ?? '').map((c, i) => (\n        <input\n          key={i}\n          ref={(el) => {\n            boxes.current[i] = el;\n          }}\n          id={`${id}-${i}`}\n          value={c}\n          inputMode={type === 'numeric' ? 'numeric' : 'text'}\n          pattern={type === 'numeric' ? '[0-9]*' : '[A-Za-z0-9]*'}\n          maxLength={1}\n          autoComplete={i === 0 ? 'one-time-code' : 'off'}\n          autoCapitalize=\"characters\"\n          spellCheck={false}\n          aria-label={label(i + 1, length)}\n          aria-invalid={invalid || undefined}\n          disabled={disabled}\n          readOnly={readOnly}\n          onChange={(e) => (e.target.value === '' ? remove(i) : put(i, e.target.value))}\n          onKeyDown={(e) => onKey(i, e)}\n          onPaste={(e: ClipboardEvent<HTMLInputElement>) => {\n            e.preventDefault();\n            put(i, e.clipboardData.getData('text'));\n          }}\n          onFocus={(e) => e.currentTarget.select()}\n          className={cn(\n            'min-h-12 w-full min-w-0 rounded-kh border bg-kh-surface text-center text-kh-heading font-semibold tabular-nums text-kh-ink shadow-kh-1 transition-[border-color] duration-(--kh-dur)',\n            'hover:border-kh-muted focus-visible:outline-2 focus-visible:outline-offset-0 focus-visible:outline-(--kh-focus) disabled:cursor-not-allowed disabled:bg-kh-surface-2 disabled:text-kh-muted',\n            invalid ? 'border-kh-danger' : 'border-kh-line-strong',\n          )}\n        />\n      ))}\n      {name && <input type=\"hidden\" name={name} value={text} />}\n    </div>\n  );\n}\n"
    }
  ]
}
