Skip to content

OtpInput

The short code that arrives by mail or message when signing in, typed one character per box. Pasting the code fills every box at once.

import { OtpInput } from '@sypnia/ui/react'

On this page
  1. A code of six digits
  2. Letters and numbers
  3. Disabled
  4. When to use it
  5. For everyone
  6. Props

A code of six digits

Type or paste «482 107». When the last box is filled the code is checked.

We sent six digits to laura@northwind.dev. For this example the code is 482107.

Code
import { useState } from 'react';
import { Field, OtpInput } from '@sypnia/ui/react';

export default function Basic() {
  const [code, setCode] = useState('');
  const [checked, setChecked] = useState<string | null>(null);
  const wrong = checked !== null && checked !== '482107' && code.length === 6;
  return (
    <Field label="Code from your mail" hint={checked === '482107' ? 'The code is right. You are in.' : 'We sent six digits to laura@northwind.dev. For this example the code is 482107.'}
      error={wrong ? 'That is not the code we sent. Check the mail again, or ask for a new one.' : undefined}>
      <OtpInput value={code} onChange={(c) => { setCode(c); if (c.length < 6) setChecked(null); }} onComplete={setChecked} />
    </Field>
  );
}

Letters and numbers

Four boxes that take letters too, for codes like the one that pairs a device.

It is on the screen of the device.

Code
import { Field, OtpInput } from '@sypnia/ui/react';

export default function Text() {
  return (
    <Field label="Pairing code" hint="It is on the screen of the device.">
      <OtpInput length={4} type="text" />
    </Field>
  );
}

Disabled

While the code is being checked, or after too many tries.

Too many tries. Ask for a new code in 5 minutes.

Code
import { Field, OtpInput } from '@sypnia/ui/react';

export default function States() {
  return (
    <Field label="Code from your mail" hint="Too many tries. Ask for a new code in 5 minutes.">
      <OtpInput disabled value="1234" />
    </Field>
  );
}

When to use it

Use it when

  • A code of four to eight characters confirms that the person has their mail or their phone.
  • The code should be sent for checking as soon as the last box is filled: onComplete.

Do not use it when

  • It is a password or a long key: PasswordInput or SecretField.
  • It is any other short number (a quantity, a year): NumberInput or Input.

For everyone

  • Typing moves to the next box; Backspace empties the box and goes back; left and right arrows move between boxes.
  • Pasting fills from the first box, ignoring spaces and dashes. Phones offer the code from the message, because the first box says autocomplete="one-time-code".
  • The boxes form a group named by its label; each one is announced as «Character 2 of 6».
  • On phones the number keys open for codes of digits.
  • Inside a Field it takes the label, the hint and the error by itself; alone, give it an aria-label.

Props

OtpInputTypeDefault
aria-describedby string
aria-label

The name for screen readers when there is no Field around it.

string
aria-labelledby string
autoFocus boolean
className string
disabled boolean
id

Inside a Field it comes from the Field.

string
invalid

Marks the value as wrong. Inside a `<Field error>` it is set for you.

boolean
length

How many boxes.

number 6
onChange ((code: string) => void)
onComplete

Called when the last box is filled: the moment to check the code.

((code: string) => void)
type

digits: numbers only. text: letters and numbers.

"text" | "digits" digits
value string
  1. A code of six digits
  2. Letters and numbers
  3. Disabled
  4. When to use it
  5. For everyone
  6. Props

    Search pages, sections and terms