otp

Six empty boxes with a caret in the first.

otp is a field for a one-time code, such as the six digits a site texts you. It shows one box per character, but a person types into one real text input.

import { otp } from '@markless/ui';

export default function Code() @{
	<otp.root>
		<otp.field aria-label="Verification code" />
		<otp.item><otp.itemindicator>|</otp.itemindicator></otp.item>
		<otp.item><otp.itemindicator>|</otp.itemindicator></otp.item>
		<otp.item><otp.itemindicator>|</otp.itemindicator></otp.item>
		<otp.item><otp.itemindicator>|</otp.itemindicator></otp.item>
		<otp.item><otp.itemindicator>|</otp.itemindicator></otp.item>
		<otp.item><otp.itemindicator>|</otp.itemindicator></otp.item>
	</otp.root>
}

There is no length prop. Six otp.item boxes make a six-character code. Each box shows the character at its own place in the row, so you never number them. A box you write in an @for loop works the same way.

One input under the boxes

otp.field is a plain <input>. It is invisible, and it covers every box. A click anywhere on the boxes lands on it. Because it is a real input, paste, undo and the browser's one-time-code autofill work with no extra code.

The family writes these attributes on the input:

  • type="text" and inputmode="numeric", so phones show a number pad.
  • autocomplete="one-time-code", so the browser can offer the code from a text message.
  • maxlength, set to the number of boxes.

Your own attributes, such as pattern or name, reach the input too. The family's own values win. The tests check that a maxlength you write cannot replace the box count.

Anatomy

otp anatomy
Anatomy perspectiveFlat
Layers
248
otp.root

The box the field is stretched over, so style is the family's: it is what makes the field measure itself against this element rather than the page.

  • otp.root is a <div> around the boxes. The input measures itself against it.
  • otp.field is the one <input>. It is the only form control in the widget.
  • otp.item is one box. It shows one character of the code.
  • otp.itemindicator is a <span> inside a box, for a caret you draw. It has no state.

otp.item writes ui-empty while it has no character. The root and every box write ui-disabled when you set disabled.

What the tests check

  • Each keystroke fills the next box.
  • Backspace removes the last character.
  • Typing past the last box adds nothing.
  • A pasted code fills every box. A longer code keeps one character per box.
  • Focus on the field puts the caret after the last character.
  • In a form, the code submits under the name you give otp.field.

Props on otp.root

Prop Type What it does
value string The code entered so far. Omit it and the field starts empty.
disabled boolean Stops all typing.
shiftPWManagers boolean Keeps a password manager's icon off the boxes. It is on by default.
onChange (value) => void Gets the whole code each time it changes. It does not get the one character typed.
onComplete (value) => void Gets the whole code once, when every box holds a character.

The root passes anything a <div> accepts to its element, except style. otp.field takes anything an <input> accepts, except value and style.