OtpInput

OtpInput

HkOtpInput is the one-cell-per-character code field: TOTP, SMS and e-mail verification codes, invite codes and recovery codes. The cells are a projection of a single modelValue string, so the field stays controlled: typing a run spreads it forward across cells, a paste pours the whole clipboard through them, Backspace steps back a cell, and complete fires once on the short-to-full edge. Reach for it whenever a code is short, contiguous and typed from another device.

  • For a numeric quantity inside a form, use HkNumberInput.
  • For an arbitrary string the user composes, use HkInput.
  • For the phone number that receives the code, use HkPhoneInput.

Import

tsx
1
import { HkOtpInput } from "@celestia-island/hikari";
rust
1
2
use hikari_components::*;          // OtpInput, OtpSize
use hikari_components::prelude::*; // rsx!, VNode

The Rust form is a plain function, OtpInput(OtpInputProps { … }). length is clamped to 1..=12 in the mirror, exactly like Vue's cellCount.

Length and grouping

length sets the cell count — 6 is the universal TOTP and SMS width, 4 a PIN. separated splits an even row into two groups (the 123 456 shape) and is ignored when the count is odd. size — sm / md / lg — is class-only: the stylesheet owns the cell box, the gap and the font ceiling for each ramp.

Alphabet

Digits only by default; alphanumeric widens the accepted set to [0-9A-Za-z] for invite and recovery codes. The alphabet is enforced twice — filtering on the way in and a numeric keypad on touch devices via inputmode — so a letters-only paste into a digits-only row is dropped instead of being painted into a cell.

Six digits.

Letters and digits.

States and messages

error paints the row and the cells in the error channel and prints the message line; hint is the quiet line shown while there is no error. disabled and readonly reach every cell, and the disabled cells also publish aria-disabled. The short-to-full edge is where a host submits or verifies — complete fires there once, and autoSubmit turns it into a submit signal.

Six digits, sent by SMS.

Waiting for the code

API

Vue

Props

NameTypeDefaultRequired
modelValueString""—
lengthNumber6—
errorStringundefined—
hintStringundefined—
labelStringundefined—
size<code>String as () => "sm" | "md" | "lg"</code>"md"—
disabledBooleanfalse—
readonlyBooleanfalse—
alphanumericBooleanfalse—
separatedBooleanfalse—
autofocusBooleanfalse—
idStringundefined—
submitOnEnterFunction as PropType<() => void>undefined—
autoSubmitBooleanfalse—
ariaLabelStringundefined—
cellAriaLabelFunction as PropType<(index: number) => string>undefined—

Emits

EventPayload
update:modelValuestring
completestring
focusFocusEvent
blurFocusEvent
keydownKeyboardEvent

Rust

Props — OtpInput

PropTypeDefaultDescription
model_valueString""The composed code, at most length characters (Vue: modelValue).
lengthi326Cell count; clamped to 1..=12 like Vue's cellCount.
errorOption<String>NoneError ink + message line; also marks every cell aria-invalid.
hintOption<String>NoneQuiet message under the row, shown only while error is unset.
labelOption<String>NoneVisible caption above the row.
sizeOtpSizeOtpSize::Md—
disabledboolfalse—
readonlyboolfalse—
alphanumericboolfalseAccept [0-9A-Za-z] instead of digits only (invite / recovery codes).
separatedboolfalseSplit an even row into two groups (the "123 456" SMS shape).
idOption<String>NoneFirst cell's id (Vue: id); cell *n* gets {id}-{n}.
submit_on_enterOption<EventHandler<()>>NoneSubmit intent: Enter in any cell, and auto_submit on the fill edge.
auto_submitboolfalseFire submit_on_enter the moment the row fills (Vue: autoSubmit).
aria_labelOption<String>NoneAccessible name for the row (Vue: ariaLabel, falls back to label).
cell_aria_labelOption<Callback<usize, String>>NonePer-cell accessible name (Vue: cellAriaLabel, called with the 1-based position).
on_cursor_changeOption<EventHandler<usize>>NoneThe caret index Vue would have moved to (see the header delta) — auto-advance after a keystroke/paste, the step-back target after Backspace, the landing cell after ←/→/Home/End.
on_inputOption<EventHandler<String>>NoneVue: update:modelValue, emitted with the sanitized code.
on_completeOption<EventHandler<String>>NoneVue: complete, fired on the short → full edge only.
on_focusOption<EventHandler<FocusEvent>>NoneVue: the focus emit (per cell).
on_blurOption<EventHandler<FocusEvent>>NoneVue: the blur emit (per cell).
on_keydownOption<EventHandler<KeyboardEvent>>NoneVue: the keydown emit, forwarded after the field's own handling.
classString""Host classes, appended to the row (Vue merges $attrs.class there).
styleString""Host style, applied to the row (Vue merges $attrs.style there).

Accessibility

  • The row is a role="group" named by aria-label, falling back to label; its optional description points at the hint line's id ({id}-hint). The visible caption is a <label> whose for targets the first cell's id, and each cell after the first gets {id}-{index}.
  • Every cell carries its own name, Digit {n} by default or whatever cellAriaLabel returns for the 1-based position — so a screen reader hears "Digit 2 of 6" rather than eight anonymous inputs. Pass ariaLabel when the row needs a name the caption cannot give.
  • error sets aria-invalid on every cell and renders the message in a role="alert" paragraph, so a verification failure is announced without a manual refocus.
  • Keyboard: typed characters advance the caret, Backspace clears the current cell or steps back into the previous one, Delete clears only the current cell, and the arrow keys, Home and End walk the row (Home/End jump to the ends). Each cell is its own tab stop.
  • In the browser the row opts in to one-time-code autofill — the first cell carries autocomplete="one-time-code" — and turns off autocapitalize, autocorrect and spellcheck so a code is never "corrected".
  • The Rust mirror cannot move focus, so it reports the index Vue would have focused through on_cursor_change; the host performs the movement. Enter submits where Vue's submitOnEnter would, and autoSubmit fires it on the short-to-full edge.

Related components