{
  "name": "input-otp",
  "type": "ui:component",
  "title": "InputOTP",
  "description": "Verification code input with auto-advance, Backspace rewind, paste support, and one-time-code autocomplete.",
  "dependencies": [],
  "registryDependencies": [
    "theme"
  ],
  "files": [
    {
      "path": "src/ui/input-otp.tsx",
      "target": "src/ui/input-otp.tsx",
      "sha256": "fe2dcd5a557f0788e4e45153bcfd6af4f1b19c4f0dc092b453a399d6480570f2",
      "content": "import './theme.css';\nimport { scope, type Ctx } from '@fierst/aster';\n\nconst otpScope = scope`\n  :scope {\n    display: grid;\n    gap: 0.45rem;\n    font-family: var(--aster-font-sans);\n  }\n  .otp-label { font-size: 0.84rem; font-weight: 500; color: var(--aster-on-surface); }\n  .otp-slots {\n    display: inline-flex;\n    align-items: center;\n    gap: 0.4rem;\n    flex-wrap: wrap;\n  }\n  .otp-sep {\n    width: 8px;\n    height: 2px;\n    border-radius: 9999px;\n    background: var(--aster-border-strong);\n    margin: 0 0.1rem;\n  }\n  .otp-cell {\n    width: 40px;\n    height: 44px;\n    border-radius: var(--aster-radius-control);\n    border: 1px solid var(--aster-input-border);\n    background: var(--aster-surface);\n    color: var(--aster-on-surface);\n    font-family: var(--aster-font-mono);\n    font-size: 1.05rem;\n    font-weight: 600;\n    text-align: center;\n    box-shadow: var(--aster-shadow-xs);\n    outline: none;\n    transition: border-color var(--aster-dur-fast), box-shadow var(--aster-dur-fast);\n  }\n  .otp-cell:hover:not(:disabled) { border-color: var(--aster-border-strong); }\n  .otp-cell:focus { border-color: var(--aster-primary); box-shadow: var(--aster-focus-ring); }\n  .otp-cell:disabled { opacity: 0.45; cursor: not-allowed; }\n  .otp-hint { font-size: 0.78rem; color: var(--aster-on-surface-muted); }\n`;\n\nlet otpSeq = 0;\n\nexport function* InputOTP(\n  props: {\n    length?: number;\n    value?: string;\n    defaultValue?: string;\n    label?: string;\n    hint?: string;\n    separatorAt?: number;\n    disabled?: boolean;\n    onChange?: (code: string) => void;\n    onComplete?: (code: string) => void;\n    class?: string;\n  },\n  ctx: Ctx,\n) {\n  const uid = `aster-otp-${++otpSeq}`;\n  let internal = props.defaultValue ?? '';\n\n  for (props of ctx) {\n    const len = props.length ?? 6;\n    const isControlled = props.value !== undefined;\n    const raw = (isControlled ? props.value! : internal).replace(/\\D/g, '').slice(0, len);\n    const digits = Array.from({ length: len }, (_, i) => raw[i] ?? '');\n    const sepAt = props.separatorAt ?? (len === 6 ? 3 : undefined);\n\n    const focusSlot = (idx: number) => {\n      const clamped = Math.max(0, Math.min(len - 1, idx));\n      document.getElementById(`${uid}-${clamped}`)?.focus();\n    };\n\n    const commit = (nextDigits: string[]) => {\n      const next = nextDigits.join('').slice(0, len);\n      if (!isControlled) internal = next;\n      props.onChange?.(next);\n      if (next.length === len) props.onComplete?.(next);\n    };\n\n    const onInput = (idx: number, e: Event) => {\n      const el = e.target as HTMLInputElement;\n      const cleaned = el.value.replace(/\\D/g, '');\n      if (!cleaned) {\n        const next = [...digits];\n        next[idx] = '';\n        commit(next);\n        return;\n      }\n      const next = [...digits];\n      for (let k = 0; k < cleaned.length && idx + k < len; k++) {\n        next[idx + k] = cleaned[k];\n      }\n      commit(next);\n      focusSlot(Math.min(len - 1, idx + cleaned.length));\n    };\n\n    const onKeyDown = (idx: number, e: KeyboardEvent) => {\n      if (e.key === 'Backspace') {\n        if (!digits[idx] && idx > 0) {\n          e.preventDefault();\n          const next = [...digits];\n          next[idx - 1] = '';\n          commit(next);\n          focusSlot(idx - 1);\n        }\n      } else if (e.key === 'ArrowLeft') {\n        e.preventDefault();\n        focusSlot(idx - 1);\n      } else if (e.key === 'ArrowRight') {\n        e.preventDefault();\n        focusSlot(idx + 1);\n      }\n    };\n\n    const onPaste = (e: Event) => {\n      const text = (e as ClipboardEvent).clipboardData?.getData('text') ?? '';\n      const cleaned = text.replace(/\\D/g, '').slice(0, len);\n      if (!cleaned) return;\n      e.preventDefault();\n      const next = Array.from({ length: len }, (_, i) => cleaned[i] ?? '');\n      commit(next);\n      focusSlot(Math.min(len - 1, cleaned.length));\n    };\n\n    yield (\n      <div class={[otpScope, props.class].filter(Boolean).join(' ')}>\n        {props.label ? (\n          <label class=\"otp-label\" for={`${uid}-0`}>\n            {props.label}\n          </label>\n        ) : null}\n        <div class=\"otp-slots\" role=\"group\" aria-label={props.label ?? 'One-time code'}>\n          {digits.map((ch, idx) => (\n            <span key={idx} style=\"display:inline-flex; align-items:center; gap:0.4rem\">\n              {sepAt !== undefined && idx === sepAt ? (\n                <span class=\"otp-sep\" aria-hidden=\"true\" />\n              ) : null}\n              <input\n                id={`${uid}-${idx}`}\n                type=\"text\"\n                inputmode=\"numeric\"\n                autocomplete={idx === 0 ? 'one-time-code' : 'off'}\n                maxlength={1}\n                disabled={props.disabled}\n                value={ch}\n                aria-label={`Digit ${idx + 1} of ${len}`}\n                class=\"otp-cell\"\n                onInput={(e: Event) => onInput(idx, e)}\n                onKeyDown={(e: KeyboardEvent) => onKeyDown(idx, e)}\n                on:paste={onPaste}\n              />\n            </span>\n          ))}\n        </div>\n        {props.hint ? <span class=\"otp-hint\">{props.hint}</span> : null}\n      </div>\n    );\n  }\n}\n"
    }
  ]
}
