Examples

Verify a code

onComplete fires once every character is entered \u2014 typed, pasted or autofilled.

Preview

Enter the 6-digit code we sent to •••• 4821.

Example.tsx
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

Preview

Incorrect PIN. You have 2 attempts left.

Example.tsx
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

OtpInput props
PropTypeDefault
length
Number of characters.
number6
characters
Allowed characters; numeric shows a number keyboard.
'numeric' | 'alphanumeric''numeric'
value / defaultValue / onValueChange
string—
onComplete
Called when the code is full.
(value) => void—
masked
Show dots instead of characters.
booleanfalse
label
Visible label.
ReactNode—
description · error
Help text and error, linked with aria-describedby.
ReactNode—
hideLabel · required · disabled · size
As 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

VariableDefault
--pl-input-radiusvar(--pl-radius-md

State attributes

Target these in your CSS to style states without depending on class names.

AttributeValues
[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.
Keyboard interactions
KeyAction
Type or pasteFills the boxes in order
BackspaceDeletes 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.