OTP Input
A one-time code, one input per character, in the look of the app's style.
Usage
import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form } from '@angular/forms/signals';import { ScField, ScLabel, ScOtpInput, ScOtpInputGroup, ScOtpInputSlot, ScSeparator,} from '@semantic-components/ui';import { SiMinusIcon } from '@semantic-icons/lucide-icons';
/** Six characters, one input each: typed in order, pasted or autofilled from an SMS. */@Component({ selector: 'app-otp-input-usage-demo', imports: [ SiMinusIcon, ScField, FormField, ScLabel, ScOtpInput, ScOtpInputGroup, ScOtpInputSlot, ScSeparator, ], template: ` <div scField class="w-fit"> <label scLabel>Verification code</label> <div scOtpInput slotLabel="Character {index} of {length}" [formField]="otpForm.code" > <div scOtpInputGroup> <input scOtpInputSlot /> <input scOtpInputSlot /> <input scOtpInputSlot /> </div> <div scSeparator><svg siMinusIcon></svg></div> <div scOtpInputGroup> <input scOtpInputSlot /> <input scOtpInputSlot /> <input scOtpInputSlot /> </div> </div> </div> `, host: { class: 'flex w-full justify-center' }, encapsulation: ViewEncapsulation.None,})export class OtpInputUsageDemo { private readonly otpForm = form(signal({ code: '123456' }));}Anatomy
Base UI's OTP Field: one real input per character, in chunks (groups, each a joined box) with separators between them, in a row that holds the code — a [formField] or [(value)] — as long as its inputs. Its label, description and errors go round it, in a field.
import { ScOtpInput, ScOtpInputGroup, ScOtpInputSlot, ScSeparator } from '@semantic-components/ui';<div scField> <label scLabel>…</label> <div scOtpInput [formField]="…" slotLabel="Character {index} of {length}"> <div scOtpInputGroup> <input scOtpInputSlot /> <input scOtpInputSlot /> </div> <div scSeparator><svg siMinusIcon></svg></div> <!-- the library's Separator, your icon in it --> <div scOtpInputGroup>…</div> </div> <p scFieldErrors></p></div>Accessibility
- Real inputs: typing moves on, Backspace back; ← and → move (mirrored right to left), Home and End to the ends; focus goes to the first empty input, so the code is entered in order.
- The inputs are a group named by the field's label; each input is named by its place, in your words, written once on the root (slotLabel="Character {index} of {length}") — an input's own aria-label wins. Clicking the label goes to the first. A description is read with them.
- The first input offers the code from an SMS (autocomplete="one-time-code"); a pasted code fills them all.
- Invalid once left (aria-invalid on its inputs).
Examples
Digits only
import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form } from '@angular/forms/signals';import { ScField, ScLabel, ScOtpInput, ScOtpInputGroup, ScOtpInputSlot,} from '@semantic-components/ui';Alphanumeric
import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form } from '@angular/forms/signals';import { ScField, ScLabel, ScOtpInput, ScOtpInputGroup, ScOtpInputSlot, ScSeparator,} from '@semantic-components/ui';Four digits
import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form } from '@angular/forms/signals';import { ScField, ScLabel, ScOtpInput, ScOtpInputGroup, ScOtpInputSlot,} from '@semantic-components/ui';Separator
import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form } from '@angular/forms/signals';import { ScField, ScLabel, ScOtpInput, ScOtpInputGroup, ScOtpInputSlot, ScSeparator,} from '@semantic-components/ui';Invalid
Enter all 6 digits.
import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form, minLength, required } from '@angular/forms/signals';import { ScField, ScFieldErrors, ScLabel, ScOtpInput, ScOtpInputGroup, ScOtpInputSlot, ScSeparator,Disabled
import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, disabled, form } from '@angular/forms/signals';import { ScField, ScLabel, ScOtpInput, ScOtpInputGroup, ScOtpInputSlot, ScSeparator,} from '@semantic-components/ui';Controlled
Enter your one-time password.
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScField, ScLabel, ScOtpInput, ScOtpInputGroup, ScOtpInputSlot,} from '@semantic-components/ui';
/** Two-way, [(value)], without a form: the code shown as it is typed. */Form
import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form, minLength, required } from '@angular/forms/signals';import { RouterLink } from '@angular/router';import { ScButton, ScCard, ScCardBody, ScCardDescription, ScCardFooter, ScCardHeader,Placeholder hints
import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form } from '@angular/forms/signals';import { ScField, ScLabel, ScOtpInput, ScOtpInputGroup, ScOtpInputSlot,} from '@semantic-components/ui';Custom normalization
import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form } from '@angular/forms/signals';import { ScField, ScLabel, ScOtpInput, ScOtpInputGroup, ScOtpInputSlot, ScSeparator,} from '@semantic-components/ui';Masked entry
import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form } from '@angular/forms/signals';import { ScField, ScLabel, ScOtpInput, ScOtpInputGroup, ScOtpInputSlot,} from '@semantic-components/ui';Verification
We sent a code to your email address.
Enter all 6 digits.
import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form, minLength, required } from '@angular/forms/signals';import { ScField, ScFieldDescription, ScFrame, ScLabel, ScOtpInput, ScOtpInputGroup, ScOtpInputSlot,Validation with a server call
Try 123456 (right), 000000 (no answer) or any other code.
import { Component, ViewEncapsulation, computed, resource, signal,} from '@angular/core';import { FormField, form,RTL
import { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation, signal } from '@angular/core';import { FormField, form } from '@angular/forms/signals';import { ScField, ScLabel, ScOtpInput, ScOtpInputGroup, ScOtpInputSlot, ScSeparator,API reference
Each part takes class, merged over the style (cn: a later class wins).
| Part | Selector | Description |
|---|---|---|
ScOtpInput | div[scOtpInput] | The code (a [formField] or [(value)]), as long as its inputs; paste and SMS autofill spread over them. validationType ('numeric', 'alpha', 'alphanumeric', 'none'), normalizeValue, mask, readOnly; disabled and required from the schema. complete(); data-complete, data-filled, data-focused. The row of chunks and separators; its label, description and errors go round it, in a scField (the first input takes its id). A group, named by the field’s label; slotLabel: each input’s name, written once ({index}, {length}). |
ScOtpInputGroup | div[scOtpInputGroup] | One chunk of inputs, joined into one box (the style's: separate boxes in some styles). |
ScOtpInputSlot | input[scOtpInputSlot] | One character, on your input, named from the root's slotLabel ({index}, {length}) — its own aria-label wins. The first takes the field's id (the label goes to it) and autocomplete="one-time-code". data-filled; aria-invalid once the field is left invalid. |