Skip to content

OTP โ€‹

A one-time-code field: a row of single-character boxes that behaves as one input. Typing fills a box and moves to the next one. Backspace clears a box and steps back. A paste, or a phone's SMS autofill, fills every box from the focused one on. It has the same props as <mono-input> (size, color, variant, label, label position, validation position, tooltip, messages and states) and binds to controlMonoForm like any form control. Toggle Vue / CSS to switch the live demo and source together.

Each box uses the input's look, and every --mono-otp-* knob falls back to the matching --mono-input-* knob, so the field follows every flavor with no entries of its own.

ts
import '@mono-lit/helper/ui/otp'          // <mono-otp>
import '@mono-lit/helper/ui/shadow/otp'   // <mono-shadow-otp> (SSR / shadow DOM)

Basic โ€‹

The value is always a string with no gaps: box i holds value[i]. Focus never lands past the first empty box, and deleting a character moves the ones after it left. The first box carries autocomplete="one-time-code", so phones offer the code from an incoming SMS.

Vue SFC

Types โ€‹

type sets which characters a box accepts. A rejected character is dropped and fires no event.

typeAcceptsKeyboard
numeric (default)0-9numeric keypad (inputmode="numeric")
alphanumericA-Z, 0-9; letters are uppercasedtext
textany character except a spacetext
Vue SFC

Mask โ€‹

mask draws each box as a password input, so the characters show as dots. The value is still the real code.

Vue SFC

Groups โ€‹

  • length sets the number of boxes (default 6).
  • groups splits the boxes into groups:
    • one size repeats: 3 gives 3-3;
    • a list is taken as given: [2, 2, 2], or "2,2,2" from markup.
  • separator is the text drawn between groups (default -). An empty string leaves only the gap.
  • placeholder sets one character for every empty box. A string exactly length long sets one character per box.
Vue SFC

Sizes โ€‹

Each box is a square at the shared control height of its size, so a code field lines up with the inputs and buttons beside it.

Vue SFC

Colors โ€‹

