Skip to content

Input OTP

One-time code entry for 2FA, sign-in verification, and PIN screens — paste-aware slots with neobrutalist borders and hard shadows.

import {
  InputOTP,
  InputOTPGroup,

The input OTP is a segmented one-time-code field: visual slots rendered over a single native input, so paste and mobile code autofill work out of the box. Both backend variants wrap the same input-otp library by @guilherme_rodz — there is no Radix or Base primitive underneath — with slots styled to the neobrutalist recipe: thick borders, hard shadows, and bold type.

Reach for it when:

  • Two-factor auth — the 6-digit TOTP or SMS code after password sign-in.
  • Email and phone verification — confirm ownership during signup, checkout, or account recovery.
  • PIN entry — 4-digit codes for screen locks or payment confirmation (see the Four Digits example).

Installation

pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/input-otp.json
npx shadcn@latest add https://neobrutalism.com/r/base/input-otp.json
yarn dlx shadcn@latest add https://neobrutalism.com/r/base/input-otp.json
bunx --bun shadcn@latest add https://neobrutalism.com/r/base/input-otp.json

Install the following dependencies:

pnpm add input-otp
npm install input-otp
yarn add input-otp
bun add input-otp

Copy and paste the following code into your project.

components/ui/input-otp.tsx
"use client"

import * as React from "react"
import { OTPInput, OTPInputContext } from "input-otp"
import { MinusIcon } from "lucide-react"

import { cn } from "@/lib/utils"

function InputOTP({
  className,
  containerClassName,
  ...props
}: React.ComponentProps<typeof OTPInput> & {
  containerClassName?: string
}) {
  return (
    <OTPInput
      data-slot="input-otp"
      containerClassName={cn(
        "cn-input-otp flex items-center has-disabled:opacity-50",
        containerClassName
      )}
      spellCheck={false}
      className={cn("disabled:cursor-not-allowed", className)}
      {...props}
    />
  )
}

function InputOTPGroup({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="input-otp-group"
      className={cn(
        "flex items-center rounded has-aria-invalid:border-destructive",
        className
      )}
      {...props}
    />
  )
}

function InputOTPSlot({
  index,
  className,
  ...props
}: React.ComponentProps<"div"> & {
  index: number
}) {
  const inputOTPContext = React.useContext(OTPInputContext)
  const { char, hasFakeCaret, isActive } = inputOTPContext?.slots[index] ?? {}

  return (
    <div
      data-slot="input-otp-slot"
      data-active={isActive}
      className={cn(
        "relative flex size-8 items-center justify-center border-y-2 border-r-2 bg-input text-sm shadow-sm transition-all outline-none first:rounded-l first:border-l-2 last:rounded-r aria-invalid:border-destructive data-[active=true]:z-10 data-[active=true]:outline-2 data-[active=true]:outline-primary data-[active=true]:aria-invalid:border-destructive",
        className
      )}
      {...props}
    >
      {char}
      {hasFakeCaret && (
        <div className="pointer-events-none absolute inset-0 flex items-center justify-center">
          <div className="h-4 w-px animate-caret-blink bg-foreground duration-1000" />
        </div>
      )}
    </div>
  )
}

function InputOTPSeparator({ ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="input-otp-separator"
      className="flex items-center [&_svg:not([class*='size-'])]:size-4"
      role="separator"
      {...props}
    >
      <MinusIcon />
    </div>
  )
}

export { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator }

Update the import paths to match your project setup.

Usage

import {
  InputOTP,
  InputOTPGroup,
  InputOTPSeparator,
  InputOTPSlot,
} from "@/components/ui/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
    └── InputOTPSlot

Pattern

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

import { REGEXP_ONLY_DIGITS_AND_CHARS } from "input-otp"
 
;<InputOTP maxLength={6} pattern={REGEXP_ONLY_DIGITS_AND_CHARS}>
  ...
</InputOTP>
"use client"

import { REGEXP_ONLY_DIGITS } from "input-otp"

Examples

Separator

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

import {
  InputOTP,
  InputOTPGroup,

Disabled

Use the disabled prop to disable the input.

import { Field, FieldLabel } from "@/components/ui/field"
import {
  InputOTP,

Controlled

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

"use client"

import * as React from "react"

Invalid

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

"use client"

import * as React from "react"

Four Digits

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

"use client"

import { REGEXP_ONLY_DIGITS } from "input-otp"

Alphanumeric

Use REGEXP_ONLY_DIGITS_AND_CHARS to accept both letters and numbers.

"use client"

import { REGEXP_ONLY_DIGITS_AND_CHARS } from "input-otp"

Form

import { RefreshCwIcon } from "lucide-react"

import { Button } from "@/components/ui/button"

RTL

To enable RTL support in Neobrutalism.com, see the RTL configuration guide.

"use client"

import * as React from "react"

Accessibility

There is no WAI-ARIA APG pattern for OTP fields, and none is needed: input-otp renders one real <input> with autocomplete="one-time-code" behind the visual slots, so assistive tech announces a single text field and mobile keyboards can suggest the incoming code. The slots are presentational — label the field itself (a FormLabel or aria-label), and pass aria-invalid to the slots for error styling.

Keyboard interactions follow native text-field behavior and are identical in the Radix and Base variants:

KeyAction
Tab / Shift + TabMove focus into / out of the field (one tab stop)
ArrowLeft / ArrowRightMove the caret between slots
Home / EndJump to the first / last slot
Backspace / DeleteDelete the previous / next character
Ctrl/Cmd + VPaste — the code fills the slots if it matches pattern

API Reference

See the input-otp documentation for more information.