Skip to main content
196

Search Lumen

Find components, APIs, guides, and recipes.

GitHub
Web docs
Forms and dates

useInputOTP

Builds an Astro-compatible OTP field with a native input, visual segments, sanitized values, and caret navigation.

Live component preview · Astro

Explore InputOTP

Try the component this hook connects to. The preview uses the Astro adapter; the React example below uses useInputOTP.

Paste the full code, or move through digits with Arrow Left / Arrow Right.

React usage

Import from @santi020k/lumen-react and spread the controller props onto the matching components.

tsxuseInputOTP example
import { useInputOTP } from '@santi020k/lumen-react'

export function VerificationCode() {
  const otp = useInputOTP({ length: 6, defaultValue: '123456' })

  return (
    <div {...otp.rootProps}>
      <input aria-label="Verification code" name="code" {...otp.getInputProps()} />
      <div {...otp.segmentsProps}>
        {otp.segmentIndexes.map(index => (
          <button key={index} {...otp.getSegmentProps(index)}>
            <span data-ui-input-otp-char>{otp.getSegmentChar(index)}</span>
          </button>
        ))}
      </div>
    </div>
  )
}
import { useInputOTP } from '@santi020k/lumen-react'

export function VerificationCode() {
  const otp = useInputOTP({ length: 6, defaultValue: '123456' })

  return (
    <div {...otp.rootProps}>
      <input aria-label="Verification code" name="code" {...otp.getInputProps()} />
      <div {...otp.segmentsProps}>
        {otp.segmentIndexes.map(index => (
          <button key={index} {...otp.getSegmentProps(index)}>
            <span data-ui-input-otp-char>{otp.getSegmentChar(index)}</span>
          </button>
        ))}
      </div>
    </div>
  )
}

Load @santi020k/lumen-react/styles.css once in your application. With React Server Components, put 'use client' at the top of the module that calls the hook. Follow the React setup guide for the framework boundary.

Options

PropertyTypeDefaultDescription
lengthnumber6Number of visible OTP segments.
valuestring-Controlled OTP value.
defaultValuestring""Initial value for uncontrolled usage.
onValueChange(value: string) => void-Callback fired when input or paste changes the sanitized value.
inputModestring"numeric"Native inputMode forwarded to the hidden input. Numeric mode removes non-digits.
patternstring"[0-9]*"Native pattern forwarded to the hidden input.

Controller return values

PropertyTypeDescription
rootPropsLumenProps<"div">Spread onto the OTP field root.
getInputProps(props?) => LumenProps<"input">Returns props for the native input.
segmentsPropsLumenProps<"div">Spread onto the visual segment container.
getSegmentProps(index, props?) => LumenProps<"button">Returns props for a visual segment button.
getSegmentChar(index) => stringReturns the character or blank placeholder for a segment.
setValue(value) => stringSanitize and set the current OTP value.
Web docsFull catalog