Appearance
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.
type | Accepts | Keyboard |
|---|---|---|
numeric (default) | 0-9 | numeric keypad (inputmode="numeric") |
alphanumeric | A-Z, 0-9; letters are uppercased | text |
text | any character except a space | text |
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 โ
lengthsets the number of boxes (default 6).groupssplits the boxes into groups:- one size repeats:
3gives 3-3; - a list is taken as given:
[2, 2, 2], or"2,2,2"from markup.
- one size repeats:
separatoris the text drawn between groups (default-). An empty string leaves only the gap.placeholdersets one character for every empty box. A string exactlylengthlong 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 โ
disableddisables every box and fades the row.readonlykeeps the boxes focusable but blocks every edit.requiredshows the asterisk and setsaria-requiredon the group.validation-state/validation-message(orerror-message,success-message) recolour every box and show the message.clearableadds 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.
inputruns theliverules.changeruns thechangerules and marks the field touched. It fires when focus leaves the boxes, not on every box.- The built-in
min/maxrules 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.
| Event | Fires |
|---|---|
input | On every edit (a typed character, a Backspace or Delete, a paste) |
complete | Each time an edit fills the last empty box |
change | When focus leaves the boxes and the code differs from when focus entered |
clear | From the ร button or el.clear() (with input and change) |
Vue SFC
Keyboard โ
| Key | Action |
|---|---|
| A character | Fill this box, move to the next |
| Backspace | Clear this box; on an empty box, clear the one before and move there |
| Delete | Remove 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 / End | First box / first empty box |
| Ctrl/โ + V | Fill 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.
| Variable | Default | Controls |
|---|---|---|
--mono-otp-cell-size-<size> / --mono-otp-cell-width | --mono-control-height-<size> / = size | Box height and width |
--mono-otp-font-<size> / --mono-otp-font-weight | xs โ --mono-text-xs, md โ --mono-text-base, xl โ --mono-text-xl / semibold | Character type |
--mono-otp-gap / --mono-otp-group-gap | 2 ร spacing | Between boxes / between groups |
--mono-otp-radius | --mono-input-radius โ --mono-radius-md | Box corners |
--mono-otp-bg / --mono-otp-filled-bg / --mono-otp-border-color / --mono-otp-shadow | the input's | Box surface |
--mono-otp-ring-color / -ring-width / -ring-alpha | the color / --mono-ring-width / --mono-ring-alpha | Focus ring |
--mono-otp-separator-color / --mono-otp-placeholder | muted foreground | Separator and placeholder text |
--mono-otp-readonly-bg / --mono-otp-disabled-bg | --muted / the box surface | State surfaces |
--mono-otp-label-* / --mono-otp-message-* | the input's | Label and message type |
--mono-otp-<color> (primary โฆ dark, valid, invalid) | the input's palette | Palette slots |
Types โ
Import
import { OtpProps } from '@mono-lit/helper'| Prop | Value | Default | Description |
|---|---|---|---|
type | 'text' | 'numeric' | 'alphanumeric' | โ | Which characters a box accepts: `numeric` (default), `alphanumeric` (uppercased) or `text`. |
length | string | number | โ | Number of boxes (default 6). |
groups | string | 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"`. |
separator | string | โ | Text drawn between groups (default `-`). An empty string leaves only the gap. |
mask | boolean | โ | 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). |
placeholder | string | โ | A placeholder character drawn in every empty box (e.g. `โ` or `-`). |
value | string | number | โ | Current code. |
name | string | โ | Form field name โ a hidden input carries the joined code for a native form submit. |
modelValue | string | number | โ | Two-way bound value. |
disabled | boolean | โ | Disables interaction and dims the boxes. |
readonly | boolean | โ | Makes the boxes read-only while still focusable. |
clearable | boolean | โ | Shows a clear button when a code has been typed. |
required | boolean | โ | Marks the field as required and shows a required indicator. |
autofocus | boolean | โ | Focuses the first empty box on first render. |
label | string | โ | Label text displayed above the boxes. |
helperText | string | โ | Helper text shown below the boxes. |
tooltip | string | 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. |
validationMessage | string | โ | Validation message shown below the boxes. |
error | boolean | โ | Marks the field in an error state. |
errorMessage | string | โ | Error message shown below the boxes. |
success | boolean | โ | Marks the field in a success state. |
successMessage | string | โ | Success message shown below the boxes. |
autocomplete | string | โ | Native autocomplete hint on the first box (default `one-time-code`, which lets phones offer an SMS code). |
inputmode | string | โ | On-screen keyboard hint. Defaults to `numeric` for `type="numeric"`, otherwise `text`. |
ariaLabelText | string | โ | Accessible label for the group; each box reads "<label> โ character i of n". |
ariaLabel | string | โ | โ |
width | string | number | โ | Explicit sizing of the root (CSS length or px number). |
height | string | number | โ | โ |
minWidth | string | number | โ | โ |
maxWidth | string | number | โ | โ |
minHeight | string | number | โ | โ |
maxHeight | string | number | โ | โ |
cssClass | OtpCssClass | โ | Per-part class overrides for internal elements. |
visible | boolean | โ | Whether the control is shown. Defaults to `true`. |
visibleType | 'none' | 'invisible' | โ | How it hides when `visible` is `false`. Defaults to `'none'`. |