Forms
OtpInput
A one-time code field drawn as separate boxes. Underneath it is one native input, so paste, SMS autofill and password managers just work.
import { OtpInput } from '@reactloom/ui';Examples
Verify a code
onComplete fires once every character is entered \u2014 typed, pasted or autofilled.
Enter the 6-digit code we sent to •••• 4821.
import { Button, OtpInput, Stack, Text } from '@reactloom/ui';
import { useState } from 'react';
export default function Example() {
const [code, setCode] = useState('');
const [status, setStatus] = useState('');
return (
<Stack gap="3" style={{ maxInlineSize: 420 }}>
<OtpInput
label="Verification code"
description="Enter the 6-digit code we sent to •••• 4821."
value={code}
onValueChange={setCode}
onComplete={(value) => setStatus(value === '123456' ? 'Verified.' : 'That code did not match. Try 123456.')}
/>
<Text size="sm" role="status">
{status}
</Text>
<Button variant="secondary" size="sm" onClick={() => setCode('')} style={{ justifySelf: 'start' }}>
Clear
</Button>
</Stack>
);
}Length, characters and masking
Incorrect PIN. You have 2 attempts left.
import { OtpInput, Stack } from '@reactloom/ui';
export default function Example() {
return (
<Stack gap="4">
<OtpInput label="Recovery code" length={8} characters="alphanumeric" size="sm" />
<OtpInput label="Card PIN" length={4} masked error="Incorrect PIN. You have 2 attempts left." />
</Stack>
);
}API reference
| Prop | Type | Default |
|---|---|---|
lengthNumber of characters. | number | 6 |
charactersAllowed characters; numeric shows a number keyboard. | 'numeric' | 'alphanumeric' | 'numeric' |
value / defaultValue / onValueChange | string | — |
onCompleteCalled when the code is full. | (value) => void | — |
maskedShow dots instead of characters. | boolean | false |
labelVisible label. | ReactNode | — |
description · errorHelp text and error, linked with aria-describedby. | ReactNode | — |
hideLabel · required · disabled · sizeAs on Input. | | — |
Customization
Extracted from the component’s source on every build, so it matches the code. How the layers fit together is explained in Customization.
CSS variables
| Variable | Default |
|---|---|
--pl-input-radius | var(--pl-radius-md |
State attributes
Target these in your CSS to style states without depending on class names.
| Attribute | Values |
|---|---|
[data-active] | present when on |
[data-disabled] | present when on |
[data-filled] | present when on |
[data-invalid] | present when on |
[data-size] | sm · md · lg |
Theme tokens it reads
--pl-color-bg-surface --pl-color-border-focus --pl-color-border-input --pl-color-danger-border --pl-color-danger-solid --pl-color-fg-default --pl-focus-ring-color --pl-focus-ring-width --pl-font-size-2xl --pl-font-size-md --pl-font-size-xl --pl-radius-md
Accessibility
- One labelled input with autocomplete="one-time-code", so iOS and Android offer codes from messages.
- Screen readers read it as a normal text field with its value; the boxes are decorative.
- Characters outside the allowed set are dropped.
| Key | Action |
|---|---|
| Type or paste | Fills the boxes in order |
| Backspace | Deletes the last character |
Usage guidelines
Do
- Tell people where the code was sent and offer a way to resend it.
- Verify on onComplete, and keep a submit button for people who prefer it.
Don’t
- Don’t split the code into several inputs — it breaks paste and autofill.