Pin Input
One box per character, for a PIN or the code a message brings.
Installation
pnpm dlx @human-kit/design add pin-inputnpx @human-kit/design add pin-inputyarn dlx @human-kit/design add pin-inputbunx @human-kit/design add pin-inputThe 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
otpfor a code that arrives by text message: the phone fills every cell in one touch. - Use
splitfor six or more characters, at the place the message groups them. - Use
maskonly for a secret the person chose, as a PIN. A one-time code is short-lived and better seen. - Use
defaultin a form, andunderlinein a dense form where boxes would crowd.
Accessibility
- The cells sit in a
role="group", named by aField.Root's label or byaria-label. Withotpand 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,
Backspacedeletes and steps back, the arrows andHomeandEndmove 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.
invalidputsaria-invalidon each cell, and theField.Root's message is read with each of them.disabledtakes the cells out of the tab order;readonlykeeps them in it, and the value does not change.
API reference
| Prop | Type | Default |
|---|---|---|
value | string The characters typed, never longer than | — |
defaultValue | string The value at the start, when you give no | — |
length | number 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' |
split | number A dash after this many cells, to read the code in two runs. | — |
type | 'numeric' | 'alphanumeric' | 'alphabetic' Which characters the cells take. | 'numeric' |
pattern | RegExp A test of your own for one character, in place of the one of | — |
otp | boolean Marks it as a one-time code, which the phone offers to fill from a text message. | false |
mask | boolean Hides the characters, as a password field does. | false |
placeholder | string The text of an empty cell. | — |
onChange | (value: string, details: PinInputChangeDetails) => void Called on each change, with its reason: | — |
onComplete | (value: string) => void Called once, when the last cell takes a character. | — |
controlledValue | boolean Stops writing back to | false |
blurOnComplete | boolean Moves the focus off the last cell when the code is complete. | false |
invalid | boolean Turns the cells red and sets | false |
required | boolean Sets | false |
readonly | boolean Keeps the cells in the tab order, but the value does not change. | false |
disabled | boolean Turns the cells off and takes them out of the tab order. | false |
name | string The name of the hidden input that carries the whole code in a form. | — |
form | string The id of the form of the hidden input, when the group is outside it. | — |
element | HTMLDivElement | null The group element. Bindable. | — |
Plus every attribute of a <div>, on the group.