# Code input

> The six boxes, done properly: digits rise in, a paste cascades, a wrong code shakes and clears, the right one becomes a pill with a check.

One-time codes are the last step before someone gets in, and most code inputs are six boxes that blink. Here each digit rises into its box out of a blur and the box you’re on carries a blinking caret; delete and the digit lifts away. Paste a code, or let the phone autofill it, and it drops in one box after another. With every box full the row carries a sweep of light while you check it. A wrong code shakes, turns the boxes red, says so, and clears from the right so you can type again. For the right one the digits stay exactly where they are while the boxes’ own outlines melt into one pill around them, and a check draws itself at its end. Underneath, "Resend in 0:28" rolls its seconds down and becomes a Resend button that spins, ticks and starts the clock again.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/code-input.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): `number-roll`, `status-button`, `text-morph`
- Docs and live preview: https://hairlineui.com/ui/code-input
- Registry item (source): https://hairlineui.com/r/code-input.json

## Usage

```tsx
import { CodeInput } from "@/components/code-input";

<CodeInput
  groups={[3, 3]}
  onComplete={(code) => verify(code)} // resolve true to accept, false or throw to reject
  onResend={() => sendCode()}
/>
```

## With a sign-in API

Verify on complete; whatever your API returns decides right or wrong, and the component handles every state in between.

```tsx
"use client";
import { useRouter } from "next/navigation";
import { CodeInput } from "@/components/code-input";

export function VerifyStep({ phone }: { phone: string }) {
  const router = useRouter();
  return (
    <CodeInput
      autoFocus
      groups={[3, 3]}
      onComplete={async (code) => {
        const res = await fetch("/api/auth/verify", { method: "POST", body: JSON.stringify({ phone, code }) });
        if (!res.ok) return false;
        setTimeout(() => router.push("/app"), 900); // let "Verified" land first
        return true;
      }}
      onResend={() => fetch("/api/auth/send", { method: "POST", body: JSON.stringify({ phone }) })}
    />
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `onComplete` | `(code) => Promise<boolean \| void> \| boolean \| void` | – | Runs when every box is filled. Resolve or return true to accept; throw or return false to reject. |
| `onResend` | `() => Promise \| void` | – | Shows the resend line; the countdown starts on mount and after each resend. |
| `resendAfter` | `number` | `30` | Seconds before a code can be resent. |
| `length` | `number` | `6` | How many boxes. |
| `groups` | `number[]` | – | Split the boxes, e.g. [3, 3] puts a dash between two threes. |
| `value / defaultValue / onValueChange` | `string` | – | Controlled or not; a full value set from outside is checked too. |
| `alphanumeric` | `boolean` | `false` | Letters as well as digits (shown upper case). |
| `label` | `string` | `"Enter the 6-digit code"` | The field’s label; it morphs through the states. |
| `autoFocus` | `boolean` | – | Focus the first box on mount. |

## States

- **typing**: Each character rises 45% out of a 4px blur (300ms); a deleted one lifts away (200ms). The active box has a 1.5px ring and a blinking caret.
- **paste**: Several characters arriving at once (paste, one-time-code autofill, or a value set from outside) cascade in 45ms apart.
- **checking**: Once full, onComplete runs and the row carries a 1.4s sweep of light; the input is read-only.
- **wrong**: The row shakes (±5px, 380ms), the boxes ring red and the label morphs to "That code didn’t work"; after 650ms the characters lift away right to left (30ms apart) and focus returns to the first box.
- **right**: Nothing moves: the boxes lose their own rings and the dash fades (300ms) while one emerald ring closes around the row (500ms), which opens at its end for a check that draws itself, and the label morphs to "Verified".
- **resend**: With onResend, "Resend in 0:28" counts down (seconds roll down), then crossfades to a Resend button (Status button): Sending, Sent, and the clock starts again.

## What it handles

One-time codes are the last step before someone gets in, and most code inputs are six boxes that blink. This one handles typing, pasting, autofill, wrong codes and resending.

- **One real input underneath**: It has autocomplete="one-time-code" and the numeric keyboard, so SMS autofill, paste and screen readers all work.
- **Paste and autofill cascade**: A pasted code drops into the boxes one after another.
- **It checks on the last digit**: onComplete runs when every box is full, and the input is read-only while it checks.
- **Wrong codes are clear**: The row shakes, the boxes turn red, the label says so, and the boxes clear from the right.
- **The right code is a moment**: The boxes melt into one pill with a check and the label reads “Verified”.
- **Resend with a countdown**: “Resend in 0:28” rolls down, then becomes a Resend button that shows Sending and Sent.

## Replaces

six separate inputs, focus juggling between boxes, paste handling, a shake animation, a resend countdown

## Where it fits

- **Sign-in codes**: The step where someone types the code sent by email or SMS.
- **Two-factor authentication**: Confirm a second factor with a six-digit code.
- **Email verification**: Confirm an address right after sign-up.
- **Sensitive actions**: Ask for a code before a payment or a settings change goes through.

## Notes

- One real input with autocomplete="one-time-code" and the numeric keyboard sits over the boxes, so SMS autofill, paste, and screen readers see a normal text field with a label.
- The caret is always at the end; the boxes only show where you are.
- A polite status region announces "Checking the code", "That code didn’t work. Enter it again." and "Code verified"; the red ring always comes with that message.
- The countdown’s digits are hidden from assistive tech while ticking; the button announces itself when it’s ready.
- Installs Number roll, Status button and Text morph. With reduced motion nothing rises, shakes or sweeps; the states change in place.

License: MIT (https://hairlineui.com/license#components)
