# Pin Input

A one-time code or PIN typed one character per box.

A one-time code or PIN typed one character per cell. Each cell is the Input made square (xs 24 · sm 28 · md 32 · lg 36px; 40px on touch), its character centred. Typing moves to the next cell, Backspace to the previous one, a pasted or suggested code fills every cell at once. Behaviour by Ark UI's pin-input (Zag). Controlled or uncontrolled.

Kind: component · Category: Inputs · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `pin-input` machine, through Ark UI

## Add

```bash
npx fndn add pin-input
```

It writes the folder `pin-input/` into the project's ui folder (`index.ts`, `pin-input.tsx`, `PinInput.vue`, `PinInput.svelte`, `pin-input.recipe.ts`), and what it is built from: [Input](/components/input.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { PinInput } from "@/components/ui/pin-input";
```

```vue
import { PinInput } from "@/components/ui/pin-input";
```

```svelte
import { PinInput } from "$lib/components/ui/pin-input";
```

## Parts

In the DOM the root carries `data-slot="pin-input"` and each part `data-slot="pin-input-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `pin-input` |  | Ark's pin-input root: a column, the label above the cells. |
| `label` | `label` | `pin-input-label` | optional | Visible name (the label prop): text.sm, medium, ink, as the Field's label; it also names the group and every cell. |
| `control` | `div` | `pin-input-control` |  | role=group, named by the label or aria-label. The cells in a row, 6px apart (4px up to 28px); they wrap rather than overflow a narrow container. |
| `input` | `input` | `pin-input-input` | repeats | One cell: the Input (data-pin-input="input"), square (the size's height: 24, 28, 32 or 36px; 40×40 on a coarse pointer), centred tabular character, named "<label>, 2 of 6". Only one cell is in the tab order. data-filled, data-complete from the engine. |
| `hidden-input` | `input` | `pin-input-hidden-input` | optional | Ark's hidden input with the whole code, for a form (name). |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `length` | `number` | `6` | Number of cells. |
| `value` | `string[]` |  | Bindable (React: `defaultValue` and `onValueChange`; Vue: `v-model:value`; Svelte: `bind:value`). One character per cell, "" for an empty one. |
| `type` | `"numeric" \| "alphanumeric" \| "alphabetic"` | `"numeric"` | What a cell accepts; numeric also brings up the number keyboard. |
| `otp` | `boolean` | `false` | A one-time code: autocomplete="one-time-code". |
| `mask` | `boolean` | `false` | Dots instead of the characters (a PIN). |
| `size` | `"xs" \| "sm" \| "md" \| "lg"` | `"md"` | The Input's sizes (`lib/control.ts`): square cells of 24, 28, 32 or 36px. On a coarse pointer every cell is 40px. |
| `variant` | `"outline" \| "subtle" \| "ghost"` | `"outline"` | The shared control's looks (`lib/control.ts`), at every size. outline: the form field (field colour, hairline). subtle: the inspector (inset grey, no edge). ghost: nothing until touched. Focused, open and invalid look the same in every variant. |
| `label` | `string` |  | Visible label above the cells. |
| `placeholder` | `string` | `""` | Shown in an empty cell without focus. |
| `blurOnComplete` | `boolean` | `false` |  |
| `invalid` | `boolean` | `false` | The Input's red ring on every cell (aria-invalid). |
| `disabled` | `boolean` | `false` |  |
| `name` | `string` |  | Form field name (the hidden input holds the whole code). |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `valueChange` | (value: string[]) on every character |
| `valueComplete` | (code: string) when every cell is filled |

## States

`hover`, `focus`, `filled`, `complete`, `invalid`, `disabled`

## Keyboard and accessibility

Role: group.

Keyboard:

- Tab enters the first empty cell (one tab stop for the whole code)
- Typing fills the cell and moves to the next; a character the type does not accept is ignored
- Backspace clears the cell, or the previous one when it is empty; Delete clears the cell
- Arrow Left / Arrow Right move between cells (mirrored right-to-left)
- Paste fills every cell from the focused one

Notes:

- The cells are named "<label>, 2 of 6"; the group is named by the label or aria-label.
- otp sets autocomplete="one-time-code" and numeric sets inputmode=numeric, so phones offer the code they received.
- The text is 16px on touch devices (the Input's rule), so iOS does not zoom.
- To set it from outside, control `value`: React value / onValueChange.

## Motion

- the Input's: rings over duration.base, fill over duration.fast (pointer hover only)
- focus moves from cell to cell instantly

## Tokens

`color.ink`, `text.sm`

## Dependencies

- [Input](/components/input.md)

## Examples

### Two-factor code

React · `two-factor-code.tsx`

```tsx
import { PinInput } from "@/components/ui/pin-input";

/* otp: the browser and the phone offer the code from the latest text message */
export default function TwoFactorCode() {
  return <PinInput label="Authentication code" otp />;
}
```

Vue · `two-factor-code.vue`

```vue
<script setup lang="ts">
import { PinInput } from "@/components/ui/pin-input";
</script>

<!-- otp: the browser and the phone offer the code from the latest text message -->
<template>
  <PinInput label="Authentication code" otp />
</template>
```

Svelte · `two-factor-code.svelte`

```svelte
<script lang="ts">
  import { PinInput } from "$lib/components/ui/pin-input";
</script>

<!-- otp: the browser and the phone offer the code from the latest text message -->
<PinInput label="Authentication code" otp />
```

### Verify an email, checked when complete

React · `verify-email.tsx`

```tsx
import { useState } from "react";
import { PinInput } from "@/components/ui/pin-input";

/* checked when the last digit is in; the message under it is a live region */
export default function VerifyEmail() {
  const [status, setStatus] = useState<"idle" | "wrong" | "ok">("idle");
  return (
    <div className="flex flex-col gap-2">
      <PinInput
        label="Verification code"
        otp
        invalid={status === "wrong"}
        onValueChange={() => setStatus("idle")}
        onValueComplete={(code) => setStatus(code === "123456" ? "ok" : "wrong")}
      />
      <p className="m-0 text-xs text-ink-2" role="status">
        {status === "wrong" ? "That code is wrong or has expired." : status === "ok" ? "Email verified." : "Sent to maya@northwind.example."}
      </p>
    </div>
  );
}
```

Vue · `verify-email.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { PinInput } from "@/components/ui/pin-input";

const status = ref<"idle" | "wrong" | "ok">("idle");
</script>

<!-- checked when the last digit is in; the message under it is a live region -->
<template>
  <div class="flex flex-col gap-2">
    <PinInput
      label="Verification code"
      otp
      :invalid="status === 'wrong'"
      @value-change="status = 'idle'"
      @value-complete="(code) => (status = code === '123456' ? 'ok' : 'wrong')"
    />
    <p class="m-0 text-xs text-ink-2" role="status">
      {{ status === "wrong" ? "That code is wrong or has expired." : status === "ok" ? "Email verified." : "Sent to maya@northwind.example." }}
    </p>
  </div>
</template>
```

Svelte · `verify-email.svelte`

```svelte
<script lang="ts">
  import { PinInput } from "$lib/components/ui/pin-input";

  let status = $state<"idle" | "wrong" | "ok">("idle");
</script>

<!-- checked when the last digit is in; the message under it is a live region -->
<div class="flex flex-col gap-2">
  <PinInput
    label="Verification code"
    otp
    invalid={status === "wrong"}
    onvaluechange={() => (status = "idle")}
    onvaluecomplete={(code) => (status = code === "123456" ? "ok" : "wrong")}
  />
  <p class="m-0 text-xs text-ink-2" role="status">
    {status === "wrong" ? "That code is wrong or has expired." : status === "ok" ? "Email verified." : "Sent to maya@northwind.example."}
  </p>
</div>
```

### Invite code, letters and digits

React · `invite-code.tsx`

```tsx
import { PinInput } from "@/components/ui/pin-input";

/* alphanumeric: letters and digits, as printed on the invite */
export default function InviteCode() {
  return <PinInput label="Invite code" type="alphanumeric" length={6} defaultValue={["N", "W", "7"]} />;
}
```

Vue · `invite-code.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { PinInput } from "@/components/ui/pin-input";

const code = ref(["N", "W", "7"]);
</script>

<!-- alphanumeric: letters and digits, as printed on the invite -->
<template>
  <PinInput v-model:value="code" label="Invite code" type="alphanumeric" :length="6" />
</template>
```

Svelte · `invite-code.svelte`

```svelte
<script lang="ts">
  import { PinInput } from "$lib/components/ui/pin-input";

  let code = $state(["N", "W", "7"]);
</script>

<!-- alphanumeric: letters and digits, as printed on the invite -->
<PinInput bind:value={code} label="Invite code" type="alphanumeric" length={6} />
```

### Masked PIN

React · `security-pin.tsx`

```tsx
import { PinInput } from "@/components/ui/pin-input";

/* mask: dots instead of digits, for a PIN someone could read over a shoulder */
export default function SecurityPin() {
  return <PinInput label="PIN to reveal production secrets" length={4} mask />;
}
```

Vue · `security-pin.vue`

```vue
<script setup lang="ts">
import { PinInput } from "@/components/ui/pin-input";
</script>

<!-- mask: dots instead of digits, for a PIN someone could read over a shoulder -->
<template>
  <PinInput label="PIN to reveal production secrets" :length="4" mask />
</template>
```

Svelte · `security-pin.svelte`

```svelte
<script lang="ts">
  import { PinInput } from "$lib/components/ui/pin-input";
</script>

<!-- mask: dots instead of digits, for a PIN someone could read over a shoulder -->
<PinInput label="PIN to reveal production secrets" length={4} mask />
```

## Recipe

`pin-input.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* pin-input — a one-time code or PIN typed one character per cell (Ark UI pin-input).
 * Each cell is the real Input, square at its size's height (the shared control's: lib/control.ts):
 *   xs 24 · sm 28 (4px apart) · md 32 (default) · lg 36 (6px apart)
 * Each cell is the real Input (fill, hairline, focus and invalid rings, 16px text on touch), made
 * square, its character centred in tabular figures. On a coarse pointer a cell is 40×40, so each
 * is a whole touch target. The engine moves focus from cell to cell, takes a pasted code whole and
 * fills one-time codes from the keyboard's suggestion (otp). */
export const pinInput = tv({
  slots: {
    base: "flex flex-col gap-1.5 min-w-0 max-w-full",
    label: "text-sm font-medium text-ink [overflow-wrap:anywhere] [unicode-bidi:plaintext]",
    control: "flex flex-wrap items-center",
    /* the Input, square */
    /* typing moves focus from cell to cell: the ring must not cross-fade on every character */
    input: "kbd-instant shrink-0 w-(--control-height) px-0 text-center tabular-nums pointer-coarse:size-10",
  },
  variants: {
    size: {
      xs: { control: "gap-1" },
      sm: { control: "gap-1" },
      md: { control: "gap-1.5" },
      lg: { control: "gap-1.5" },
    },
  },
  defaultVariants: { size: "md" },
});

export type PinInputRecipeProps = VariantProps<typeof pinInput>;
```
