{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "otp-field",
  "title": "OTP Field",
  "description": "A one-time code input with a slot per digit, a tick per keystroke, a cascade on paste, and a shake when the code is wrong.",
  "dependencies": [
    "@rexa-developer/tiks",
    "lucide-react",
    "motion",
    "web-haptics"
  ],
  "files": [
    {
      "path": "src/registry/components/otp-field/otp-field.tsx",
      "content": "\"use client\";\n\nimport { useTiks } from \"@rexa-developer/tiks/react\";\nimport { Check } from \"lucide-react\";\nimport {\n\tAnimatePresence,\n\tanimate,\n\tmotion,\n\tuseReducedMotion,\n\ttype Variants,\n} from \"motion/react\";\nimport {\n\ttype ChangeEvent,\n\ttype ClipboardEvent,\n\tFragment,\n\ttype KeyboardEvent,\n\ttype MouseEvent,\n\ttype PointerEvent,\n\ttype SyntheticEvent,\n\tuseEffect,\n\tuseId,\n\tuseLayoutEffect,\n\tuseRef,\n\tuseState,\n} from \"react\";\nimport { useWebHaptics } from \"web-haptics/react\";\nimport { cn } from \"@/lib/utils\";\n\nconst riseSpring = { type: \"spring\", stiffness: 600, damping: 30 } as const;\nconst ringSpring = { type: \"spring\", stiffness: 500, damping: 40 } as const;\n\n/** Ms between digits landing on a paste or autofill. */\nconst CASCADE_STAGGER_MS = 30;\n/** Ms between slots popping on success. */\nconst SUCCESS_STAGGER_MS = 40;\n/** Ms between digits dropping out, right to left, after an error. */\nconst DROP_STAGGER_MS = 30;\n/** Ms an error shows before the code clears. */\nconst ERROR_CLEAR_MS = 600;\n/** Minimum ms between typing and delete sounds and haptics. */\nconst CUE_THROTTLE_MS = 35;\n/** Volume of the delete click; typed digits play at the tiks default of 0.3. */\nconst DELETE_VOLUME = 0.15;\n/** Px offsets of the error shake, played over 400 ms. */\nconst SHAKE = [0, -8, 8, -6, 6, -3, 0];\n\n// Hoisted and deduplicated by React, so many fields share one sheet. The\n// selection rule outranks a global `::selection` color on the hidden text.\nconst STYLES =\n\t\"@keyframes otp-field-caret-blink{0%,70%,100%{opacity:1}20%,50%{opacity:0}}\" +\n\t\"[data-slot=otp-field-input]::selection{background:transparent;color:transparent;-webkit-text-fill-color:transparent}\";\n\n/** Whitespace and dashes that pasted or autofilled codes often carry. */\nconst SEPARATORS = /[\\s\\-‐‑‒–—−]/g;\n\nconst patterns = {\n\tdigits: /^\\d$/,\n\talphanumeric: /^[a-z\\d]$/i,\n} as const;\n\nconst sizes = {\n\tsm: {\n\t\tslot: \"w-8 rounded-md text-[clamp(0.625rem,44cqw,0.875rem)]\",\n\t\tgap: \"gap-1.5\",\n\t\tcaret: \"h-3.5\",\n\t\tseparator: \"w-2\",\n\t\tcheck: \"size-4\",\n\t},\n\tmd: {\n\t\tslot: \"w-10 rounded-lg text-[clamp(0.75rem,45cqw,1.125rem)]\",\n\t\tgap: \"gap-2\",\n\t\tcaret: \"h-5\",\n\t\tseparator: \"w-2.5\",\n\t\tcheck: \"size-5\",\n\t},\n\tlg: {\n\t\tslot: \"w-12 rounded-lg text-[clamp(0.875rem,50cqw,1.5rem)]\",\n\t\tgap: \"gap-2.5\",\n\t\tcaret: \"h-6\",\n\t\tseparator: \"w-3\",\n\t\tcheck: \"size-6\",\n\t},\n} as const;\n\nconst statusSlot = {\n\tidle: \"\",\n\tverifying: \"text-muted-foreground\",\n\tsuccess: \"border-success/50 bg-success/10 text-success dark:bg-success/15\",\n\terror:\n\t\t\"border-destructive/60 bg-destructive/5 text-destructive dark:bg-destructive/10\",\n} as const;\n\nconst announcements = {\n\tidle: \"\",\n\tverifying: \"Verifying code\",\n\tsuccess: \"Code verified\",\n\terror: \"Incorrect code, try again\",\n} as const;\n\nexport type OTPFieldStatus = keyof typeof announcements;\nexport type OTPFieldPattern = keyof typeof patterns | RegExp;\n\n/** How the digits of the latest value arrived. */\ntype EntryMode = \"type\" | \"cascade\" | \"drop\";\n\ntype CharMotion = {\n\treduce: boolean;\n\t/** Seconds before an entering digit rises. */\n\tdelay: number;\n\tdrop: boolean;\n\t/** Seconds before an exiting digit drops. */\n\tdropDelay: number;\n};\n\nconst charVariants: Variants = {\n\thidden: ({ reduce }: CharMotion) =>\n\t\treduce ? { opacity: 0 } : { opacity: 0, y: 6, scale: 0.85 },\n\tshown: ({ reduce, delay }: CharMotion) => ({\n\t\topacity: 1,\n\t\ty: 0,\n\t\tscale: 1,\n\t\ttransition: reduce ? { duration: 0 } : { ...riseSpring, delay },\n\t}),\n\texit: ({ reduce, drop, dropDelay }: CharMotion) => {\n\t\tif (reduce) return { opacity: 0, transition: { duration: 0 } };\n\t\tif (drop) {\n\t\t\treturn {\n\t\t\t\topacity: 0,\n\t\t\t\ty: 10,\n\t\t\t\ttransition: {\n\t\t\t\t\tduration: 0.2,\n\t\t\t\t\tease: [0.23, 1, 0.32, 1],\n\t\t\t\t\tdelay: dropDelay,\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t\treturn {\n\t\t\topacity: 0,\n\t\t\tscale: 0.85,\n\t\t\ttransition: { duration: 0.1, ease: \"easeOut\" },\n\t\t};\n\t},\n};\n\nexport interface OTPFieldProps {\n\t/** Controlled value. Characters outside `pattern` are dropped for display. */\n\tvalue?: string;\n\t/** Initial value when uncontrolled. */\n\tdefaultValue?: string;\n\t/** Called on every edit: typing, deleting, pasting, autofill, and the clear after an error. */\n\tonValueChange?: (value: string) => void;\n\t/** Called once each time the code becomes complete. */\n\tonComplete?: (value: string) => void;\n\t/** Number of slots. */\n\tlength?: number;\n\t/** Slots per group, e.g. `[3, 3]`, with a separator between groups. Must add up to `length`. */\n\tgroups?: number[];\n\t/** Characters a slot accepts: `\"digits\"`, `\"alphanumeric\"`, or a RegExp that matches one allowed character. */\n\tpattern?: OTPFieldPattern;\n\t/** `verifying` shimmers and pauses typing, `success` locks the code, and `error` shakes and clears it. */\n\tstatus?: OTPFieldStatus;\n\t/** Clear the code and refocus the first slot 600 ms after `status` turns `error`. */\n\tclearOnError?: boolean;\n\t/** Show a dot in place of each character. */\n\tmask?: boolean;\n\tdisabled?: boolean;\n\tautoFocus?: boolean;\n\t/** Submits the code with a native form through a hidden input. */\n\tname?: string;\n\t/** Id of the input, for a `<label htmlFor>`. */\n\tid?: string;\n\tsize?: keyof typeof sizes;\n\t/** Play a click per character, a pop on paste, and a tone on success and error. */\n\tsound?: boolean;\n\t/** Trigger haptics on supported devices. */\n\thaptics?: boolean;\n\tclassName?: string;\n\t/** Classes for every slot. */\n\tslotClassName?: string;\n\t/** Accessible name for the input. Defaults to \"One-time code\" unless `aria-labelledby` is set. */\n\t\"aria-label\"?: string;\n\t\"aria-labelledby\"?: string;\n\t/** Id of helper or error text read with the input. */\n\t\"aria-describedby\"?: string;\n}\n\nfunction isAllowed(char: string, pattern: OTPFieldPattern) {\n\tconst test = typeof pattern === \"string\" ? patterns[pattern] : pattern;\n\t// A global or sticky RegExp keeps state between tests.\n\ttest.lastIndex = 0;\n\treturn test.test(char);\n}\n\n/** Drops whitespace and dashes, keeps allowed characters, and truncates. */\nfunction sanitize(text: string, pattern: OTPFieldPattern, length: number) {\n\treturn Array.from(text.replace(SEPARATORS, \"\"))\n\t\t.filter((char) => isAllowed(char, pattern))\n\t\t.slice(0, length)\n\t\t.join(\"\");\n}\n\n/** Indexes of the last slot in every group but the final one. */\nfunction groupEnds(groups: number[] | undefined, length: number) {\n\tconst ends = new Set<number>();\n\tif (!groups || groups.length < 2) return ends;\n\tconst valid =\n\t\tgroups.every((size) => Number.isInteger(size) && size > 0) &&\n\t\tgroups.reduce((total, size) => total + size, 0) === length;\n\tif (!valid) return ends;\n\tlet total = 0;\n\tfor (const size of groups.slice(0, -1)) {\n\t\ttotal += size;\n\t\tends.add(total - 1);\n\t}\n\treturn ends;\n}\n\n/** Selects the character in a filled slot so typing replaces it, or puts a caret in an empty one. */\nfunction selectSlot(input: HTMLInputElement, index: number, filled: number) {\n\tif (document.activeElement !== input) return;\n\tconst end = index < filled ? index + 1 : index;\n\tif (input.selectionStart !== index || input.selectionEnd !== end) {\n\t\tinput.setSelectionRange(index, end);\n\t}\n}\n\nexport function OTPField({\n\tvalue: valueProp,\n\tdefaultValue = \"\",\n\tonValueChange,\n\tonComplete,\n\tlength: lengthProp = 6,\n\tgroups,\n\tpattern = \"digits\",\n\tstatus = \"idle\",\n\tclearOnError = true,\n\tmask = false,\n\tdisabled = false,\n\tautoFocus = false,\n\tname,\n\tid,\n\tsize = \"md\",\n\tsound = true,\n\thaptics = true,\n\tclassName,\n\tslotClassName,\n\t\"aria-label\": ariaLabel,\n\t\"aria-labelledby\": ariaLabelledBy,\n\t\"aria-describedby\": ariaDescribedBy,\n}: OTPFieldProps) {\n\tconst length = Math.max(1, Math.round(lengthProp));\n\tconst last = length - 1;\n\tconst locked = status === \"success\" || status === \"verifying\";\n\tconst interactive = !disabled && !locked;\n\n\tconst controlled = valueProp !== undefined;\n\tconst [internalValue, setInternalValue] = useState(() =>\n\t\tsanitize(defaultValue, pattern, length),\n\t);\n\tconst value = sanitize(\n\t\tcontrolled ? valueProp : internalValue,\n\t\tpattern,\n\t\tlength,\n\t);\n\n\tconst [caret, setCaret] = useState(0);\n\tconst [focused, setFocused] = useState(false);\n\tconst [entry, setEntry] = useState<{ value: string; mode: EntryMode }>({\n\t\tvalue,\n\t\tmode: \"type\",\n\t});\n\tconst [announcement, setAnnouncement] = useState(\"\");\n\n\t// The caret never sits past the first empty slot.\n\tconst active = Math.min(caret, value.length, last);\n\t// A value from props, not from input, enters without a cascade.\n\tconst entryMode = entry.value === value ? entry.mode : \"type\";\n\n\tconst shouldReduceMotion = useReducedMotion() ?? false;\n\tconst { trigger: haptic } = useWebHaptics();\n\tconst {\n\t\tclick: playClick,\n\t\tpop: playPop,\n\t\tsuccess: playSuccess,\n\t\terror: playError,\n\t} = useTiks({\n\t\t// Sound stays on under reduced motion; only movement is reduced.\n\t\trespectReducedMotion: false,\n\t});\n\tconst { click: playDeleteClick } = useTiks({\n\t\trespectReducedMotion: false,\n\t\tvolume: DELETE_VOLUME,\n\t});\n\n\tconst generatedId = useId();\n\tconst inputId = id ?? `${generatedId}-input`;\n\tconst ringLayoutId = `${generatedId}-ring`;\n\n\tconst inputRef = useRef<HTMLInputElement>(null);\n\tconst rowRef = useRef<HTMLDivElement>(null);\n\tconst slotRefs = useRef<(HTMLDivElement | null)[]>([]);\n\tconst valueRef = useRef(value);\n\tconst activeRef = useRef(active);\n\tconst completedRef = useRef<string | null>(\n\t\tvalue.length === length ? value : null,\n\t);\n\tconst lastCueAtRef = useRef(Number.NEGATIVE_INFINITY);\n\tconst pointerRef = useRef(false);\n\tconst pointerSlotRef = useRef<number | null>(null);\n\tconst statusRef = useRef(status);\n\tconst clearTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);\n\n\tuseLayoutEffect(() => {\n\t\tvalueRef.current = value;\n\t\tactiveRef.current = active;\n\t\tif (value.length < length) completedRef.current = null;\n\t});\n\n\t// Keep the native selection on the active slot after every render.\n\tuseLayoutEffect(() => {\n\t\tconst input = inputRef.current;\n\t\tif (focused && input) selectSlot(input, active, value.length);\n\t}, [active, focused, value.length]);\n\n\tuseEffect(() => {\n\t\tif (autoFocus) inputRef.current?.focus();\n\t}, [autoFocus]);\n\n\tconst throttled = () => {\n\t\tconst now = performance.now();\n\t\tif (now - lastCueAtRef.current < CUE_THROTTLE_MS) return true;\n\t\tlastCueAtRef.current = now;\n\t\treturn false;\n\t};\n\n\tconst typeCue = () => {\n\t\tif (throttled()) return;\n\t\tif (sound) playClick();\n\t\tif (haptics) void haptic(\"selection\");\n\t};\n\n\tconst deleteCue = () => {\n\t\tif (throttled()) return;\n\t\tif (sound) playDeleteClick();\n\t\tif (haptics) void haptic(\"selection\");\n\t};\n\n\tconst fillCue = () => {\n\t\tlastCueAtRef.current = performance.now();\n\t\tif (sound) playPop();\n\t\tif (haptics) void haptic(\"light\");\n\t};\n\n\t// A rejected character is felt, not heard.\n\tconst rejectCue = () => {\n\t\tif (throttled()) return;\n\t\tif (haptics) void haptic(\"soft\");\n\t};\n\n\tconst cancelClear = () => {\n\t\tif (clearTimerRef.current === null) return;\n\t\tclearTimeout(clearTimerRef.current);\n\t\tclearTimerRef.current = null;\n\t};\n\n\t/** Moves the caret, clamped to the first empty slot. */\n\tconst moveCaret = (index: number) => {\n\t\tconst next = Math.max(0, Math.min(index, valueRef.current.length, last));\n\t\tactiveRef.current = next;\n\t\tsetCaret(next);\n\t\treturn next;\n\t};\n\n\t/** Re-applies the selection once React restores the input's value. */\n\tconst restoreSelection = (input: HTMLInputElement) => {\n\t\tqueueMicrotask(() =>\n\t\t\tselectSlot(input, activeRef.current, valueRef.current.length),\n\t\t);\n\t};\n\n\tconst emit = (next: string, mode: EntryMode) => {\n\t\tif (next === valueRef.current) return false;\n\t\tcancelClear();\n\t\tvalueRef.current = next;\n\t\tsetEntry({ value: next, mode });\n\t\tif (!controlled) setInternalValue(next);\n\t\tonValueChange?.(next);\n\t\tif (next.length < length) {\n\t\t\tcompletedRef.current = null;\n\t\t} else if (completedRef.current !== next) {\n\t\t\tcompletedRef.current = next;\n\t\t\tonComplete?.(next);\n\t\t}\n\t\treturn true;\n\t};\n\n\tconst typeChar = (char: string) => {\n\t\tif (!isAllowed(char, pattern)) {\n\t\t\trejectCue();\n\t\t\treturn;\n\t\t}\n\t\tconst current = valueRef.current;\n\t\tconst index = activeRef.current;\n\t\temit(current.slice(0, index) + char + current.slice(index + 1), \"type\");\n\t\ttypeCue();\n\t\tmoveCaret(index + 1);\n\t};\n\n\t/** Fills from the first slot, as paste and autofill do. */\n\tconst fill = (text: string) => {\n\t\tconst next = sanitize(text, pattern, length);\n\t\tif (next === \"\") {\n\t\t\trejectCue();\n\t\t\treturn;\n\t\t}\n\t\tif (emit(next, \"cascade\")) fillCue();\n\t\tmoveCaret(next.length);\n\t};\n\n\t/** Clears the selected slot, or the one before an empty caret. */\n\tconst deleteBackward = () => {\n\t\tconst current = valueRef.current;\n\t\tconst index = activeRef.current;\n\t\tconst at = index < current.length ? index : index - 1;\n\t\tif (at < 0) return;\n\t\temit(current.slice(0, at) + current.slice(at + 1), \"type\");\n\t\tdeleteCue();\n\t\tmoveCaret(at);\n\t};\n\n\tconst deleteForward = () => {\n\t\tconst current = valueRef.current;\n\t\tconst index = activeRef.current;\n\t\tif (index >= current.length) return;\n\t\temit(current.slice(0, index) + current.slice(index + 1), \"type\");\n\t\tdeleteCue();\n\t\tmoveCaret(index);\n\t};\n\n\tconst slotAt = (clientX: number) => {\n\t\tlet nearest = 0;\n\t\tlet nearestDistance = Number.POSITIVE_INFINITY;\n\t\tfor (let index = 0; index < length; index++) {\n\t\t\tconst slot = slotRefs.current[index];\n\t\t\tif (!slot) continue;\n\t\t\tconst rect = slot.getBoundingClientRect();\n\t\t\tconst distance = Math.max(rect.left - clientX, clientX - rect.right, 0);\n\t\t\tif (distance < nearestDistance) {\n\t\t\t\tnearest = index;\n\t\t\t\tnearestDistance = distance;\n\t\t\t}\n\t\t}\n\t\treturn nearest;\n\t};\n\n\tconst handleChange = (event: ChangeEvent<HTMLInputElement>) => {\n\t\tconst input = event.currentTarget;\n\t\trestoreSelection(input);\n\t\tif (!interactive) return;\n\t\tconst native = event.nativeEvent as InputEvent;\n\t\tconst inputType = native.inputType ?? \"\";\n\t\tconst data = native.data ?? \"\";\n\n\t\t// Backspace from a virtual keyboard that skips `keydown`.\n\t\tif (inputType.startsWith(\"delete\")) {\n\t\t\tif (inputType.endsWith(\"Forward\")) deleteForward();\n\t\t\telse deleteBackward();\n\t\t\treturn;\n\t\t}\n\t\tif (inputType === \"insertText\" && Array.from(data).length === 1) {\n\t\t\ttypeChar(data);\n\t\t\treturn;\n\t\t}\n\t\t// An IME composes in place, so take what the field now holds.\n\t\tif (inputType === \"insertCompositionText\") {\n\t\t\tconst next = sanitize(input.value, pattern, length);\n\t\t\tif (emit(next, \"type\")) typeCue();\n\t\t\tmoveCaret(next.length);\n\t\t\treturn;\n\t\t}\n\t\t// Paste without a clipboard event, drop, SMS and password manager\n\t\t// autofill, and multi-character inserts all fill from the start.\n\t\tfill(data || input.value);\n\t};\n\n\tconst handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {\n\t\tpointerRef.current = false;\n\t\tif (disabled) return;\n\t\tconst index = activeRef.current;\n\t\tswitch (event.key) {\n\t\t\tcase \"ArrowLeft\":\n\t\t\t\tevent.preventDefault();\n\t\t\t\tif (interactive) moveCaret(index - 1);\n\t\t\t\treturn;\n\t\t\tcase \"ArrowRight\":\n\t\t\t\tevent.preventDefault();\n\t\t\t\tif (interactive) moveCaret(index + 1);\n\t\t\t\treturn;\n\t\t\tcase \"Home\":\n\t\t\tcase \"ArrowUp\":\n\t\t\t\tevent.preventDefault();\n\t\t\t\tif (interactive) moveCaret(0);\n\t\t\t\treturn;\n\t\t\tcase \"End\":\n\t\t\tcase \"ArrowDown\":\n\t\t\t\tevent.preventDefault();\n\t\t\t\tif (interactive) moveCaret(length);\n\t\t\t\treturn;\n\t\t\tcase \"Backspace\":\n\t\t\t\tevent.preventDefault();\n\t\t\t\tif (interactive) deleteBackward();\n\t\t\t\treturn;\n\t\t\tcase \"Delete\":\n\t\t\t\tevent.preventDefault();\n\t\t\t\tif (interactive) deleteForward();\n\t\t\t\treturn;\n\t\t}\n\t};\n\n\tconst handlePaste = (event: ClipboardEvent<HTMLInputElement>) => {\n\t\tevent.preventDefault();\n\t\tif (interactive) fill(event.clipboardData.getData(\"text\"));\n\t};\n\n\t// The browser places its own caret by the invisible text, so a tap or\n\t// click picks the slot under the pointer instead.\n\tconst handlePointerDown = (event: PointerEvent<HTMLInputElement>) => {\n\t\tpointerRef.current = true;\n\t\tpointerSlotRef.current = slotAt(event.clientX);\n\t};\n\n\tconst handleClick = (event: MouseEvent<HTMLInputElement>) => {\n\t\tpointerRef.current = false;\n\t\tpointerSlotRef.current = null;\n\t\tif (!interactive) return;\n\t\tconst index = moveCaret(slotAt(event.clientX));\n\t\tselectSlot(event.currentTarget, index, valueRef.current.length);\n\t};\n\n\t// Anything else that moves the selection, like select-all or a long\n\t// press, snaps back to a slot. React can report a selection read before\n\t// a key handler moved the caret, so check once the frame has settled.\n\tconst handleSelect = (event: SyntheticEvent<HTMLInputElement>) => {\n\t\tif (pointerRef.current) return;\n\t\tconst input = event.currentTarget;\n\t\trequestAnimationFrame(() => {\n\t\t\tif (pointerRef.current || document.activeElement !== input) return;\n\t\t\tconst filled = valueRef.current.length;\n\t\t\tconst index = activeRef.current;\n\t\t\tconst end = index < filled ? index + 1 : index;\n\t\t\tif (input.selectionStart === index && input.selectionEnd === end) return;\n\t\t\tselectSlot(input, moveCaret(input.selectionStart ?? filled), filled);\n\t\t});\n\t};\n\n\tconst handleFocus = () => {\n\t\tsetFocused(true);\n\t\tmoveCaret(pointerSlotRef.current ?? valueRef.current.length);\n\t};\n\n\tconst handleBlur = () => {\n\t\tsetFocused(false);\n\t\tpointerRef.current = false;\n\t\tpointerSlotRef.current = null;\n\t};\n\n\tconst clearAfterError = () => {\n\t\tclearTimerRef.current = null;\n\t\temit(\"\", \"drop\");\n\t\tmoveCaret(0);\n\t\tif (!disabled) inputRef.current?.focus({ preventScroll: true });\n\t};\n\n\tconst enterStatus = (next: OTPFieldStatus) => {\n\t\tsetAnnouncement(announcements[next]);\n\t\tif (disabled) return;\n\t\tif (next === \"success\") {\n\t\t\tif (sound) playSuccess();\n\t\t\tif (haptics) void haptic(\"success\");\n\t\t\tif (shouldReduceMotion) return;\n\t\t\tslotRefs.current.slice(0, length).forEach((slot, index) => {\n\t\t\t\tif (!slot) return;\n\t\t\t\tanimate(\n\t\t\t\t\tslot,\n\t\t\t\t\t{ scale: [1, 1.06, 1] },\n\t\t\t\t\t{\n\t\t\t\t\t\tduration: 0.3,\n\t\t\t\t\t\tdelay: (index * SUCCESS_STAGGER_MS) / 1000,\n\t\t\t\t\t\tease: \"easeOut\",\n\t\t\t\t\t},\n\t\t\t\t);\n\t\t\t});\n\t\t} else if (next === \"error\") {\n\t\t\tif (sound) playError();\n\t\t\tif (haptics) void haptic(\"error\");\n\t\t\tif (!shouldReduceMotion && rowRef.current) {\n\t\t\t\tanimate(\n\t\t\t\t\trowRef.current,\n\t\t\t\t\t{ x: SHAKE },\n\t\t\t\t\t{ duration: 0.4, ease: \"easeOut\" },\n\t\t\t\t);\n\t\t\t}\n\t\t\tif (clearOnError) {\n\t\t\t\tclearTimerRef.current = setTimeout(\n\t\t\t\t\t() => clearAfterErrorRef.current(),\n\t\t\t\t\tERROR_CLEAR_MS,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t};\n\n\t// Timers and effects call through refs so they always see the latest props.\n\tconst enterStatusRef = useRef(enterStatus);\n\tconst clearAfterErrorRef = useRef(clearAfterError);\n\tuseLayoutEffect(() => {\n\t\tenterStatusRef.current = enterStatus;\n\t\tclearAfterErrorRef.current = clearAfterError;\n\t});\n\n\t// Status cues play on a change, never for the status a field mounts with.\n\tuseEffect(() => {\n\t\tif (statusRef.current === status) return;\n\t\tstatusRef.current = status;\n\t\tenterStatusRef.current(status);\n\t\treturn () => {\n\t\t\tif (clearTimerRef.current === null) return;\n\t\t\tclearTimeout(clearTimerRef.current);\n\t\t\tclearTimerRef.current = null;\n\t\t};\n\t}, [status]);\n\n\tconst sizeStyle = sizes[size];\n\tconst ends = groupEnds(groups, length);\n\tconst chars = Array.from(value);\n\tconst showCaret = focused && interactive;\n\n\treturn (\n\t\t<div\n\t\t\tdir=\"ltr\"\n\t\t\tdata-slot=\"otp-field\"\n\t\t\tdata-status={status}\n\t\t\tdata-size={size}\n\t\t\tdata-complete={value.length === length || undefined}\n\t\t\tdata-disabled={disabled || undefined}\n\t\t\tclassName={cn(\n\t\t\t\t\"relative inline-flex w-fit max-w-full data-disabled:cursor-not-allowed data-disabled:opacity-50\",\n\t\t\t\tclassName,\n\t\t\t)}\n\t\t>\n\t\t\t<style href=\"tentui-otp-field\" precedence=\"default\">\n\t\t\t\t{STYLES}\n\t\t\t</style>\n\t\t\t<div\n\t\t\t\tref={rowRef}\n\t\t\t\tdata-slot=\"otp-field-row\"\n\t\t\t\tclassName={cn(\n\t\t\t\t\t\"relative flex min-w-0 max-w-full items-center\",\n\t\t\t\t\tsizeStyle.gap,\n\t\t\t\t)}\n\t\t\t>\n\t\t\t\t{Array.from({ length }, (_, index) => {\n\t\t\t\t\tconst char = chars[index];\n\t\t\t\t\tconst isActive = showCaret && index === active;\n\t\t\t\t\tconst custom: CharMotion = {\n\t\t\t\t\t\treduce: shouldReduceMotion,\n\t\t\t\t\t\tdelay:\n\t\t\t\t\t\t\tentryMode === \"cascade\" ? (index * CASCADE_STAGGER_MS) / 1000 : 0,\n\t\t\t\t\t\tdrop: entryMode === \"drop\",\n\t\t\t\t\t\tdropDelay: ((last - index) * DROP_STAGGER_MS) / 1000,\n\t\t\t\t\t};\n\t\t\t\t\treturn (\n\t\t\t\t\t\t<Fragment key={index}>\n\t\t\t\t\t\t\t<div\n\t\t\t\t\t\t\t\tref={(node) => {\n\t\t\t\t\t\t\t\t\tslotRefs.current[index] = node;\n\t\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\t\taria-hidden\n\t\t\t\t\t\t\t\tdata-slot=\"otp-field-slot\"\n\t\t\t\t\t\t\t\tdata-active={isActive || undefined}\n\t\t\t\t\t\t\t\tdata-filled={char !== undefined || undefined}\n\t\t\t\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\t\t\t\"@container relative flex aspect-square min-w-0 shrink items-center justify-center border border-input bg-background font-medium text-foreground tabular-nums shadow-xs transition-[color,background-color,border-color] duration-200 motion-reduce:transition-none dark:bg-input/30\",\n\t\t\t\t\t\t\t\t\tsizeStyle.slot,\n\t\t\t\t\t\t\t\t\tstatusSlot[status],\n\t\t\t\t\t\t\t\t\tslotClassName,\n\t\t\t\t\t\t\t\t)}\n\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t<AnimatePresence initial={false} custom={custom}>\n\t\t\t\t\t\t\t\t\t{char === undefined ? null : (\n\t\t\t\t\t\t\t\t\t\t<motion.span\n\t\t\t\t\t\t\t\t\t\t\tkey={char}\n\t\t\t\t\t\t\t\t\t\t\tcustom={custom}\n\t\t\t\t\t\t\t\t\t\t\tvariants={charVariants}\n\t\t\t\t\t\t\t\t\t\t\tinitial=\"hidden\"\n\t\t\t\t\t\t\t\t\t\t\tanimate=\"shown\"\n\t\t\t\t\t\t\t\t\t\t\texit=\"exit\"\n\t\t\t\t\t\t\t\t\t\t\tclassName=\"absolute inset-0 grid place-items-center\"\n\t\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t\t\t{mask ? (\n\t\t\t\t\t\t\t\t\t\t\t\t<span className=\"size-[0.4em] rounded-full bg-current\" />\n\t\t\t\t\t\t\t\t\t\t\t) : (\n\t\t\t\t\t\t\t\t\t\t\t\tchar\n\t\t\t\t\t\t\t\t\t\t\t)}\n\t\t\t\t\t\t\t\t\t\t</motion.span>\n\t\t\t\t\t\t\t\t\t)}\n\t\t\t\t\t\t\t\t</AnimatePresence>\n\t\t\t\t\t\t\t\t{isActive && char === undefined ? (\n\t\t\t\t\t\t\t\t\t<span\n\t\t\t\t\t\t\t\t\t\tdata-slot=\"otp-field-caret\"\n\t\t\t\t\t\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\t\t\t\t\t\"pointer-events-none absolute w-0.5 animate-[otp-field-caret-blink_1.2s_ease-out_infinite] rounded-full bg-foreground motion-reduce:animate-none\",\n\t\t\t\t\t\t\t\t\t\t\tsizeStyle.caret,\n\t\t\t\t\t\t\t\t\t\t)}\n\t\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t\t) : null}\n\t\t\t\t\t\t\t\t{isActive ? (\n\t\t\t\t\t\t\t\t\t<motion.span\n\t\t\t\t\t\t\t\t\t\tlayoutId={ringLayoutId}\n\t\t\t\t\t\t\t\t\t\ttransition={\n\t\t\t\t\t\t\t\t\t\t\tshouldReduceMotion ? { duration: 0 } : ringSpring\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\tdata-slot=\"otp-field-ring\"\n\t\t\t\t\t\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\t\t\t\t\t\"pointer-events-none absolute -inset-px rounded-[inherit] border border-ring ring-3 ring-ring/50\",\n\t\t\t\t\t\t\t\t\t\t\tstatus === \"error\" &&\n\t\t\t\t\t\t\t\t\t\t\t\t\"border-destructive ring-destructive/20 dark:ring-destructive/40\",\n\t\t\t\t\t\t\t\t\t\t)}\n\t\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t\t) : null}\n\t\t\t\t\t\t\t\t{status === \"verifying\" && !shouldReduceMotion ? (\n\t\t\t\t\t\t\t\t\t<span className=\"pointer-events-none absolute inset-0 overflow-hidden rounded-[inherit]\">\n\t\t\t\t\t\t\t\t\t\t<motion.span\n\t\t\t\t\t\t\t\t\t\t\tdata-slot=\"otp-field-shimmer\"\n\t\t\t\t\t\t\t\t\t\t\tclassName=\"absolute inset-y-0 -start-full w-full bg-linear-to-r from-transparent via-foreground/10 to-transparent\"\n\t\t\t\t\t\t\t\t\t\t\tinitial={{ x: \"0%\" }}\n\t\t\t\t\t\t\t\t\t\t\tanimate={{ x: \"200%\" }}\n\t\t\t\t\t\t\t\t\t\t\ttransition={{\n\t\t\t\t\t\t\t\t\t\t\t\tduration: 0.9,\n\t\t\t\t\t\t\t\t\t\t\t\tease: \"easeInOut\",\n\t\t\t\t\t\t\t\t\t\t\t\trepeat: Number.POSITIVE_INFINITY,\n\t\t\t\t\t\t\t\t\t\t\t\trepeatDelay: 0.4,\n\t\t\t\t\t\t\t\t\t\t\t\tdelay: index * 0.07,\n\t\t\t\t\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t\t\t</span>\n\t\t\t\t\t\t\t\t) : null}\n\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t{ends.has(index) ? (\n\t\t\t\t\t\t\t\t<span\n\t\t\t\t\t\t\t\t\taria-hidden\n\t\t\t\t\t\t\t\t\tdata-slot=\"otp-field-separator\"\n\t\t\t\t\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\t\t\t\t\"h-0.5 min-w-1 shrink rounded-full bg-border\",\n\t\t\t\t\t\t\t\t\t\tsizeStyle.separator,\n\t\t\t\t\t\t\t\t\t)}\n\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t) : null}\n\t\t\t\t\t\t</Fragment>\n\t\t\t\t\t);\n\t\t\t\t})}\n\t\t\t\t<input\n\t\t\t\t\tref={inputRef}\n\t\t\t\t\tid={inputId}\n\t\t\t\t\ttype=\"text\"\n\t\t\t\t\tinputMode={pattern === \"digits\" ? \"numeric\" : \"text\"}\n\t\t\t\t\tautoComplete=\"one-time-code\"\n\t\t\t\t\tautoCorrect=\"off\"\n\t\t\t\t\tautoCapitalize=\"off\"\n\t\t\t\t\tspellCheck={false}\n\t\t\t\t\taria-label={\n\t\t\t\t\t\tariaLabelledBy ? ariaLabel : (ariaLabel ?? \"One-time code\")\n\t\t\t\t\t}\n\t\t\t\t\taria-labelledby={ariaLabelledBy}\n\t\t\t\t\taria-describedby={ariaDescribedBy}\n\t\t\t\t\taria-invalid={status === \"error\" || undefined}\n\t\t\t\t\tdisabled={disabled}\n\t\t\t\t\treadOnly={locked}\n\t\t\t\t\tvalue={value}\n\t\t\t\t\tonChange={handleChange}\n\t\t\t\t\tonKeyDown={handleKeyDown}\n\t\t\t\t\tonPaste={handlePaste}\n\t\t\t\t\tonSelect={handleSelect}\n\t\t\t\t\tonPointerDown={handlePointerDown}\n\t\t\t\t\tonPointerCancel={() => {\n\t\t\t\t\t\tpointerRef.current = false;\n\t\t\t\t\t}}\n\t\t\t\t\tonClick={handleClick}\n\t\t\t\t\tonFocus={handleFocus}\n\t\t\t\t\tonBlur={handleBlur}\n\t\t\t\t\tdata-slot=\"otp-field-input\"\n\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t// 16px text keeps iOS from zooming in on focus. The text, caret,\n\t\t\t\t\t\t// and autofill tint stay invisible over the slots.\n\t\t\t\t\t\t\"absolute inset-0 size-full min-w-0 cursor-text bg-transparent text-base text-transparent caret-transparent outline-none disabled:cursor-not-allowed autofill:[transition:background-color_0s_600000s]\",\n\t\t\t\t\t\tmask && \"[-webkit-text-security:disc]\",\n\t\t\t\t\t\tlocked && \"cursor-default\",\n\t\t\t\t\t)}\n\t\t\t\t/>\n\t\t\t</div>\n\t\t\t<span\n\t\t\t\taria-hidden\n\t\t\t\tclassName=\"pointer-events-none absolute inset-y-0 start-full ms-2.5 flex items-center text-success\"\n\t\t\t>\n\t\t\t\t<AnimatePresence initial={false}>\n\t\t\t\t\t{status === \"success\" ? (\n\t\t\t\t\t\t<motion.span\n\t\t\t\t\t\t\tkey=\"check\"\n\t\t\t\t\t\t\tdata-slot=\"otp-field-check\"\n\t\t\t\t\t\t\tinitial={\n\t\t\t\t\t\t\t\tshouldReduceMotion ? { opacity: 0 } : { opacity: 0, scale: 0.6 }\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tanimate={{\n\t\t\t\t\t\t\t\topacity: 1,\n\t\t\t\t\t\t\t\tscale: 1,\n\t\t\t\t\t\t\t\ttransition: shouldReduceMotion\n\t\t\t\t\t\t\t\t\t? { duration: 0 }\n\t\t\t\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\t\t\t\t...riseSpring,\n\t\t\t\t\t\t\t\t\t\t\tdelay: (length * SUCCESS_STAGGER_MS) / 1000,\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\texit={{\n\t\t\t\t\t\t\t\topacity: 0,\n\t\t\t\t\t\t\t\ttransition: { duration: shouldReduceMotion ? 0 : 0.15 },\n\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\tclassName=\"grid place-items-center\"\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t<Check className={sizeStyle.check} strokeWidth={2.5} />\n\t\t\t\t\t\t</motion.span>\n\t\t\t\t\t) : null}\n\t\t\t\t</AnimatePresence>\n\t\t\t</span>\n\t\t\t{name ? (\n\t\t\t\t<input type=\"hidden\" name={name} value={value} disabled={disabled} />\n\t\t\t) : null}\n\t\t\t<span aria-live=\"polite\" aria-atomic className=\"sr-only\">\n\t\t\t\t{announcement}\n\t\t\t</span>\n\t\t</div>\n\t);\n}\n\nexport default OTPField;\n",
      "type": "registry:component",
      "target": "@components/otp-field.tsx"
    }
  ],
  "cssVars": {
    "theme": {
      "color-success": "var(--success)"
    },
    "light": {
      "success": "oklch(0.723 0.219 149.579)"
    },
    "dark": {
      "success": "oklch(0.723 0.219 149.579)"
    }
  },
  "meta": {
    "createdAt": "2026-10-26",
    "image": "https://cdn.tentui.com/images/components/otp-field?v=2",
    "video": "https://cdn.tentui.com/videos/components/otp-field?v=2"
  },
  "docs": "https://tentui.com/components/otp-field",
  "categories": [
    "inputs"
  ],
  "type": "registry:component"
}