vael-ui v0.5.7

Forms & inputs

Layout

OtpInput

A row of single-character boxes for a one-time code, with paste and auto-advance. Use it for sign-in and verification codes.

Validating a form? vael-form wires this up with a schema

Install

import { OtpInput } from 'vael-ui'

Playground

Placement, behaviour and animation

Usage
<script setup lang="ts">
import { ref } from 'vue'
import { OtpInput } from 'vael-ui'

const value = ref('')
</script>

<template>
  <OtpInput aria-label="Verification code" v-model="value" />
</template>

Props

NameTypeDefaultDescription
lengthnumber6Number of cells, and the maximum code length.
type'numeric' | 'alphanumeric''numeric'Restricts input to digits or to alphanumeric characters.
maskbooleanfalseShows a bullet instead of the entered character in every filled cell, and keeps screen readers from reading the code.
disabledbooleanfalseDisables the input and blocks interaction. A disabled parent Field also disables it.
invalidbooleanfalseStandalone override; ORed with the nearest Field's error state.
size'sm' | 'md' | 'lg''md'Control size.
variant'cells' | 'field''cells''field' draws one box around the whole code, like a phone's verification field, instead of a box per cell.
placeholderstringOne character shown in each empty cell, such as 0 or •.
namestringNative name on the input, for plain <form> submission.
groupsnumber | number[]Splits the cells into groups with a separator between them: a group size (3 for 123-456), or each group's size ([3, 4]).
motionOtpInputMotionAnimates its states, like a shake when the code is wrong: { states: { invalid: { root: { x: [0, -6, 6, -3, 0] } } } }.
uiPartial<Record<'root' | 'input' | 'cell' | 'separator', UiPartValue>>Class and style overrides for each part.
modelValuestring''Entered code.

Events

NameTypeDescription
update:modelValue(value: string)Fires when modelValue changes (v-model).
complete(code: string)Fires with the code each time it changes to a full length characters, not on every re-render.

Slots

NameTypeDescription
cell{ char: string | null; index: number; active: boolean; filled: boolean; }Custom content for one cell.
separator{ index: number; }Replaces the dash between groups. index counts separators from 0.

Exposed

NameTypeDescription
elHTMLElement | nullRoot element.
focus() => voidFocuses the field. Called while it's disabled, it focuses as soon as it's enabled again.
inputElHTMLInputElement | nullInvisible native <input> that receives typing and paste.
cellElsHTMLElement[] | nullCell elements, one per character slot.
ringElHTMLElement | nullThe ring and caret that glide to the active cell. null with a custom #cell slot.

Motion New

OtpInput's motion: its parts (OtpInputPart) and states (invalid, complete, disabled, focus). Each cell plays them on its own, plus filled and current (the caret's); give a cell's values as a function to use its index. false turns it off.

<OtpInput
  :motion="{ states: { invalid: { root: { scale: 1.04 } } } }"
/>
NameTypeDescription
initialexitrootcellSets where each part comes in from and leaves to: any CSS property, plus x, y and blur. A plain number is px, or degrees for rotate.
statesinvalidcompletedisabledfocusfilledcurrentSets values per part that hold while a state is on. An array is a set of keyframes that plays once as the state turns on, such as a shake on invalid.
enterleave(parts, context) => animationAnimates the parts with GSAP, motion or element.animate(). Return the animation and the component waits for it, and stops it if interrupted.
springdurationeasing{ damping, response } | number | stringTimes all of it: a spring, or a duration in ms with an easing. Never both.
falsebooleanTurns its motion off, for an animation of your own on its exposed elements.

Accessibility

KeyAction
A digitFills the current cell and moves to the next one.
BackspaceDeletes the character before the caret.
ArrowLeftArrowRightMoves the caret, and the highlighted cell, between cells.
  • One native text input sits over the cells and takes all typing and paste; the cells are aria-hidden.
  • Inside a Field, the label names the input. Outside one, give it an aria-label or aria-labelledby; both go to the input, not the wrapper.
  • It sets autocomplete="one-time-code", so browsers can fill a code from a text message. With type="alphanumeric" letters work too, and other characters are dropped.
  • With mask, the cells show bullets and the input becomes a password field, so screen readers don’t read the code aloud.