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-uiUsage
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
| Prop | Type | Default | Description |
|---|---|---|---|
maxLength | number | required | Number of characters. |
value | string | none | Controlled value. |
onChange | (value: string) => void | none | Called on every change. |
onComplete | (value: string) => void | none | Called when all slots are filled. |
defaultValue | string | none | Initial value when uncontrolled. |
pattern | string | none (any character) | Regular expression source the whole value must match, such as "^[0-9]+$". |
inputMode | string | "numeric" | Soft keyboard type. |
textAlign | "left" | "center" | "right" | "left" | Alignment of the hidden input's caret. |
pasteTransformer | (pasted: string) => string | none | Clean pasted text, for example strip dashes. |
pushPasswordManagerStrategy | "increase-width" | "none" | "increase-width" | Make room for a password manager's badge. |
autoComplete | string | "one-time-code" | Lets the OS offer SMS codes. |
disabled | boolean | false | Disable the input (a standard input attribute). |
containerClassName | string | none | Class for the wrapper. |
className | string | none | Class for the hidden input; disabled:cursor-not-allowed is built in. |
InputOTPSlot
| Prop | Type | Default | Description |
|---|---|---|---|
index | number | required | Which character this slot shows. |
InputOTPGroup, InputOTPSeparator
InputOTPGroup joins slots into one bordered row. InputOTPSeparator is a decorative dash, hidden from screen readers.