# Input OTP

A one-time code, one drawn box a character: typing moves on, backspace goes back, pasting fills them all.

Docs: https://ballpoint.st9wd.com/docs/input-otp

```bash
npx shadcn@latest add @ballpoint/input-otp
```

```tsx
import { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot } from "@/components/ui/input-otp"

<InputOTP length={6}>
  <InputOTPGroup>
    <InputOTPSlot index={0} />
    <InputOTPSlot index={1} />
    <InputOTPSlot index={2} />
  </InputOTPGroup>
  <InputOTPSeparator />
  <InputOTPGroup>
    <InputOTPSlot index={3} />
    <InputOTPSlot index={4} />
    <InputOTPSlot index={5} />
  </InputOTPGroup>
</InputOTP>
```

Props:

- `length (or maxLength)`: `number` (default `6`). How many characters. Add one InputOTPSlot for each.
- `value / defaultValue`: `string`. The code so far, controlled or not.
- `onValueChange / onValueComplete`: `(value: string) => void`. Called as it changes, and when every box is filled.
- `validationType`: `"numeric" | "alphanumeric" | "none"` (default `"numeric"`). Which characters a box accepts.
- `mask`: `boolean` (default `false`). Hide the characters as they are typed.
- `seed`: `string | number`. Pins the drawing.

Pen settings: `draw`, `roughness`, `passes`, `radius`, `corners`, `weight`, `speed` (see Pen settings).

Every other prop goes to Base UI's OTP Field: https://base-ui.com/react/components/otp-field

## Example

```tsx
import { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot } from "@/components/ui/input-otp";

export default function InputOTPDemo() {
  return (
    <InputOTP length={6} aria-label="One-time code" validationType="numeric">
      <InputOTPGroup>
        <InputOTPSlot index={0} seed="otp-0" />
        <InputOTPSlot index={1} seed="otp-1" />
        <InputOTPSlot index={2} seed="otp-2" />
      </InputOTPGroup>
      <InputOTPSeparator />
      <InputOTPGroup>
        <InputOTPSlot index={3} seed="otp-3" />
        <InputOTPSlot index={4} seed="otp-4" />
        <InputOTPSlot index={5} seed="otp-5" />
      </InputOTPGroup>
    </InputOTP>
  );
}
```

## Verifying a code

Controlled, with an invalid state when the code is wrong.

```tsx
"use client";

import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Field, FieldDescription, FieldLabel } from "@/components/ui/field";
import { InputOTP, InputOTPGroup, InputOTPSlot } from "@/components/ui/input-otp";

export default function InputOTPVerify() {
  const [code, setCode] = useState("");
  const [status, setStatus] = useState<"idle" | "ok" | "wrong">("idle");
  return (
    <form
      className="flex w-full max-w-sm flex-col gap-4"
      onSubmit={(event) => {
        event.preventDefault();
        setStatus(code === "123456" ? "ok" : "wrong");
      }}
    >
      <Field>
        <FieldLabel htmlFor="otp-verify">Verification code</FieldLabel>
        <InputOTP id="otp-verify" value={code} onValueChange={(next) => (setCode(next), setStatus("idle"))} length={6} validationType="numeric">
          <InputOTPGroup>
            {Array.from({ length: 6 }, (_, i) => (
              <InputOTPSlot key={i} index={i} seed={`otpv-${i}`} aria-invalid={status === "wrong"} />
            ))}
          </InputOTPGroup>
        </InputOTP>
        <FieldDescription>Enter the 6-digit code we sent you. (Try 123456.)</FieldDescription>
      </Field>
      <div className="flex items-center gap-4">
        <Button type="submit" disabled={code.length < 6} seed="otpv-go">
          Verify
        </Button>
        <p role="status" className={status === "wrong" ? "text-destructive" : "text-ink-2"}>
          {status === "ok" ? "That's the one." : status === "wrong" ? "That code isn't right." : ""}
        </p>
      </div>
    </form>
  );
}
```

## Pen settings

- `draw`: `"auto" | "mount" | "none"` (default `"auto"`). When the strokes draw themselves in: the first time they scroll into view, as soon as they render, or never (already drawn).
- `roughness`: `number` (default `1`). 0 is ruler-neat, 1 a quick confident hand, 2 a scrawl.
- `passes`: `1 | 2 | 3`. How many times an outline is gone over. Each component picks its own default.
- `radius`: `number | "full"` (default `0`). Corner radius in px; "full" draws a pill.
- `corners`: `"crossed" | "joined"` (default `"crossed"`). Square corners: sides pulled separately past each other, or the box drawn in one motion.
- `weight`: `number` (default `1`). Line weight multiplier. Also settable in CSS as --ink-weight.
- `speed`: `number` (default `1`). Drawing speed multiplier. Also settable in CSS as --ink-speed.
