{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "otp-field",
  "title": "OtpField",
  "description": "A one-time code entry on the Field contract: one slot per character, with focus that moves as the person types, a whole code pasted at once, and `autoComplete=\"one-time-code\"` so phones offer the code from a text message.",
  "dependencies": [
    "@base-ui/react@^1.8.0"
  ],
  "registryDependencies": [
    "@ouro/field",
    "@ouro/lib-env",
    "@ouro/lib-page-direction",
    "@ouro/lib-refs",
    "@ouro/lib-use-dev-messages",
    "@ouro/lib-use-field"
  ],
  "files": [
    {
      "path": "registry/ouro/components/ouro/otp-field/otp-field-styles.ts",
      "content": "export { defaultSlotLabel, otpValidationTypes, separatorClass, slotClass, slotsClass };\nexport type { OtpValidationType };\n\n// Class strings and value lists for OtpField, apart from the component so its file exports\n// only components (React Fast Refresh). Class strings are literal so the app's Tailwind finds\n// them when it compiles.\n\n/**\n * The kinds of character OtpField accepts.\n *\n * @internal\n */\nconst otpValidationTypes = [\"numeric\", \"alpha\", \"alphanumeric\"] as const;\n\n/**\n * A kind of character OtpField accepts.\n *\n * @internal\n */\ntype OtpValidationType = (typeof otpValidationTypes)[number];\n\n/**\n * The row of slots.\n *\n * @internal\n */\nconst slotsClass = \"m-0 box-border flex max-w-full items-center gap-2\";\n\n/**\n * One slot: a square with Field's control look, 44px with 16px text on phones (iOS zooms\n * into smaller text on focus), stepping down to 36px and 14px from the `sm` breakpoint with\n * a fine pointer, so a landscape phone keeps the phone size. The focused slot is outlined\n * however it got focus, because focus moves between slots as the person types. Its width is\n * at most the row's (`min(…, 100%)`), which a layout that sizes to content (a grid column)\n * reads as no width at all, so too many slots for the column narrow instead of spilling out.\n *\n * @internal\n * @claim \"One slot: a square with Field's\" registry/ouro/components/ouro/otp-field/otp-field.test.tsx verify:testExists(registry/ouro/components/ouro/otp-field/otp-field.test.tsx::OtpField looks > has 44px slots with 16px text on phones and steps down with a fine pointer from sm)\n * @claim \"One slot: a square with Field's\" registry/ouro/components/ouro/otp-field/otp-field.touch.test.tsx verify:testExists(registry/ouro/components/ouro/otp-field/otp-field.touch.test.tsx::keeps 44px slots and 16px text on a landscape phone, past the sm breakpoint)\n * @claim \"too many slots for the column narrow\" registry/ouro/components/ouro/otp-field/otp-field.test.tsx verify:testExists(registry/ouro/components/ouro/otp-field/otp-field.test.tsx::OtpField looks > narrows its slots to fit a column too narrow for them, rather than spilling out of it)\n */\nconst slotClass =\n  \"m-0 box-border block h-(--ouro-otp-slot) w-[min(var(--ouro-otp-slot),100%)] min-w-0 shrink p-0 text-center tabular-nums \" +\n  \"[--ouro-otp-slot:calc(var(--spacing)*11)] sm:pointer-fine:[--ouro-otp-slot:calc(var(--spacing)*9)] \" +\n  \"rounded-ouro-md border border-solid border-input bg-transparent text-base text-foreground sm:pointer-fine:text-sm font-[inherit] caret-transparent \" +\n  \"outline-ring outline-offset-2 focus:outline-2 focus:outline-solid \" +\n  \"transition-[border-color] duration-(--ouro-duration-fast) ease-[ease] \" +\n  \"aria-invalid:border-destructive disabled:cursor-not-allowed disabled:opacity-50 \" +\n  \"dark:bg-input/30\";\n\n/**\n * The dash between groups of slots.\n *\n * @internal\n */\nconst separatorClass = \"m-0 box-border h-0.5 w-2 shrink-0 rounded-full border-0 bg-border\";\n\n/**\n * Each slot's name after the first, which takes the field's label.\n *\n * @internal\n * @claim \"Each slot's name after the first\" registry/ouro/components/ouro/otp-field/otp-field.test.tsx verify:testExists(registry/ouro/components/ouro/otp-field/otp-field.test.tsx::OtpField > is a labeled group of six slots on the Field contract)\n * @claim \"Each slot's name after the first\" registry/ouro/components/ouro/otp-field/otp-field.test.tsx verify:testExists(registry/ouro/components/ouro/otp-field/otp-field.test.tsx::OtpField > takes a whole code that iOS autofill writes into the first slot)\n */\nfunction defaultSlotLabel(position: number, length: number): string {\n  return `Character ${position} of ${length}`;\n}\n",
      "type": "registry:ui",
      "target": "@components/ouro/otp-field/otp-field-styles.ts"
    },
    {
      "path": "registry/ouro/components/ouro/otp-field/otp-field.tsx",
      "content": "\"use client\";\n\nimport { OTPField } from \"@base-ui/react/otp-field\";\nimport { Fragment, useLayoutEffect, useRef, type JSX, type ReactNode, type Ref } from \"react\";\nimport { Field } from \"@/registry/ouro/components/ouro/field/field\";\nimport { contractWarnings } from \"@/registry/ouro/components/ouro/field/field-styles\";\nimport { useMergedRef } from \"@/registry/ouro/lib/ouro/refs\";\nimport { FieldOwnerContext, useField, type ReadonlyErrorMessages } from \"@/registry/ouro/lib/ouro/use-field\";\nimport { useDevWarnings } from \"@/registry/ouro/lib/ouro/use-dev-messages\";\nimport { PageDirection } from \"@/registry/ouro/lib/ouro/page-direction\";\nimport {\n  defaultSlotLabel,\n  otpValidationTypes,\n  separatorClass,\n  slotClass,\n  slotsClass,\n  type OtpValidationType,\n} from \"@/registry/ouro/components/ouro/otp-field/otp-field-styles\";\n\n/** Props for {@link OtpField}. */\nexport interface OtpFieldProps {\n  /**\n   * What the code is (\"Verification code\"), shown above the slots and read as the first slot's\n   * name. Required: every control needs a name. To hide it visually, keep it and add `hideLabel`.\n   * @claim \"Required: every control needs a name\" registry/ouro/components/ouro/otp-field/otp-field.test-d.ts verify:fileLine(registry/ouro/components/ouro/otp-field/otp-field.test-d.ts, /expectTypeOf<\\{\\slength:\\s6\\s\\}>\\(\\)\\.not\\.toExtend<OtpFieldProps>/)\n   */\n  label: string;\n  /** Hides the label visually; screen readers still read it. */\n  hideLabel?: boolean | undefined;\n  /** Supporting text under the label (\"We sent it to your phone\"), read through `aria-describedby`. */\n  description?: ReactNode | undefined;\n  /**\n   * What is wrong (\"That code has expired\"), from your server action or client check. It shows\n   * under the slots, which get `aria-invalid`. A message is text or a React node (one with a link); an empty string or array means valid.\n   */\n  error?: ReadonlyErrorMessages | undefined;\n  /** The code must be filled before the form submits. */\n  required?: boolean | undefined;\n  /**\n   * Disables every slot and fades the field.\n   *\n   * @claim \"Disables every slot and fades the\" registry/ouro/components/ouro/otp-field/otp-field.test.tsx verify:testExists(registry/ouro/components/ouro/otp-field/otp-field.test.tsx::OtpField > wires description, error, required and disabled through Field)\n   */\n  disabled?: boolean | undefined;\n  /** The form field name the whole code submits under. */\n  name?: string | undefined;\n  /**\n   * How many characters the code has: one slot each.\n   * @default 6\n   * @claim \"How many characters the code has\" registry/ouro/components/ouro/otp-field/otp-field.test.tsx verify:testExists(registry/ouro/components/ouro/otp-field/otp-field.test.tsx::OtpField > fills every slot from a pasted code, ignoring spaces)\n   * @claim \"How many characters the code has\" registry/ouro/components/ouro/otp-field/otp-field.test.tsx verify:testExists(registry/ouro/components/ouro/otp-field/otp-field.test.tsx::OtpField > is a labeled group of six slots on the Field contract)\n   */\n  length?: number | undefined;\n  /**\n   * Which characters the slots take. Others are dropped as the person types or pastes.\n   * `numeric` brings up the number keyboard on phones.\n   * @default \"numeric\"\n   */\n  validationType?: \"numeric\" | \"alpha\" | \"alphanumeric\" | (string & { 'Use \"numeric\", \"alpha\" or \"alphanumeric\"': never }) | undefined;\n  /** The code, when you control it. Pair it with `onValueChange`. */\n  value?: string | undefined;\n  /** The code the field starts with, when it keeps its own state. */\n  defaultValue?: string | undefined;\n  /** Called with the code as the person types, pastes or deletes. */\n  onValueChange?: ((value: string) => void) | undefined;\n  /**\n   * Called with the code when every slot is filled.\n   *\n   * @claim \"Called with the code when every\" registry/ouro/components/ouro/otp-field/otp-field.test.tsx verify:testExists(registry/ouro/components/ouro/otp-field/otp-field.test.tsx::OtpField > moves to the next slot as the person types, and reports the value and completion)\n   */\n  onValueComplete?: ((value: string) => void) | undefined;\n  /**\n   * Submits the form when every slot is filled.\n   *\n   * @claim \"Submits the form when every slot\" registry/ouro/components/ouro/otp-field/otp-field.test.tsx verify:testExists(registry/ouro/components/ouro/otp-field/otp-field.test.tsx::OtpField > submits the code under its name, and can submit the form when complete)\n   */\n  autoSubmit?: boolean | undefined;\n  /** Hides the characters as dots, as a password field does. */\n  mask?: boolean | undefined;\n  /** Shows the slots in groups of this many, with a dash between groups: `groupSize={3}` reads `123-456`. */\n  groupSize?: number | undefined;\n  /**\n   * The name of each slot after the first (the first takes `label`). Translate it with your app.\n   * @default (position, length) => `Character ${position} of ${length}`\n   * @claim \"The name of each slot after\" registry/ouro/components/ouro/otp-field/otp-field.test.tsx verify:testExists(registry/ouro/components/ouro/otp-field/otp-field.test.tsx::OtpField > takes a whole code that iOS autofill writes into the first slot)\n   * @claim \"The name of each slot after\" registry/ouro/components/ouro/otp-field/otp-field.test.tsx verify:testExists(registry/ouro/components/ouro/otp-field/otp-field.test.tsx::OtpField > is a labeled group of six slots on the Field contract)\n   */\n  slotLabel?: ((position: number, length: number) => string) | undefined;\n  /**\n   * Autofill for the first slot. The default lets phones offer a code from a text message.\n   * @default \"one-time-code\"\n   */\n  autoComplete?: string | undefined;\n  /** Shows the code without letting the person change it. */\n  readOnly?: boolean | undefined;\n  /**\n   * Layout only: margins, width, grid placement (`mt-4`). Appearance classes warn in\n   * development; style fields in your theme.\n   */\n  className?: string | undefined;\n  /** The slots' group element (`role=\"group\"`). */\n  ref?: Ref<HTMLDivElement> | undefined;\n\n  /** Not a prop. Ouro UI uses `length`: `<OtpField length={6}>`. */\n  maxLength?: { 'Use <OtpField length={6}>': never };\n  /** Not a prop. Ouro UI uses `length`: `<OtpField length={4}>`. */\n  numInputs?: { 'Use <OtpField length={4}>': never };\n  /** Not a prop. Ouro UI uses `validationType`: `<OtpField validationType=\"alphanumeric\">`. */\n  pattern?: { 'Use <OtpField validationType=\"alphanumeric\">': never };\n  /** Not a prop. Ouro UI uses `onValueComplete`: `<OtpField onValueComplete={(code) => ...}>`. */\n  onComplete?: { 'Use <OtpField onValueComplete={(code) => ...}>': never };\n  /** Not a prop. Ouro UI uses `onValueChange`, which gets the code: `<OtpField onValueChange={(code) => ...}>`. */\n  onChange?: { 'Use <OtpField onValueChange={(code) => ...}>': never };\n  /** Not a prop. Ouro UI uses `error`: `<OtpField error=\"Wrong code.\">`. */\n  errorMessage?: { 'Use <OtpField error=\"Wrong code.\">': never };\n  /** Not a prop. Ouro UI uses `description`: `<OtpField description=\"...\">`. */\n  helperText?: { 'Use <OtpField description=\"...\">': never };\n  /** Not a prop. `error` marks the field invalid: `<OtpField error=\"What is wrong\">`. */\n  isInvalid?: { 'Use <OtpField error=\"What is wrong\">': never };\n  /** Not a prop. `error` marks the field invalid: `<OtpField error=\"What is wrong\">`. */\n  invalid?: { 'Use <OtpField error=\"What is wrong\">': never };\n  /** Not a prop. Ouro UI uses `required`: `<OtpField required>`. */\n  isRequired?: { 'Use <OtpField required>': never };\n  /** Not a prop. Ouro UI uses `disabled`: `<OtpField disabled>`. */\n  isDisabled?: { 'Use <OtpField disabled>': never };\n}\n\n/** Field's messages and root name OtpField. */\nconst owner = { name: \"OtpField\", data: \"otp-field\" };\n\ninterface SlotsProps {\n  length: number;\n  validationType: OtpValidationType;\n  value: string | undefined;\n  defaultValue: string | undefined;\n  onValueChange: ((value: string) => void) | undefined;\n  onValueComplete: ((value: string) => void) | undefined;\n  autoSubmit: boolean | undefined;\n  mask: boolean | undefined;\n  groupSize: number | undefined;\n  slotLabel: (position: number, length: number) => string;\n  autoComplete: string | undefined;\n  readOnly: boolean | undefined;\n  ref: Ref<HTMLDivElement> | undefined;\n}\n\n/**\n * The slots, inside the Field, where they read its required state, invalid state and\n * server-known description ids. The group and every slot get the ids, since a screen reader\n * may land on any slot, and the invalid state is Ouro's own (Base UI's tracks only its own\n * checks).\n * @claim \"The group and every slot get\" registry/ouro/components/ouro/otp-field/otp-field.test.tsx verify:testExists(registry/ouro/components/ouro/otp-field/otp-field.test.tsx::OtpField > is a labeled group of six slots on the Field contract)\n * @claim \"The group and every slot get\" registry/ouro/components/ouro/otp-field/otp-field.test.tsx verify:testExists(registry/ouro/components/ouro/otp-field/otp-field.test.tsx::OtpField looks > outlines the focused slot and marks invalid slots)\n * @claim \"The group and every slot get\" registry/ouro/components/ouro/otp-field/otp-field.test.tsx verify:testExists(registry/ouro/components/ouro/otp-field/otp-field.test.tsx::OtpField server rendering > hydrates with the same ids and no hydration warning)\n */\nfunction Slots({ length, groupSize, slotLabel, onValueChange, onValueComplete, ref, ...rest }: SlotsProps): JSX.Element {\n  const field = useField()!;\n  // Base UI renders the hidden input the code submits with beside the slots' group and takes no\n  // ref or props for it, so its control marker goes on directly.\n  const rootRef = useRef<HTMLDivElement>(null);\n  const setRef = useMergedRef(ref, rootRef);\n  useLayoutEffect(() => {\n    for (const input of rootRef.current!.parentElement!.querySelectorAll(':scope > input[aria-hidden=\"true\"]')) {\n      input.setAttribute(\"data-ouro-control\", \"otp-field\");\n      input.setAttribute(\"data-ouro-part\", \"hidden-input\");\n    }\n  });\n  // The Field's invalid state: OtpField's own error, or a Form's error for this name.\n  const invalid = field.invalid;\n  const groups = groupSize && Number.isInteger(groupSize) && groupSize > 0 && groupSize < length ? groupSize : length;\n  const directed = (\n    <OTPField.Root\n      {...rest}\n      ref={setRef}\n      length={length}\n      required={field.required}\n      aria-describedby={field.describedBy}\n      onValueChange={onValueChange && ((next) => onValueChange(next))}\n      onValueComplete={onValueComplete && ((next) => onValueComplete(next))}\n      className={slotsClass}\n    >\n      {Array.from({ length }, (_, index) => (\n        <Fragment key={index}>\n          {index > 0 && index % groups === 0 && <OTPField.Separator data-ouro-otp-separator=\"\" className={separatorClass} />}\n          <OTPField.Input\n            data-ouro-otp-slot=\"\"\n            data-ouro-control=\"otp-field\"\n            data-ouro-part=\"slot\"\n            aria-label={index === 0 ? undefined : slotLabel(index + 1, length)}\n            aria-describedby={field.describedBy}\n            {...(invalid ? { \"aria-invalid\": true } : {})}\n            className={slotClass}\n          />\n        </Fragment>\n      ))}\n    </OTPField.Root>\n  );\n  return <PageDirection of={rootRef}>{directed}</PageDirection>;\n}\n\n/**\n * A one-time code entry on the Field contract: one slot per character, with focus that moves\n * as the person types, a whole code pasted at once, and `autoComplete=\"one-time-code\"` so\n * phones offer the code from a text message. Pass `label`, `length`, and `error` from your\n * server action; `onValueComplete` or `autoSubmit` act when every slot is filled.\n *\n * Use `TextField` with `inputMode=\"numeric\"` instead for a number with no fixed length, and\n * `TextField type=\"password\"` for a password.\n * In a right-to-left page (`dir=\"rtl\"`) the slots run from the right, and ArrowLeft moves to the next.\n *\n *\n * @group Forms\n * @claim \"In a right-to-left page\" registry/ouro/components/ouro/otp-field/otp-field.test.tsx verify:testExists(registry/ouro/components/ouro/otp-field/otp-field.test.tsx::OtpField > moves between slots the way they run in a right-to-left page: ArrowLeft to the next)\n * @see https://ui.ouroboros.bot/docs/otp-field\n *\n * @example\n * <OtpField label=\"Verification code\" name=\"code\" length={6} autoSubmit error={errors?.code} />\n * <OtpField label=\"Recovery code\" validationType=\"alphanumeric\" length={8} groupSize={4} />\n */\nexport function OtpField(props: OtpFieldProps): JSX.Element {\n  const {\n    label,\n    hideLabel,\n    description,\n    error,\n    required,\n    disabled,\n    name,\n    length: lengthProp = 6,\n    validationType: validationTypeProp = \"numeric\",\n    value,\n    defaultValue,\n    onValueChange,\n    onValueComplete,\n    autoSubmit,\n    mask,\n    groupSize,\n    slotLabel = defaultSlotLabel,\n    autoComplete,\n    readOnly,\n    className,\n    ref,\n    // Guard props (hint types), dropped here so they never reach the DOM.\n    maxLength,\n    numInputs,\n    pattern,\n    onComplete,\n    onChange,\n    errorMessage,\n    helperText,\n    isInvalid,\n    invalid,\n    isRequired,\n    isDisabled,\n  } = props;\n  // A length or validation type that is not one of OtpField's warns and renders the default.\n  const length = Number.isInteger(lengthProp) && lengthProp > 0 ? lengthProp : 6;\n  const validationType = otpValidationTypes.includes(validationTypeProp as OtpValidationType)\n    ? (validationTypeProp as OtpValidationType)\n    : \"numeric\";\n  useDevWarnings(\n    \"OtpField\",\n    process.env.NODE_ENV !== \"production\"\n      ? () => [\n          length !== lengthProp && [`length={${String(lengthProp)}} is not a positive whole number`, \"<OtpField length={6}>\"],\n          validationType !== validationTypeProp && [\n            `validationType=\"${String(validationTypeProp)}\" is not a validation type; use ${otpValidationTypes.map((type) => `\"${type}\"`).join(\", \")}`,\n            '<OtpField validationType=\"numeric\">',\n          ],\n          ...contractWarnings(\"OtpField\", { errorMessage, helperText, isInvalid, invalid, isRequired, isDisabled }),\n          maxLength !== undefined && [\"maxLength is not a prop\", `<OtpField length={${String(maxLength)}}>`],\n          numInputs !== undefined && [\"numInputs is not a prop\", `<OtpField length={${String(numInputs)}}>`],\n          pattern !== undefined && [\"pattern is not a prop\", '<OtpField validationType=\"alphanumeric\">'],\n          onComplete !== undefined && [\"onComplete is not a prop\", \"<OtpField onValueComplete={(code) => …}>\"],\n          onChange !== undefined && [\"onChange is not a prop\", \"<OtpField onValueChange={(code) => …}>\"],\n        ]\n      : /* istanbul ignore next @preserve -- production only */ undefined,\n  );\n\n  return (\n    <FieldOwnerContext.Provider value={owner}>\n      <Field\n        label={label}\n        hideLabel={hideLabel}\n        description={description}\n        error={error}\n        required={required}\n        disabled={disabled}\n        name={name}\n        className={className}\n      >\n        <Slots\n          ref={ref}\n          length={length}\n          validationType={validationType}\n          value={value}\n          defaultValue={defaultValue}\n          onValueChange={onValueChange}\n          onValueComplete={onValueComplete}\n          autoSubmit={autoSubmit}\n          mask={mask}\n          groupSize={groupSize}\n          slotLabel={slotLabel}\n          autoComplete={autoComplete}\n          readOnly={readOnly}\n        />\n      </Field>\n    </FieldOwnerContext.Provider>\n  );\n}\n",
      "type": "registry:ui",
      "target": "@components/ouro/otp-field/otp-field.tsx"
    }
  ],
  "docs": "A one-time code entry on the Field contract: one slot per character, with focus that moves as the person types, a whole code pasted at once, and `autoComplete=\"one-time-code\"` so phones offer the code from a text message. Import: import { OtpField } from \"@/components/ouro/otp-field/otp-field\", with @/components replaced by your components alias (aliases.components in components.json, such as ~/components). Also installed: field, the helpers they import in lib/ouro. Needs ouro-base in your stylesheet: add that item once per app, from the same place as this one, if you have not. Docs: https://ui.ouroboros.bot/docs/otp-field.md Ouro UI v1.3.257.",
  "type": "registry:ui"
}