# Input Otp

shadcn input-otp reskinned for UI95.

Source: https://www.ui95.dev/ui/input-otp

## Examples

```tsx
"use client"

import * as React from "react"

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

import type { ShowcaseDemo } from "../types"

function Slots({ from, count }: { from: number; count: number }) {
  return (
    <InputOTPGroup>
      {Array.from({ length: count }, (_, i) => (
        <InputOTPSlot key={from + i} index={from + i} />
      ))}
    </InputOTPGroup>
  )
}

function useSeed(seed: string) {
  const [value, setValue] = React.useState(seed)
  return { value, onChange: setValue }
}

function SixDigits() {
  return (
    <InputOTP maxLength={6} {...useSeed("")}>
      <Slots from={0} count={6} />
    </InputOTP>
  )
}

function Split() {
  return (
    <InputOTP maxLength={6} {...useSeed("640")}>
      <Slots from={0} count={3} />
      <InputOTPSeparator />
      <Slots from={3} count={3} />
    </InputOTP>
  )
}

function Disabled() {
  return (
    <InputOTP maxLength={4} disabled {...useSeed("1981")}>
      <Slots from={0} count={4} />
    </InputOTP>
  )
}

function Invalid() {
  return (
    <InputOTP maxLength={4} aria-invalid {...useSeed("404")}>
      <Slots from={0} count={4} />
    </InputOTP>
  )
}

function Alphanumeric() {
  return (
    <InputOTP maxLength={4} pattern="^[a-zA-Z0-9]+$" {...useSeed("A1")}>
      <Slots from={0} count={4} />
    </InputOTP>
  )
}

export const demo: ShowcaseDemo = {
  span: "wide",
  variants: [
    { label: "6 digits", render: () => <SixDigits /> },
    { label: "3 + 3 separator", render: () => <Split /> },
    { label: "disabled", render: () => <Disabled /> },
    { label: "invalid", render: () => <Invalid /> },
    { label: "alphanumeric", render: () => <Alphanumeric /> },
  ],
}
```

## Installation

```bash
npx shadcn@latest add https://www.ui95.dev/r/input-otp.json
```

Run it in a Radix shadcn project that already has the UI95 theme.

Dependencies:

```bash
npm install cn input-otp
```

Add these items first:

- [pixel-glyphs](https://www.ui95.dev/ui/pixel-glyphs)

`components/ui/input-otp.tsx`

```tsx
"use client"

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

import { MinusGlyph } from "@/components/ui/pixel-glyphs"

function InputOTP({
  className,
  containerClassName,
  ...props
}: React.ComponentProps<typeof OTPInput> & {
  containerClassName?: string
}) {
  return (
    <OTPInput
      data-slot="input-otp"
      containerClassName={cn(
        "group/otp flex items-center has-disabled:text-muted-foreground",
        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", 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-10 items-center justify-center border-(length:--pixel) border-border well bg-input text-foreground font-pixel text-[16px] leading-none uppercase not-first:border-l-0 group-has-disabled/otp:bg-muted group-has-aria-invalid/otp:border-dashed group-has-aria-invalid/otp:border-destructive data-[active=true]:z-10 data-[active=true]:outline-(length:--pixel) data-[active=true]:outline-foreground data-[active=true]:outline-dashed",
        className
      )}
      {...props}
    >
      {char}
      {hasFakeCaret && (
        <div className="pointer-events-none absolute inset-0 flex items-center justify-center">
          <div className="h-4 w-(--pixel) animate-blink bg-foreground motion-reduce:animate-none" />
        </div>
      )}
    </div>
  )
}

function InputOTPSeparator({ ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="input-otp-separator"
      className="flex items-center px-1"
      role="separator"
      {...props}
    >
      <MinusGlyph />
    </div>
  )
}

export { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot }
```

## API reference

### InputOTP

| Prop | Type | Default |
| --- | --- | --- |
| `containerClassName` | `string` | - |

### InputOTPGroup

No props of its own. It forwards the props of the element or primitive it wraps.

### InputOTPSeparator

No props of its own. It forwards the props of the element or primitive it wraps.

### InputOTPSlot

| Prop | Type | Default |
| --- | --- | --- |
| `index` * | `number` | - |
