{
  "schemaVersion": 1,
  "target": "react",
  "name": "input-otp",
  "version": "0.1.0",
  "category": "forms",
  "title": "Input OTP",
  "description": "A one-time code input with one cell per character, grouped cells, paste support, a busy state, and a caret that respects reduced motion.",
  "tags": [
    "form",
    "otp",
    "verification",
    "code",
    "two-factor",
    "base-ui"
  ],
  "requires": {
    "projectTypes": [
      "next",
      "vite-react"
    ],
    "styling": "tailwind-v4"
  },
  "npmDependencies": {
    "@base-ui/react": "^1.9.0"
  },
  "registryDependencies": [],
  "docsPath": "/components/input-otp",
  "props": [
    {
      "name": "length",
      "type": "number",
      "default": "6",
      "description": "Number of characters."
    },
    {
      "name": "groups",
      "type": "number[]",
      "description": "How the cells split, for example [3, 3]; a dash separates groups."
    },
    {
      "name": "value / defaultValue",
      "type": "string",
      "description": "The code."
    },
    {
      "name": "onValueChange",
      "type": "(value: string) => void",
      "description": "Called on every change."
    },
    {
      "name": "onValueComplete",
      "type": "(value: string) => void",
      "description": "Called when the last cell is filled or a full code is pasted."
    },
    {
      "name": "validationType",
      "type": "\"numeric\" | \"alpha\" | \"alphanumeric\" | \"none\"",
      "default": "\"numeric\"",
      "description": "Which characters are accepted."
    },
    {
      "name": "label / description / error",
      "type": "ReactNode",
      "description": "Like Input: a bound label, helper text, and an error that marks the cells invalid."
    },
    {
      "name": "busy",
      "type": "boolean",
      "default": "false",
      "description": "Shows a spinner after the cells and sets aria-busy while the code is checked."
    }
  ],
  "accessibility": [
    "The label names the first cell; the other cells are labeled \"Character n of length\".",
    "Typing advances, Backspace steps back, arrow keys move between cells, and a pasted code fills every cell.",
    "Helper and error text are linked with aria-describedby; an error sets aria-invalid on the cells.",
    "The custom caret blinks only when reduced motion is not preferred, and holds still otherwise.",
    "autoComplete=\"one-time-code\" lets phones offer the code from a text message."
  ],
  "states": [
    "empty",
    "focused",
    "filled",
    "complete",
    "busy",
    "invalid",
    "disabled"
  ],
  "files": [
    {
      "path": "ui/input-otp.tsx",
      "content": "\"use client\";\n\nimport { OTPField as BaseOTPField } from \"@base-ui/react/otp-field\";\nimport { Fragment, type ReactNode, useEffect, useId, useRef } from \"react\";\n\ntype ClassName<State> = string | ((state: State) => string | undefined) | undefined;\n\nfunction cx(...classes: (string | false | null | undefined)[]) {\n  return classes.filter(Boolean).join(\" \");\n}\n\n/** Prepends base classes while keeping Base UI's state-function form of className working. */\nfunction withBase<State>(base: string, className: ClassName<State>) {\n  return typeof className === \"function\" ? (state: State) => cx(base, className(state)) : cx(base, className);\n}\n\nexport interface InputOTPProps extends Omit<BaseOTPField.Root.Props, \"length\" | \"children\"> {\n  /** Number of characters in the code. */\n  length?: number;\n  /**\n   * How the cells are split into groups, for example `[3, 3]` for a six-digit code read in two\n   * halves. A short dash separates the groups. Defaults to one group.\n   */\n  groups?: number[];\n  /** Visible label. Pass `aria-label` instead when the label is shown elsewhere. */\n  label?: ReactNode;\n  /** Helper text below the cells, for example where the code was sent. */\n  description?: ReactNode;\n  /** Error message. Marks the cells invalid and replaces the description. */\n  error?: ReactNode;\n  /** Shows a spinner after the cells and sets `aria-busy`, for example while the code is checked. */\n  busy?: boolean;\n  /** Classes for the wrapping element. `className` styles the row of cells. */\n  containerClassName?: string;\n}\n\n/**\n * A one-time code entry with one cell per character. Typing moves to the next cell, Backspace\n * steps back, arrow keys move between cells, and pasting a whole code fills every cell at once.\n * The empty focused cell shows a blinking caret; under reduced motion the caret holds still.\n * Use `onValueComplete` to verify the code as soon as the last character lands.\n */\nexport function InputOTP({\n  length = 6,\n  groups,\n  label,\n  description,\n  error,\n  busy = false,\n  id,\n  className,\n  containerClassName,\n  disabled,\n  \"aria-describedby\": describedBy,\n  ...props\n}: InputOTPProps) {\n  const generatedId = useId();\n  const inputId = id ?? generatedId;\n  const messageId = `${inputId}-message`;\n  const invalid = error != null && error !== false;\n  const message = invalid ? error : description;\n  const sizes = groups?.length ? groups : [length];\n\n  let offset = 0;\n  const cells = sizes.map((size, group) => {\n    const start = offset;\n    offset += size;\n    return (\n      <Fragment key={group}>\n        {group > 0 ? <span aria-hidden=\"true\" className=\"h-px w-2.5 shrink-0 bg-zinc-300 dark:bg-zinc-700\" /> : null}\n        <div className=\"flex min-w-0 gap-1.5\">\n          {Array.from({ length: size }, (_, i) => (\n            <InputOTPSlot key={start + i} index={start + i} length={length} invalid={invalid} />\n          ))}\n        </div>\n      </Fragment>\n    );\n  });\n\n  return (\n    <div className={cx(\"grid min-w-0 grid-cols-[minmax(0,1fr)] gap-1.5\", containerClassName)}>\n      {label ? (\n        <label htmlFor={inputId} className=\"text-sm leading-5 font-medium text-zinc-950 dark:text-white\">\n          {label}\n        </label>\n      ) : null}\n      <div className=\"flex min-w-0 items-center gap-3\">\n        <BaseOTPField.Root\n          id={inputId}\n          length={length}\n          disabled={disabled}\n          aria-busy={busy || undefined}\n          aria-describedby={cx(message ? messageId : null, describedBy) || undefined}\n          className={withBase(\"flex min-w-0 items-center gap-2 data-[disabled]:opacity-60\", className)}\n          {...props}\n        >\n          {cells}\n        </BaseOTPField.Root>\n        {busy ? (\n          <svg\n            aria-hidden=\"true\"\n            viewBox=\"0 0 16 16\"\n            fill=\"none\"\n            className=\"size-4 shrink-0 text-zinc-400 motion-safe:animate-spin starting:opacity-0 transition-opacity duration-200 motion-reduce:transition-none dark:text-zinc-500\"\n          >\n            <circle cx=\"8\" cy=\"8\" r=\"6\" stroke=\"currentColor\" strokeOpacity=\"0.3\" strokeWidth=\"1.5\" />\n            <path d=\"M14 8a6 6 0 0 0-6-6\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" />\n          </svg>\n        ) : null}\n      </div>\n      {message ? (\n        <p id={messageId} className={invalid ? \"text-sm leading-5 text-red-600 dark:text-red-400\" : \"text-sm leading-5 text-zinc-500 dark:text-zinc-400\"}>\n          {message}\n        </p>\n      ) : null}\n    </div>\n  );\n}\n\nfunction InputOTPSlot({ index, length, invalid }: { index: number; length: number; invalid: boolean }) {\n  const caretRef = useRef<HTMLSpanElement>(null);\n\n  // A text caret blinks 500ms on, 500ms off. It restarts solid whenever the cell gains focus,\n  // like a native caret, and does not blink at all when reduced motion is preferred.\n  useEffect(() => {\n    const caret = caretRef.current;\n    if (!caret || typeof caret.animate !== \"function\") return;\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    const animation = caret.animate(\n      [\n        { opacity: 1, offset: 0 },\n        { opacity: 1, offset: 0.5 },\n        { opacity: 0, offset: 0.5 },\n        { opacity: 0, offset: 1 },\n      ],\n      { duration: 1000, iterations: Infinity },\n    );\n    return () => animation.cancel();\n  }, []);\n\n  return (\n    <BaseOTPField.Input\n      aria-label={index === 0 ? undefined : `Character ${index + 1} of ${length}`}\n      onFocus={() => caretRef.current?.getAnimations().forEach((animation) => (animation.currentTime = 0))}\n      className={cx(\n        \"peer size-full min-w-0 bg-transparent text-center font-mono text-lg text-zinc-950 caret-transparent outline-none tabular-nums dark:text-white\",\n        \"disabled:cursor-not-allowed\",\n      )}\n      render={(inputProps, state) => (\n        <span\n          data-filled={state.filled || undefined}\n          className={cx(\n            // Cells shrink toward 28px wide on narrow screens instead of overflowing.\n            \"relative flex h-10 w-9 min-w-7 rounded-lg border bg-white shadow-sm dark:bg-zinc-950\",\n            \"transition-[border-color,box-shadow] duration-150 ease-out motion-reduce:transition-none\",\n            invalid\n              ? \"border-red-400 has-focus:border-red-500 has-focus:ring-3 has-focus:ring-red-500/20 dark:border-red-500/60\"\n              : cx(\n                  \"border-zinc-300 dark:border-zinc-700\",\n                  \"data-filled:border-zinc-400 dark:data-filled:border-zinc-600\",\n                  \"has-focus:border-sky-500 has-focus:ring-3 has-focus:ring-sky-500/20\",\n                ),\n          )}\n        >\n          <input {...inputProps} />\n          <span\n            ref={caretRef}\n            aria-hidden=\"true\"\n            className={cx(\n              \"pointer-events-none absolute top-1/2 left-1/2 hidden h-5 w-px -translate-x-1/2 -translate-y-1/2 bg-zinc-950 dark:bg-white\",\n              !state.filled && \"peer-focus:block\",\n            )}\n          />\n        </span>\n      )}\n    />\n  );\n}\n"
    }
  ],
  "example": {
    "content": "\"use client\";\n\nimport { useState } from \"react\";\nimport { InputOTP } from \"@/components/shaken/ui/input-otp\";\n\nexport default function InputOTPExample() {\n  const [status, setStatus] = useState<\"idle\" | \"checking\" | \"wrong\" | \"verified\">(\"idle\");\n\n  function verify(code: string) {\n    setStatus(\"checking\");\n    window.setTimeout(() => setStatus(code === \"482916\" ? \"verified\" : \"wrong\"), 900);\n  }\n\n  return (\n    <div className=\"grid w-full max-w-sm gap-8\">\n      <InputOTP\n        label=\"Verification code\"\n        groups={[3, 3]}\n        autoComplete=\"one-time-code\"\n        busy={status === \"checking\"}\n        readOnly={status === \"checking\" || status === \"verified\"}\n        onValueChange={() => status === \"wrong\" && setStatus(\"idle\")}\n        onValueComplete={verify}\n        description={\n          status === \"verified\" ? (\n            <span className=\"text-emerald-600 dark:text-emerald-400\">Verified. You can close this tab.</span>\n          ) : (\n            \"Enter the 6-digit code we sent to ada@northwind.dev. Try 482916.\"\n          )\n        }\n        error={status === \"wrong\" ? \"That code did not match. Check the latest email.\" : undefined}\n      />\n      <InputOTP\n        label=\"Recovery code\"\n        length={8}\n        groups={[4, 4]}\n        validationType=\"alphanumeric\"\n        normalizeValue={(value) => value.toUpperCase()}\n        description=\"Use one of the backup codes you saved when you turned on two-factor sign-in.\"\n      />\n    </div>\n  );\n}\n"
  }
}
