Skip to Content

Input OTP

A one-time-code input with a slot per character.

import { InputOTP, InputOTPGroup, InputOTPSlot } from "@dapplelight/signet-ui/components/input-otp"

export function Demo() {
  return (
    <InputOTP maxLength={6} aria-label="Verification code">
      <InputOTPGroup>
        <InputOTPSlot index={0} />
        <InputOTPSlot index={1} />
        <InputOTPSlot index={2} />
        <InputOTPSlot index={3} />
        <InputOTPSlot index={4} />
        <InputOTPSlot index={5} />
      </InputOTPGroup>
    </InputOTP>
  )
}

Installation

npm i @dapplelight/signet-ui

Usage

import {
  InputOTP,
  InputOTPGroup,
  InputOTPSeparator,
  InputOTPSlot,
} from "@dapplelight/signet-ui/components/input-otp"

<InputOTP maxLength={6}>
  <InputOTPGroup>
    <InputOTPSlot index={0} />
    <InputOTPSlot index={1} />
  </InputOTPGroup>
</InputOTP>

Examples

Separator

import { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot } from "@dapplelight/signet-ui/components/input-otp"

export function Demo() {
  return (
    <InputOTP maxLength={6} aria-label="Verification code">
      <InputOTPGroup>
        <InputOTPSlot index={0} />
        <InputOTPSlot index={1} />
        <InputOTPSlot index={2} />
      </InputOTPGroup>
      <InputOTPSeparator />
      <InputOTPGroup>
        <InputOTPSlot index={3} />
        <InputOTPSlot index={4} />
        <InputOTPSlot index={5} />
      </InputOTPGroup>
    </InputOTP>
  )
}

Controlled

Enter the 4-digit PIN

import { useState } from "react"
import { InputOTP, InputOTPGroup, InputOTPSlot } from "@dapplelight/signet-ui/components/input-otp"

export function Demo() {
  const [code, setCode] = useState("")
  return (
    <div className="space-y-2">
      <InputOTP
        maxLength={4}
        pattern="^[0-9]+$"
        inputMode="numeric"
        value={code}
        onChange={setCode}
        aria-label="PIN"
      >
        <InputOTPGroup>
          <InputOTPSlot index={0} />
          <InputOTPSlot index={1} />
          <InputOTPSlot index={2} />
          <InputOTPSlot index={3} />
        </InputOTPGroup>
      </InputOTP>
      <p className="text-sm text-muted-foreground">
        {code.length === 4 ? `Code ${code} entered` : "Enter the 4-digit PIN"}
      </p>
    </div>
  )
}

Disabled

4
8
2
1
import { InputOTP, InputOTPGroup, InputOTPSlot } from "@dapplelight/signet-ui/components/input-otp"

export function Demo() {
  return (
    <InputOTP maxLength={4} disabled defaultValue="4821" aria-label="PIN">
      <InputOTPGroup>
        <InputOTPSlot index={0} />
        <InputOTPSlot index={1} />
        <InputOTPSlot index={2} />
        <InputOTPSlot index={3} />
      </InputOTPGroup>
    </InputOTP>
  )
}

API Reference

Built on the input-otp package. InputOTP takes its props; screen readers see one text input.

InputOTP

PropTypeDefaultDescription
maxLengthnumberrequiredNumber of characters.
valuestringnoneControlled value.
onChange(value: string) => voidnoneCalled on every change.
onComplete(value: string) => voidnoneCalled when all slots are filled.
defaultValuestringnoneInitial value when uncontrolled.
patternstringnone (any character)Regular expression source the whole value must match, such as "^[0-9]+$".
inputModestring"numeric"Soft keyboard type.
textAlign"left" | "center" | "right""left"Alignment of the hidden input's caret.
pasteTransformer(pasted: string) => stringnoneClean pasted text, for example strip dashes.
pushPasswordManagerStrategy"increase-width" | "none""increase-width"Make room for a password manager's badge.
autoCompletestring"one-time-code"Lets the OS offer SMS codes.
disabledbooleanfalseDisable the input (a standard input attribute).
containerClassNamestringnoneClass for the wrapper.
classNamestringnoneClass for the hidden input; disabled:cursor-not-allowed is built in.

InputOTPSlot

PropTypeDefaultDescription
indexnumberrequiredWhich character this slot shows.

InputOTPGroup, InputOTPSeparator

InputOTPGroup joins slots into one bordered row. InputOTPSeparator is a decorative dash, hidden from screen readers.