Input OTP
Accessible one-time password component with copy-paste functionality.
About
The InputOTP component uses input-otp by Guilherme Rodz.
Installation
pnpm dlx cubix@latest add input-otp --base ariaUsage
import {
InputOTP,
InputOTPGroup,
InputOTPSeparator,
InputOTPSlot,
} from "@/components/cubix/input-otp"<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
└── InputOTPSlotPattern
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.
Disabled
Use the disabled prop to disable the input.
Controlled
Use the value and onChange props to control the input value.
Invalid
Use aria-invalid on the slots to show an error state.
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.
API Reference
See the input-otp documentation for the full primitive API.
InputOTP
| Prop | Type | Default | Description |
|---|---|---|---|
| maxLength | number | - | Maximum number of characters the OTP input accepts. |
| value | string | - | Controlled value of the OTP input. |
| defaultValue | string | - | Initial value when uncontrolled. |
| onChange | (value: string) => void | - | Called when the OTP value changes. |
| pattern | string | - | Regular expression pattern for allowed characters. Use REGEXP_ONLY_DIGITS or REGEXP_ONLY_DIGITS_AND_CHARS from input-otp. |
| disabled | boolean | - | Disables the OTP input. |
| containerClassName | string | - | Classes applied to the OTP container element. |
| className | string | - | Additional Tailwind classes merged with the component styles (last one wins). |
| children | React.ReactNode | - | Groups, slots, and separators that compose the OTP input. |
InputOTPGroup
| Prop | Type | Default | Description |
|---|---|---|---|
| variant | "default" | "separated" | "default" | Layout of the slots. Use separated to add space between individual inputs. |
| className | string | - | Additional Tailwind classes merged with the component styles (last one wins). |
| children | React.ReactNode | - | One or more InputOTPSlot elements. |
InputOTPSlot
| Prop | Type | Default | Description |
|---|---|---|---|
| index | number | - | 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-invalid | boolean | - | Marks the slot as invalid and applies destructive styles. |
| className | string | - | Additional Tailwind classes merged with the component styles (last one wins). |
InputOTPSeparator
| Prop | Type | Default | Description |
|---|---|---|---|
| className | string | - | Additional Tailwind classes merged with the component styles (last one wins). |