Input OTP

Accessible one-time password component with copy-paste functionality.

1
2
3
4
5
6

About

The InputOTP component uses input-otp by Guilherme Rodz.

Installation

pnpm dlx cubix@latest add input-otp

Usage

Import
import {
  InputOTP,
  InputOTPGroup,
  InputOTPSeparator,
  InputOTPSlot,
} from "@/components/cubix/input-otp"
Example
<InputOTP maxLength={6}>
  <InputOTPGroup>
    <InputOTPSlot index={0} />
    <InputOTPSlot index={1} />
    <InputOTPSlot index={2} />
  </InputOTPGroup>
  <InputOTPSeparator />
  <InputOTPGroup>
    <InputOTPSlot index={3} />
    <InputOTPSlot index={4} />
    <InputOTPSlot index={5} />
  </InputOTPGroup>
</InputOTP>

Composition

Use the following composition to build an InputOTP:

 
InputOTP
├── InputOTPGroup
   ├── InputOTPSlot
   ├── InputOTPSlot
   └── InputOTPSlot
├── InputOTPSeparator
├── InputOTPGroup
   ├── InputOTPSlot
   ├── InputOTPSlot
   └── InputOTPSlot
├── InputOTPSeparator
└── InputOTPGroup
    ├── InputOTPSlot
    └── InputOTPSlot

Pattern

Use the pattern prop to define a custom pattern for the OTP input.

Separator

Use <InputOTPSeparator /> to add a separator between input groups.

Separated

Use variant="separated" on InputOTPGroup and InputOTPSlot for standalone inputs with space between them.

1
2
3
4
5
6

Disabled

Use the disabled prop to disable the input.

1
2
3
4
5
6

Controlled

Use the value and onChange props to control the input value.

Enter your one-time password.

Invalid

Use aria-invalid on the slots to show an error state.

0
0
0
0
0
0

Four Digits

A common pattern for PIN codes. This uses the pattern={REGEXP_ONLY_DIGITS} prop.

Alphanumeric

Use REGEXP_ONLY_DIGITS_AND_CHARS to accept both letters and numbers.

Form

Compose InputOTP with Card and Field for a verification form.

Verify your login
Enter the verification code we sent to your email address: m@example.com.

I no longer have access to this email address.

Having trouble signing in? Contact support

API Reference

See the input-otp documentation for the full primitive API.

InputOTP

PropTypeDefaultDescription
maxLengthnumber-Maximum number of characters the OTP input accepts.
valuestring-Controlled value of the OTP input.
defaultValuestring-Initial value when uncontrolled.
onChange(value: string) => void-Called when the OTP value changes.
patternstring-Regular expression pattern for allowed characters. Use REGEXP_ONLY_DIGITS or REGEXP_ONLY_DIGITS_AND_CHARS from input-otp.
disabledboolean-Disables the OTP input.
containerClassNamestring-Classes applied to the OTP container element.
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).
childrenReact.ReactNode-Groups, slots, and separators that compose the OTP input.

InputOTPGroup

PropTypeDefaultDescription
variant"default" | "separated""default"Layout of the slots. Use separated to add space between individual inputs.
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).
childrenReact.ReactNode-One or more InputOTPSlot elements.

InputOTPSlot

PropTypeDefaultDescription
indexnumber-Zero-based index of this slot within the OTP input.
variant"default" | "separated""default"Visual style of the slot. Use separated for standalone bordered inputs.
aria-invalidboolean-Marks the slot as invalid and applies destructive styles.
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).

InputOTPSeparator

PropTypeDefaultDescription
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).