{
  "name": "pin-input",
  "type": "registry:ui",
  "framework": "react",
  "title": "Pin Input",
  "description": "A short code entered one character to a box, for two-factor and verification codes.",
  "files": [
    {
      "path": "ui/pin-input.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\nexport interface PinInputProps extends Omit<\n  React.HTMLAttributes<HTMLDivElement>,\n  \"onChange\" | \"defaultValue\"\n> {\n  /** How many characters the code has. */\n  length?: number\n  /** Controlled code. Leave undefined to let the component own it. */\n  value?: string\n  defaultValue?: string\n  onValueChange?: (value: string) => void\n  /** Called once, with the code, when the last box is filled. */\n  onComplete?: (value: string) => void\n  /** `numeric` accepts digits only; `alphanumeric` also takes letters. */\n  type?: \"numeric\" | \"alphanumeric\"\n  /** Draw every character as a dot, for a code that is a secret. */\n  mask?: boolean\n  /** A separator after every this-many boxes. `0` draws none. */\n  groupSize?: number\n  disabled?: boolean\n  invalid?: boolean\n  /** Accessible name for the group of boxes. */\n  label?: string\n  /** Builds the accessible name of each box. */\n  formatCellLabel?: (state: PinInputCellLabelState) => string\n  /** Posts the code with a form, through a hidden input. */\n  name?: string\n}\n\n/** What the accessible name of one box is built from. */\nexport interface PinInputCellLabelState {\n  /** The place of the box, from 1. */\n  position: number\n  length: number\n  type: \"numeric\" | \"alphanumeric\"\n}\n\n/** The English default: `Digit 2 of 6`. Pass `formatCellLabel` to translate it. */\nexport function defaultPinInputCellLabel({\n  position,\n  length,\n  type,\n}: PinInputCellLabelState): string {\n  return `${type === \"numeric\" ? \"Digit\" : \"Character\"} ${position} of ${length}`\n}\n\n/* Global, so `match` returns every accepted character and drops the rest. */\nconst ACCEPT = {\n  numeric: /\\d/g,\n  alphanumeric: /[a-z0-9]/gi,\n} as const\n\n/** Where a navigation key sends focus, or `undefined` for any other key. */\nfunction moveFor(\n  key: string,\n  index: number,\n  forward: number,\n  length: number\n): number | undefined {\n  switch (key) {\n    case \"ArrowLeft\":\n      return index - forward\n    case \"ArrowRight\":\n      return index + forward\n    case \"Home\":\n      return 0\n    case \"End\":\n      return length - 1\n    default:\n      return undefined\n  }\n}\n\n/*\n * A short code, one character to a box. Each box is its own `<input>`, so a\n * screen reader can say which one it is on; the first carries\n * `one-time-code`, so a code from a text message fills all of them. The value\n * is one string with no gaps: a character typed past the end lands in the\n * first empty box.\n */\nconst PinInput = React.forwardRef<HTMLDivElement, PinInputProps>(\n  (\n    {\n      className,\n      length = 6,\n      value,\n      defaultValue = \"\",\n      onValueChange,\n      onComplete,\n      type = \"numeric\",\n      mask = false,\n      groupSize = 0,\n      disabled = false,\n      invalid = false,\n      label = \"Verification code\",\n      formatCellLabel = defaultPinInputCellLabel,\n      name,\n      ...props\n    },\n    ref\n  ) => {\n    const [internal, setInternal] = React.useState(defaultValue)\n    const isControlled = value !== undefined\n    const code = (isControlled ? value : internal).slice(0, length)\n    const cells = React.useRef<(HTMLInputElement | null)[]>([])\n\n    const focus = (index: number) => {\n      const cell = cells.current[Math.max(0, Math.min(length - 1, index))]\n      cell?.focus()\n      cell?.select()\n    }\n\n    const commit = (next: string) => {\n      if (!isControlled) setInternal(next)\n      onValueChange?.(next)\n      if (next.length === length && next !== code) onComplete?.(next)\n    }\n\n    const handleChange = (index: number, raw: string) => {\n      const existing = code[index] ?? \"\"\n      /* With the box's own character still in it, only the rest is new. */\n      const typed =\n        existing !== \"\" && raw.length > 1 && raw.startsWith(existing)\n          ? raw.slice(existing.length)\n          : raw\n      const chars = typed.match(ACCEPT[type]) ?? []\n      if (chars.length === 0) {\n        if (raw === \"\") commit(code.slice(0, index) + code.slice(index + 1))\n        return\n      }\n      const at = Math.min(index, code.length)\n      const next = (\n        code.slice(0, at) +\n        chars.join(\"\") +\n        code.slice(at + chars.length)\n      ).slice(0, length)\n      commit(next)\n      focus(at + chars.length)\n    }\n\n    /*\n     * A paste is handled on its own event. Read from the input's value it\n     * cannot be told apart from typing, and a pasted code that begins with\n     * the box's current character would lose that character.\n     */\n    const handlePaste = (\n      index: number,\n      event: React.ClipboardEvent<HTMLInputElement>\n    ) => {\n      event.preventDefault()\n      const chars =\n        event.clipboardData.getData(\"text\").match(ACCEPT[type]) ?? []\n      if (chars.length === 0) return\n      const at = Math.min(index, code.length)\n      commit(\n        (\n          code.slice(0, at) +\n          chars.join(\"\") +\n          code.slice(at + chars.length)\n        ).slice(0, length)\n      )\n      focus(at + chars.length)\n    }\n\n    const handleKeyDown = (\n      index: number,\n      event: React.KeyboardEvent<HTMLInputElement>\n    ) => {\n      const forward =\n        getComputedStyle(event.currentTarget).direction === \"rtl\" ? -1 : 1\n      const move = moveFor(event.key, index, forward, length)\n      if (move !== undefined) {\n        event.preventDefault()\n        focus(move)\n        return\n      }\n      if (\n        event.key === \"Backspace\" &&\n        (code[index] ?? \"\") === \"\" &&\n        index > 0\n      ) {\n        event.preventDefault()\n        commit(code.slice(0, index - 1) + code.slice(index))\n        focus(index - 1)\n      }\n    }\n\n    return (\n      <div\n        ref={ref}\n        role=\"group\"\n        aria-label={label}\n        data-invalid={invalid || undefined}\n        className={cn(\"group/pin inline-flex items-center gap-2\", className)}\n        {...props}\n      >\n        {Array.from({ length }, (_, index) => (\n          <React.Fragment key={index}>\n            {groupSize > 0 && index > 0 && index % groupSize === 0 ? (\n              <span\n                aria-hidden=\"true\"\n                className=\"h-0.5 w-2 rounded-full bg-newt-text-muted\"\n              />\n            ) : null}\n            <input\n              ref={(node) => {\n                cells.current[index] = node\n              }}\n              type={mask ? \"password\" : \"text\"}\n              inputMode={type === \"numeric\" ? \"numeric\" : \"text\"}\n              autoComplete={index === 0 ? \"one-time-code\" : \"off\"}\n              aria-label={formatCellLabel({\n                position: index + 1,\n                length,\n                type,\n              })}\n              aria-invalid={invalid || undefined}\n              disabled={disabled}\n              value={code[index] ?? \"\"}\n              onChange={(event) => handleChange(index, event.target.value)}\n              onKeyDown={(event) => handleKeyDown(index, event)}\n              onPaste={(event) => handlePaste(index, event)}\n              onFocus={(event) => event.target.select()}\n              className=\"h-[52px] w-11 rounded-[var(--newt-radius-sm)] border border-newt-border bg-newt-bg-input p-0 text-center font-[family-name:var(--newt-font-mono)] text-xl font-semibold text-newt-text-primary caret-newt-brand transition-colors duration-fast ease-newt focus:border-newt-brand focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 group-data-[invalid]/pin:border-newt-danger\"\n            />\n          </React.Fragment>\n        ))}\n        {name === undefined ? null : (\n          <input type=\"hidden\" name={name} value={code} />\n        )}\n      </div>\n    )\n  }\n)\nPinInput.displayName = \"PinInput\"\n\nexport { PinInput }\n",
      "type": "registry:ui"
    }
  ]
}