Skip to content
Klarhimmel UI

Input OTP

npx shadcn add @klarhimmel/input-otp

Examples

  • Basic

  • States

  • Form

DetailsAbout, usage, props and more

A code split into boxes that behave as one field: paste fills them all, Backspace goes back, autofill works.

When to use

  • Use for

    A short code that was sent to the person: six digits by email or text, a booking reference.

  • Not for

    A long secret or a password. Use Input.

  • Pairs with

    Button, a resend link, a status line.

Preview

Enter the six-digit code we emailed you

0 of 6 digits.

input-otp.tsx
import { Theme } from '@/components/klarhimmel/theme/style';
import { InputOtp } from '@/components/klarhimmel/ui/input-otp';

export default function Example() {
  return (
    <Theme theme="brand">
      <InputOtp aria-label="Verification code" onComplete={(code) => console.log(code)} />
    </Theme>
  );
}

Import

Import

import { InputOtp } from '@/components/klarhimmel/ui/input-otp';
Files
input-otp.tsx, in components/klarhimmel/ui/. With it: theme, utils.
Render in
Render it inside <Theme>, once near the root of your app. Tokens and fonts are scoped to it, so outside it the component is unstyled.

Props

PropTypeDefaultDescription
lengthnumber6How many characters. Default 6.
valuestring-Controlled or uncontrolled. The value is one string without gaps.
defaultValuestring''Controlled or uncontrolled. The value is one string without gaps.
onValueChange(value: string) => void-Controlled or uncontrolled. The value is one string without gaps.
onComplete(value: string) => void-Called once every box is filled.
type'numeric' | 'alphanumeric''numeric'Digits only, or letters and digits.
namestring-Submits the whole code with a form (a hidden input).
disabledboolean-Error styling on every box; switches them off.
readOnlyboolean-Error styling on every box; switches them off.
invalidboolean-Error styling on every box; switches them off.
aria-labelstring-Names the group. Required unless aria-labelledby points at a label.
aria-labelledbystring-Required: names the group.
aria-describedbystring-
boxLabel(n: number, of: number) => string-The name of box `n` of `of`: default "Digit 1 of 6" (or "Character" for alphanumeric). Pass it translated.
classNamestring-Extra classes, merged after the component's own.

Accessibility

KeyDoes
TypeFills the box and moves to the next; letters are ignored in numeric mode
PasteFills from the box you pasted into, ignoring spaces
BackspaceClears the box; in an empty box removes the one before and moves back
DeleteRemoves the character in the box
Left / Right / Home / EndMove between boxes

The first box carries autocomplete one-time-code, so phones offer the code from a message. Each box is named "Digit n of N".

Accessibility