# One-time Code

> Paste-aware OTP input that auto-advances.

- Category: Inputs & forms
- Tier: Free (MIT)
- Page: https://bowls.dev/components/otp-input
- Requires: React 18+, Tailwind CSS 4

## Install

Copy the source below into `components/bowls/otp-input.tsx`.

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `length` | `number` | `6` | Number of digits. |
| `onComplete` | `(code: string) => void` | — | Called when every box is filled. |
| `label` | `string` | `"One-time code"` | Group label for screen readers. |

## Usage

```tsx
"use client";

import { useState } from "react";
import { OtpInput } from "@/components/bowls/otp-input";

export default function Demo() {
  const [done, setDone] = useState<string | null>(null);
  return (
    <div className="flex h-full w-full flex-col items-center justify-center gap-4">
      <OtpInput length={5} onComplete={setDone} />
      <span className="font-mono text-xs text-[#666] dark:text-[#999999]">{done ? `✓ ${done}` : "try pasting 42069"}</span>
    </div>
  );
}
```

## Source

`components/bowls/otp-input.tsx`

```tsx
"use client";

import { useRef, useState } from "react";

export type OtpInputProps = {
  length?: number;
  onComplete?: (code: string) => void;
  label?: string;
};

/** One-time code input: auto-advances, handles paste and backspace. */
export function OtpInput({ length = 6, onComplete, label = "One-time code" }: OtpInputProps) {
  const [digits, setDigits] = useState<string[]>(() => Array(length).fill(""));
  const refs = useRef<(HTMLInputElement | null)[]>([]);

  const update = (next: string[]) => {
    setDigits(next);
    if (next.every(Boolean)) onComplete?.(next.join(""));
  };

  const fill = (start: number, value: string) => {
    const chars = value.replace(/\D/g, "").slice(0, length - start).split("");
    if (!chars.length) return;
    const next = [...digits];
    chars.forEach((c, i) => (next[start + i] = c));
    update(next);
    refs.current[Math.min(start + chars.length, length - 1)]?.focus();
  };

  return (
    <fieldset className="flex gap-2 border-0 p-0">
      <legend className="sr-only">{label}</legend>
      {digits.map((d, i) => (
        <input
          key={i}
          ref={(el) => {
            refs.current[i] = el;
          }}
          inputMode="numeric"
          autoComplete={i === 0 ? "one-time-code" : "off"}
          aria-label={`Digit ${i + 1}`}
          value={d}
          onChange={(e) => fill(i, e.target.value)}
          onPaste={(e) => {
            e.preventDefault();
            fill(i, e.clipboardData.getData("text"));
          }}
          onKeyDown={(e) => {
            if (e.key === "Backspace" && !digits[i] && i > 0) {
              const next = [...digits];
              next[i - 1] = "";
              update(next);
              refs.current[i - 1]?.focus();
            }
            if (e.key === "ArrowLeft") refs.current[i - 1]?.focus();
            if (e.key === "ArrowRight") refs.current[i + 1]?.focus();
          }}
          className="h-14 w-11 rounded-xl bg-white dark:bg-black text-center font-mono text-2xl font-semibold caret-[#0a0a0a] dark:caret-[#f5f5f5] outline-none focus:shadow-[inset_0_0_0_2px_#0a0a0a] dark:focus:shadow-[inset_0_0_0_2px_#f5f5f5]"
        />
      ))}
    </fieldset>
  );
}
```
