Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Input OTP examples · Behavior

OTP with Enhanced Focus Management Input OTP for React

A one-time code field wrapped in a label, so a click anywhere on it focuses the code.

Framework
UI library

Small boxes are easy to miss. This example wraps the field in a Label, so a click anywhere on it, including the gaps and the separator, focuses the code and people can start typing. A hidden label text names the field for screen readers.

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

FAQ

How does a click anywhere focus the field?

A label sends a click on itself to the first field inside it. Wrapping InputOTP in Label makes the whole area, gaps included, a click target for the code.

How is the field named for screen readers?

A span with sr-only inside the label reads One-time password, which becomes the field's accessible name.

Why the text cursor?

cursor-text on the label shows that the area takes typing, not a button press.

What happens when I click a box directly?

The slots are drawn over one input that covers the field, so a click on any box focuses it and the caret goes to the next empty slot.

Does the keyboard work as usual?

Yes. Tab reaches the field, and the arrow keys and Backspace move between slots. The label adds a pointer target, not another tab stop.

How is the value kept?

In state, written on every change. The value is one string, and onChange receives the next string.