Skip to main content

Input OTP

Composable one-time-password input primitive with direction-aware keyboard navigation and paste support.

import * as InputOTP from '@gentleduck/primitives/input-otp'
import * as InputOTP from '@gentleduck/primitives/input-otp'

Anatomy

<InputOTP.Root>
  <InputOTP.Group>
    <InputOTP.Slot />
  </InputOTP.Group>
  <InputOTP.Separator />
</InputOTP.Root>
<InputOTP.Root>
  <InputOTP.Group>
    <InputOTP.Slot />
  </InputOTP.Group>
  <InputOTP.Separator />
</InputOTP.Root>

Example

<InputOTP.Root maxLength={6} dir="rtl">
  <InputOTP.Group>
    <InputOTP.Slot />
    <InputOTP.Slot />
    <InputOTP.Slot />
  </InputOTP.Group>
  <InputOTP.Separator>-</InputOTP.Separator>
  <InputOTP.Group>
    <InputOTP.Slot />
    <InputOTP.Slot />
    <InputOTP.Slot />
  </InputOTP.Group>
</InputOTP.Root>
<InputOTP.Root maxLength={6} dir="rtl">
  <InputOTP.Group>
    <InputOTP.Slot />
    <InputOTP.Slot />
    <InputOTP.Slot />
  </InputOTP.Group>
  <InputOTP.Separator>-</InputOTP.Separator>
  <InputOTP.Group>
    <InputOTP.Slot />
    <InputOTP.Slot />
    <InputOTP.Slot />
  </InputOTP.Group>
</InputOTP.Root>

API

InputOTP.Root

PropTypeDefaultDescription
valuestring-Controlled OTP value
onValueChange(value: string) => void-Called when slot values change
patternRegExp/^.$/Validates each entered character
maxLengthnumber-Optional cap for active slots used by keyboard/paste behavior
dir'ltr' | 'rtl'inheritedLocal direction override
namestring-Name for form submission
...propsReact.ComponentPropsWithoutRef<'div'>-Additional root props

InputOTP.Group

PropTypeDefaultDescription
...propsReact.ComponentPropsWithoutRef<'div'>-Additional group props

InputOTP.Slot

PropTypeDefaultDescription
maxLengthnumber1Maximum characters for slot input
...propsReact.ComponentPropsWithoutRef<'input'>-Additional input props

InputOTP.Separator

PropTypeDefaultDescription
customIndicatorReact.ReactNode-Custom separator content
childrenReact.ReactNode-Fallback separator content
...propsReact.ComponentPropsWithoutRef<'div'>-Additional separator props

Exports

  • InputOTP.Root, InputOTP.Group, InputOTP.Slot, InputOTP.Separator
  • REGEXP_ONLY_DIGITS
  • REGEXP_ONLY_DIGITS_AND_CHARS