Skip to the content
human-kit design
10 — Form

Pin Input

One box per character, for a PIN or the code a message brings.

Installation

pnpm dlx @human-kit/design add pin-input
npx @human-kit/design add pin-input
yarn dlx @human-kit/design add pin-input
bunx @human-kit/design add pin-input

The command copies the files to src/lib/components/ui/pin-input. It also installs the packages that the files import.

Usage

<script lang="ts">
	import * as Field from '$lib/components/ui/field';
	import * as PinInput from '$lib/components/ui/pin-input';

	let code = $state('');
</script>

<Field.Root label="Verification code">
	{#snippet children(control)}
		<PinInput.Root {...control} otp bind:value={code} />
	{/snippet}
</Field.Root>

Examples

In a field

Spread the Field.Root's control on it: the label names the group, and the line under it describes every cell. otp lets the phone offer the code of the last text message, and split={3} reads it in two runs.

We sent it by text to the number ending in 4417.

Checking the code

onComplete runs once, when the last cell takes its character: check the code there, without a submit button. Clear the error on the next change, in onChange.

Guidelines

  • Use it for a short code of a fixed length that the person copies from somewhere else: a code sent by text or by email, a PIN.
  • For a code of no set length, a password or a number that is typed rather than copied, use an Input.Root.
  • Set otp for a code that arrives by text message: the phone fills every cell in one touch.
  • Use split for six or more characters, at the place the message groups them.
  • Use mask only for a secret the person chose, as a PIN. A one-time code is short-lived and better seen.
  • Use default in a form, and underline in a dense form where boxes would crowd.

Accessibility

  • The cells sit in a role="group", named by a Field.Root's label or by aria-label. With otp and no name it is called "Verification code".
  • Each cell is an <input> named "Digit 2 of 6", or "Character 2 of 6" when it takes letters.
  • Typing moves to the next cell, Backspace deletes and steps back, the arrows and Home and End move between cells. A paste fills the cells from the one in focus.
  • The value has no holes: the focus never lands past the first empty cell.
  • invalid puts aria-invalid on each cell, and the Field.Root's message is read with each of them.
  • disabled takes the cells out of the tab order; readonly keeps them in it, and the value does not change.

API reference

PropTypeDefault
valuestring

The characters typed, never longer than length. Bindable.

—
defaultValuestring

The value at the start, when you give no value.

—
lengthnumber

How many cells there are.

6
variant'default' | 'filled' | 'ghost' | 'underline'

The look of a cell: a white box or a line under it.

'default'
size'sm' | 'default' | 'lg'

The size of the cells and their characters.

'default'
splitnumber

A dash after this many cells, to read the code in two runs.

—
type'numeric' | 'alphanumeric' | 'alphabetic'

Which characters the cells take. numeric also brings up the number keyboard on a phone.

'numeric'
patternRegExp

A test of your own for one character, in place of the one of type.

—
otpboolean

Marks it as a one-time code, which the phone offers to fill from a text message.

false
maskboolean

Hides the characters, as a password field does.

false
placeholderstring

The text of an empty cell.

—
onChange(value: string, details: PinInputChangeDetails) => void

Called on each change, with its reason: input, paste, delete, clear or form-reset.

—
onComplete(value: string) => void

Called once, when the last cell takes a character.

—
controlledValueboolean

Stops writing back to value: it reports through onChange only, so the parent can refuse a change.

false
blurOnCompleteboolean

Moves the focus off the last cell when the code is complete.

false
invalidboolean

Turns the cells red and sets aria-invalid on each.

false
requiredboolean

Sets required on each cell.

false
readonlyboolean

Keeps the cells in the tab order, but the value does not change.

false
disabledboolean

Turns the cells off and takes them out of the tab order.

false
namestring

The name of the hidden input that carries the whole code in a form.

—
formstring

The id of the form of the hidden input, when the group is outside it.

—
elementHTMLDivElement | null

The group element. Bindable.

—

Plus every attribute of a <div>, on the group.