color is the focus colour (the focused box's border and ring), as on mono-input.

Vue SFC

Variants โ€‹

Vue SFC

States โ€‹

  • disabled disables every box and fades the row.
  • readonly keeps the boxes focusable but blocks every edit.
  • required shows the asterisk and sets aria-required on the group.
  • validation-state / validation-message (or error-message, success-message) recolour every box and show the message.
  • clearable adds a ร— after the boxes once a code has been typed.
Vue SFC

Label position โ€‹

label-position="left | right" puts the label beside the row, on the same grid as the other field controls. Knobs: --mono-otp-label-width and --mono-otp-label-side-gap. notched renders as top, because there is no single box to cut the label into.

Vue SFC

Validation position โ€‹

Works as on mono-input. icon puts a state icon after the boxes, and hovering or focusing it shows the message. float shows the message under the boxes while the field is hovered or focused. In both modes the row under the boxes keeps the helper text.

Vue SFC

Tooltip โ€‹

Vue SFC

Form โ€‹

Bind with :control-form="form" and key-form, like any form control. The form owns the value, which is a string, and renders the validation.

  • input runs the live rules.
  • change runs the change rules and marks the field touched. It fires when focus leaves the boxes, not on every box.
  • The built-in min / max rules measure the string length, so { type: 'min', value: 6 } means "all six digits".

With a name, the element also writes a hidden <input> holding the joined code, so a native <form> submits it. This only works in the light build.

Vue SFC

Events โ€‹

All four events carry { modelValue, value, oldValue, complete, index }. index is the box that was edited, or -1 for a clear or a blur commit. The mno-* aliases fire too.

EventFires
inputOn every edit (a typed character, a Backspace or Delete, a paste)
completeEach time an edit fills the last empty box
changeWhen focus leaves the boxes and the code differs from when focus entered
clearFrom the ร— button or el.clear() (with input and change)
Vue SFC

Keyboard โ€‹

KeyAction
A characterFill this box, move to the next
BackspaceClear this box; on an empty box, clear the one before and move there
DeleteRemove this box's character; the boxes after it move left
Arrow Left / Right (Up / Down)Previous / next box (never past the first empty one)
Home / EndFirst box / first empty box
Ctrl/โŒ˜ + VFill from this box on; rejected characters such as spaces or dashes are dropped

The row is a role="group" named by the label. Each box reads "label โ€“ Digit i of n". Methods: focus(index?) (by default the first empty box), blur() and clear(). The getter complete is true once every box is filled.

CSS Variables โ€‹

Vue SFC

Each knob first reads --mono-otp-*, then the matching --mono-input-*, then vega's value.

VariableDefaultControls
--mono-otp-cell-size-<size> / --mono-otp-cell-width--mono-control-height-<size> / = sizeBox height and width
--mono-otp-font-<size> / --mono-otp-font-weightxs โ†’ --mono-text-xs, md โ†’ --mono-text-base, xl โ†’ --mono-text-xl / semiboldCharacter type
--mono-otp-gap / --mono-otp-group-gap2 ร— spacingBetween boxes / between groups
--mono-otp-radius--mono-input-radius โ†’ --mono-radius-mdBox corners
--mono-otp-bg / --mono-otp-filled-bg / --mono-otp-border-color / --mono-otp-shadowthe input'sBox surface
--mono-otp-ring-color / -ring-width / -ring-alphathe color / --mono-ring-width / --mono-ring-alphaFocus ring
--mono-otp-separator-color / --mono-otp-placeholdermuted foregroundSeparator and placeholder text
--mono-otp-readonly-bg / --mono-otp-disabled-bg--muted / the box surfaceState surfaces
--mono-otp-label-* / --mono-otp-message-*the input'sLabel and message type
--mono-otp-<color> (primary โ€ฆ dark, valid, invalid)the input's palettePalette slots

Types โ€‹

Importimport { OtpProps } from '@mono-lit/helper'
PropValueDefaultDescription
type'text' | 'numeric' | 'alphanumeric'โ€”Which characters a box accepts: `numeric` (default), `alphanumeric` (uppercased) or `text`.
lengthstring | numberโ€”Number of boxes (default 6).
groupsstring | number | number[]โ€”Split the boxes into groups with a separator between them: a group size (`3` โ†’ 3-3), a list (`[3, 3]`, `[2, 2, 2]`) or the markup form `"3,3"`.
separatorstringโ€”Text drawn between groups (default `-`). An empty string leaves only the gap.
maskbooleanโ€”Hide the typed characters like a password.
size'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl'โ€”Visual size of the boxes.
color'primary' | 'secondary' | 'success' | 'danger' | 'warning' | 'info' | 'teal' | 'purple' | 'neutral' | 'dark'โ€”Theme color applied to focus.
variant'underlined' | 'outlined' | 'filled'โ€”Visual style of each box (outlined, filled, or underlined).
labelPosition'left' | 'right' | 'top' | 'notched'โ€”Where the label sits: `top` (default), `left` or `right`. `notched` renders as `top`.
validationPosition'bottom' | 'icon' | 'float'โ€”Where the validation message shows: `bottom` (default), `icon` or `float` (see mono-input).
placeholderstringโ€”A placeholder character drawn in every empty box (e.g. `โ—‹` or `-`).
valuestring | numberโ€”Current code.
namestringโ€”Form field name โ€” a hidden input carries the joined code for a native form submit.
modelValuestring | numberโ€”Two-way bound value.
disabledbooleanโ€”Disables interaction and dims the boxes.
readonlybooleanโ€”Makes the boxes read-only while still focusable.
clearablebooleanโ€”Shows a clear button when a code has been typed.
requiredbooleanโ€”Marks the field as required and shows a required indicator.
autofocusbooleanโ€”Focuses the first empty box on first render.
labelstringโ€”Label text displayed above the boxes.
helperTextstringโ€”Helper text shown below the boxes.
tooltipstring | MonoTooltipOptionsโ€”Info icon at the end of the label (plain text, or full `mono-tooltip` options).
validationState'warning' | 'default' | 'invalid' | 'valid'โ€”Explicit validation state for styling and messaging.
validationMessagestringโ€”Validation message shown below the boxes.
errorbooleanโ€”Marks the field in an error state.
errorMessagestringโ€”Error message shown below the boxes.
successbooleanโ€”Marks the field in a success state.
successMessagestringโ€”Success message shown below the boxes.
autocompletestringโ€”Native autocomplete hint on the first box (default `one-time-code`, which lets phones offer an SMS code).
inputmodestringโ€”On-screen keyboard hint. Defaults to `numeric` for `type="numeric"`, otherwise `text`.
ariaLabelTextstringโ€”Accessible label for the group; each box reads "<label> โ€“ character i of n".
ariaLabelstringโ€”โ€”
widthstring | numberโ€”Explicit sizing of the root (CSS length or px number).
heightstring | numberโ€”โ€”
minWidthstring | numberโ€”โ€”
maxWidthstring | numberโ€”โ€”
minHeightstring | numberโ€”โ€”
maxHeightstring | numberโ€”โ€”
cssClassOtpCssClassโ€”Per-part class overrides for internal elements.
visiblebooleanโ€”Whether the control is shown. Defaults to `true`.
visibleType'none' | 'invisible'โ€”How it hides when `visible` is `false`. Defaults to `'none'`.