Theme
Customize appearance
Components
Input Otp
Collect a short verification code in visually separated slots.
View component source ↗Preview
A minimal example to get started.
Installation
Install this component and its dependencies in a project initialized with shadcn.
pnpm dlx shadcn@latest add https://ui.hyunjoon.net/r/input-otp.jsonUsage
This is the basic example. Use the Code tab above for the selected example. Adjust import aliases to match your project.
import { InputOTP, InputOTPGroup, InputOTPSlot } from "@/components/ui/input-otp";
export default function Example() {
return (
<InputOTP maxLength={6} aria-label="Verification code">
<InputOTPGroup>
{[0, 1, 2, 3, 4, 5].map((index) => (
<InputOTPSlot key={index} index={index} />
))}
</InputOTPGroup>
</InputOTP>
);
}
API reference
Derived from the installed component types. Native DOM props are also supported where applicable. A dash means no explicit default is documented. Some props depend on the selected mode.
InputOTP
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | undefinedstring | undefined | — | Additional CSS classes merged with the component styles. |
defaultValue | string | number | readonly string[] | undefinedstring | number | readonly string[] | undefined | — | Initial value for an uncontrolled component. |
defaultChecked | boolean | undefinedboolean | undefined | — | Initial checked state for an uncontrolled control. |
id | string | undefinedstring | undefined | — | Unique identifier used to associate labels and descriptions. |
children | undefined | React.ReactNodeundefined | React.ReactNode | — | Content rendered inside this component. |
onFocus | React.FocusEventHandler<HTMLInputElement> | undefinedReact.FocusEventHandler<HTMLInputElement> | undefined | — | Called when the control receives focus. |
onBlur | React.FocusEventHandler<HTMLInputElement> | undefinedReact.FocusEventHandler<HTMLInputElement> | undefined | — | Called when the control loses focus. |
onKeyDown | React.KeyboardEventHandler<HTMLInputElement> | undefinedReact.KeyboardEventHandler<HTMLInputElement> | undefined | — | Called when a key is pressed while this element is focused. |
onClick | React.MouseEventHandler<HTMLInputElement> | undefinedReact.MouseEventHandler<HTMLInputElement> | undefined | — | Called when the element is activated. |
disabled | boolean | undefinedboolean | undefined | — | Prevents user interaction. |
name | string | undefinedstring | undefined | — | Field name used in form data. |
type | React.HTMLInputTypeAttribute | undefinedReact.HTMLInputTypeAttribute | undefined | — | The native input or button behavior. |
alt | string | undefinedstring | undefined | — | Accessible text alternative for the image. |
src | string | undefinedstring | undefined | — | URL of the displayed resource. |
multiple | boolean | undefinedboolean | undefined | — | Allows more than one value to be selected. |
max | string | number | undefinedstring | number | undefined | — | Maximum allowed value. |
min | string | number | undefinedstring | number | undefined | — | Minimum allowed value. |
step | string | number | undefinedstring | number | undefined | — | Increment between valid values. |
required | boolean | undefinedboolean | undefined | — | Whether the user must provide a value before form submission. |
checked | boolean | undefinedboolean | undefined | — | Current checked state when controlled. |
placeholder | string | undefinedstring | undefined | — | Hint shown when the control has no value. |
value | string | undefinedstring | undefined | — | The current value when controlled by the application. |
onChange | ((newValue: string) => unknown) | undefined((newValue: string) => unknown) | undefined | — | Called when the underlying form control changes; inspect the event for its value. |
maxLengthRequired | numbernumber | — | Maximum number of input characters. |
textAlign | "center" | "left" | "right" | undefined"center" | "left" | "right" | undefined | — | Configures text align; see the accepted type. |
onComplete | ((...args: any[]) => unknown) | undefined((...args: any[]) => unknown) | undefined | — | Callback for complete events. See the signature for parameters. |
pushPasswordManagerStrategy | "none" | "increase-width" | undefined"none" | "increase-width" | undefined | — | Configures push password manager strategy; see the accepted type. |
pasteTransformer | ((pasted: string) => string) | undefined((pasted: string) => string) | undefined | — | Configures paste transformer; see the accepted type. |
containerClassName | string | undefinedstring | undefined | — | Configures container class name; see the accepted type. |
noScriptCSSFallback | string | null | undefinedstring | null | undefined | — | Configures no script cssfallback; see the accepted type. |
nonce | string | undefinedstring | undefined | — | Configures nonce; see the accepted type. |
render | InputOTPRenderFn | undefinedInputOTPRenderFn | undefined | — | Element or render function used to compose a custom root element. |
ref | React.Ref<HTMLInputElement> | undefinedReact.Ref<HTMLInputElement> | undefined | — | Allows getting a ref to the component instance. Once the component unmounts, React will set `ref.current` to `null` (or call the ref with `null` if you passed a callback ref). |
InputOTPGroup
| Prop | Type | Default | Description |
|---|---|---|---|
ref | React.Ref<HTMLDivElement> | undefinedReact.Ref<HTMLDivElement> | undefined | — | Allows getting a ref to the component instance. Once the component unmounts, React will set `ref.current` to `null` (or call the ref with `null` if you passed a callback ref). |
defaultChecked | boolean | undefinedboolean | undefined | — | Initial checked state for an uncontrolled control. |
defaultValue | string | number | readonly string[] | undefinedstring | number | readonly string[] | undefined | — | Initial value for an uncontrolled component. |
className | string | undefinedstring | undefined | — | Additional CSS classes merged with the component styles. |
id | string | undefinedstring | undefined | — | Unique identifier used to associate labels and descriptions. |
children | React.ReactNodeReact.ReactNode | — | Content rendered inside this component. |
onFocus | React.FocusEventHandler<HTMLDivElement> | undefinedReact.FocusEventHandler<HTMLDivElement> | undefined | — | Called when the control receives focus. |
onBlur | React.FocusEventHandler<HTMLDivElement> | undefinedReact.FocusEventHandler<HTMLDivElement> | undefined | — | Called when the control loses focus. |
onChange | React.ChangeEventHandler<HTMLDivElement, Element> | undefinedReact.ChangeEventHandler<HTMLDivElement, Element> | undefined | — | Called when the underlying form control changes; inspect the event for its value. |
onKeyDown | React.KeyboardEventHandler<HTMLDivElement> | undefinedReact.KeyboardEventHandler<HTMLDivElement> | undefined | — | Called when a key is pressed while this element is focused. |
onClick | React.MouseEventHandler<HTMLDivElement> | undefinedReact.MouseEventHandler<HTMLDivElement> | undefined | — | Called when the element is activated. |
InputOTPSlot
| Prop | Type | Default | Description |
|---|---|---|---|
ref | React.Ref<HTMLDivElement> | undefinedReact.Ref<HTMLDivElement> | undefined | — | Allows getting a ref to the component instance. Once the component unmounts, React will set `ref.current` to `null` (or call the ref with `null` if you passed a callback ref). |
defaultChecked | boolean | undefinedboolean | undefined | — | Initial checked state for an uncontrolled control. |
defaultValue | string | number | readonly string[] | undefinedstring | number | readonly string[] | undefined | — | Initial value for an uncontrolled component. |
className | string | undefinedstring | undefined | — | Additional CSS classes merged with the component styles. |
id | string | undefinedstring | undefined | — | Unique identifier used to associate labels and descriptions. |
children | React.ReactNodeReact.ReactNode | — | Content rendered inside this component. |
onFocus | React.FocusEventHandler<HTMLDivElement> | undefinedReact.FocusEventHandler<HTMLDivElement> | undefined | — | Called when the control receives focus. |
onBlur | React.FocusEventHandler<HTMLDivElement> | undefinedReact.FocusEventHandler<HTMLDivElement> | undefined | — | Called when the control loses focus. |
onChange | React.ChangeEventHandler<HTMLDivElement, Element> | undefinedReact.ChangeEventHandler<HTMLDivElement, Element> | undefined | — | Called when the underlying form control changes; inspect the event for its value. |
onKeyDown | React.KeyboardEventHandler<HTMLDivElement> | undefinedReact.KeyboardEventHandler<HTMLDivElement> | undefined | — | Called when a key is pressed while this element is focused. |
onClick | React.MouseEventHandler<HTMLDivElement> | undefinedReact.MouseEventHandler<HTMLDivElement> | undefined | — | Called when the element is activated. |
indexRequired | numbernumber | — | Configures index; see the accepted type. |
InputOTPSeparator
| Prop | Type | Default | Description |
|---|---|---|---|
ref | React.Ref<HTMLDivElement> | undefinedReact.Ref<HTMLDivElement> | undefined | — | Allows getting a ref to the component instance. Once the component unmounts, React will set `ref.current` to `null` (or call the ref with `null` if you passed a callback ref). |
defaultChecked | boolean | undefinedboolean | undefined | — | Initial checked state for an uncontrolled control. |
defaultValue | string | number | readonly string[] | undefinedstring | number | readonly string[] | undefined | — | Initial value for an uncontrolled component. |
className | string | undefinedstring | undefined | — | Additional CSS classes merged with the component styles. |
id | string | undefinedstring | undefined | — | Unique identifier used to associate labels and descriptions. |
children | React.ReactNodeReact.ReactNode | — | Content rendered inside this component. |
onFocus | React.FocusEventHandler<HTMLDivElement> | undefinedReact.FocusEventHandler<HTMLDivElement> | undefined | — | Called when the control receives focus. |
onBlur | React.FocusEventHandler<HTMLDivElement> | undefinedReact.FocusEventHandler<HTMLDivElement> | undefined | — | Called when the control loses focus. |
onChange | React.ChangeEventHandler<HTMLDivElement, Element> | undefinedReact.ChangeEventHandler<HTMLDivElement, Element> | undefined | — | Called when the underlying form control changes; inspect the event for its value. |
onKeyDown | React.KeyboardEventHandler<HTMLDivElement> | undefinedReact.KeyboardEventHandler<HTMLDivElement> | undefined | — | Called when a key is pressed while this element is focused. |
onClick | React.MouseEventHandler<HTMLDivElement> | undefinedReact.MouseEventHandler<HTMLDivElement> | undefined | — | Called when the element is activated. |
Usage notes
Set maxLength to the expected code length. Slot indexes start at zero and must remain within that length.