- Accordion
- Alert
- Alert Dialog
- Aspect Ratio
- Attachment
- Autocomplete
- Avatar
- Badge
- Breadcrumb
- Bubble
- Button
- Button Group
- Calendar
- Card
- Carousel
- Chart
- Checkbox
- Code Block
- Collapsible
- Combobox
- Command
- Context Menu
- Data Table
- Date Picker
- Dialog
- Drawer
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input Group
- Input OTP
- Input
- Item
- Kbd
- Label
- Marker
- Menubar
- Message
- Message Scroller
- Native Select
- Navigation Menu
- Pagination
- Popover
- Progress
- Questionnaire
- Radio Group
- Resizable
- Scroll Area
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Sonner (Toast)
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Toggle
- Toggle Group
- Tooltip
- Trace
Input OTP
Accessible one-time password component.
import { Component } from '@angular/core';
import { HlmInputOtpImports } from '@spartan-ng/helm/input-otp';
@Component({
selector: 'spartan-input-otp-preview',
imports: [HlmInputOtpImports],
template: `
<hlm-input-otp length="6">
<hlm-input-otp-group>
<hlm-input-otp-slot index="0" />
<hlm-input-otp-slot index="1" />
<hlm-input-otp-slot index="2" />
</hlm-input-otp-group>
<hlm-input-otp-separator />
<hlm-input-otp-group>
<hlm-input-otp-slot index="3" />
<hlm-input-otp-slot index="4" />
<hlm-input-otp-slot index="5" />
</hlm-input-otp-group>
</hlm-input-otp>
`,
})
export class InputOtpPreview {}Installation
ng g @spartan-ng/cli:ui input-otpnx g @spartan-ng/cli:ui input-otpUsage
import { HlmInputOtpImports } from '@spartan-ng/helm/input-otp';<hlm-input-otp length="6">
<hlm-input-otp-group>
<hlm-input-otp-slot index="0" />
<hlm-input-otp-slot index="1" />
<hlm-input-otp-slot index="2" />
</hlm-input-otp-group>
<hlm-input-otp-separator />
<hlm-input-otp-group>
<hlm-input-otp-slot index="3" />
<hlm-input-otp-slot index="4" />
<hlm-input-otp-slot index="5" />
</hlm-input-otp-group>
</hlm-input-otp>Animation
The fake caret animation animate-caret-blink is provided by tw-animate-css , which is included in @spartan-ng/brain/hlm-tailwind-preset.css . Here are three options for adding the animation to your project.
/* 1. default import includes 'tw-animate-css' */
@import '@spartan-ng/brain/hlm-tailwind-preset.css';
/* 2. import 'tw-animate-css' direclty */
@import 'tw-animate-css';
/* 3. add animate-caret-blink animation from 'tw-animate-css' */
@theme inline {
--animate-caret-blink: caret-blink 1.25s ease-out infinite;
@keyframes caret-blink {
0%,
70%,
100% {
opacity: 1;
}
20%,
50% {
opacity: 0;
}
}
} Adjust the animation to your needs by changing duration, easing function or keyframes by overriding the CSS variable --animate-caret-blink or the @keyframes caret-blink in your global styles.
@import '@spartan-ng/brain/hlm-tailwind-preset.css';
/* adjust animation duration */
@theme inline {
- --animate-caret-blink: caret-blink 1.25s ease-out infinite;
+ --animate-caret-blink: caret-blink 2s ease-out infinite;
}Examples
Pattern
Use the slotPattern prop to restrict the characters that can be entered. The example below only accepts digits.
import { Component } from '@angular/core';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmInputOtpImports } from '@spartan-ng/helm/input-otp';
@Component({
selector: 'spartan-input-otp-pattern',
imports: [HlmFieldImports, HlmInputOtpImports],
template: `
<hlm-field class="mx-auto max-w-xs">
<label hlmFieldLabel for="input-otp-pattern">Digit only</label>
<hlm-input-otp inputId="input-otp-pattern" slotPattern="[0-9]" length="6">
<hlm-input-otp-group>
<hlm-input-otp-slot index="0" />
<hlm-input-otp-slot index="1" />
<hlm-input-otp-slot index="2" />
</hlm-input-otp-group>
<hlm-input-otp-separator />
<hlm-input-otp-group>
<hlm-input-otp-slot index="3" />
<hlm-input-otp-slot index="4" />
<hlm-input-otp-slot index="5" />
</hlm-input-otp-group>
</hlm-input-otp>
</hlm-field>
`,
})
export class InputOtpPattern {}Separator
Use the hlm-input-otp-separator component to add a separator between input groups.
import { Component } from '@angular/core';
import { HlmInputOtpImports } from '@spartan-ng/helm/input-otp';
@Component({
selector: 'spartan-input-otp-separator',
imports: [HlmInputOtpImports],
template: `
<hlm-input-otp length="6">
<hlm-input-otp-group>
<hlm-input-otp-slot index="0" />
<hlm-input-otp-slot index="1" />
</hlm-input-otp-group>
<hlm-input-otp-separator />
<hlm-input-otp-group>
<hlm-input-otp-slot index="2" />
<hlm-input-otp-slot index="3" />
</hlm-input-otp-group>
<hlm-input-otp-separator />
<hlm-input-otp-group>
<hlm-input-otp-slot index="4" />
<hlm-input-otp-slot index="5" />
</hlm-input-otp-group>
</hlm-input-otp>
`,
})
export class InputOtpSeparator {}Disabled
Use the disabled prop to disable the input.
import { Component } from '@angular/core';
import { HlmInputOtpImports } from '@spartan-ng/helm/input-otp';
@Component({
selector: 'spartan-input-otp-disabled',
imports: [HlmInputOtpImports],
template: `
<hlm-input-otp value="123456" disabled length="6">
<hlm-input-otp-group>
<hlm-input-otp-slot index="0" />
<hlm-input-otp-slot index="1" />
<hlm-input-otp-slot index="2" />
</hlm-input-otp-group>
<hlm-input-otp-separator />
<hlm-input-otp-group>
<hlm-input-otp-slot index="3" />
<hlm-input-otp-slot index="4" />
<hlm-input-otp-slot index="5" />
</hlm-input-otp-group>
</hlm-input-otp>
`,
})
export class InputOtpDisabled {}Invalid
Use the forceInvalid prop to mark the input and its slots as invalid.
import { Component } from '@angular/core';
import { HlmInputOtpImports } from '@spartan-ng/helm/input-otp';
@Component({
selector: 'spartan-input-otp-invalid',
imports: [HlmInputOtpImports],
template: `
<hlm-input-otp length="6" forceInvalid>
<hlm-input-otp-group>
<hlm-input-otp-slot index="0" />
<hlm-input-otp-slot index="1" />
<hlm-input-otp-slot index="2" />
</hlm-input-otp-group>
<hlm-input-otp-separator />
<hlm-input-otp-group>
<hlm-input-otp-slot index="3" />
<hlm-input-otp-slot index="4" />
<hlm-input-otp-slot index="5" />
</hlm-input-otp-group>
</hlm-input-otp>
`,
})
export class InputOtpInvalid {}Four Digits
A common pattern for PIN codes. Use slotPattern="[0-9]" and length="4" .
import { Component } from '@angular/core';
import { HlmInputOtpImports } from '@spartan-ng/helm/input-otp';
@Component({
selector: 'spartan-input-otp-four-digits',
imports: [HlmInputOtpImports],
template: `
<hlm-input-otp slotPattern="[0-9]" length="4">
<hlm-input-otp-group>
<hlm-input-otp-slot index="0" />
<hlm-input-otp-slot index="1" />
<hlm-input-otp-slot index="2" />
<hlm-input-otp-slot index="3" />
</hlm-input-otp-group>
</hlm-input-otp>
`,
})
export class InputOtpFourDigits {}Alphanumeric
Use slotPattern="[A-Za-z0-9]" to accept both letters and numbers.
import { Component } from '@angular/core';
import { HlmInputOtpImports } from '@spartan-ng/helm/input-otp';
@Component({
selector: 'spartan-input-otp-alphanumeric',
imports: [HlmInputOtpImports],
template: `
<hlm-input-otp slotPattern="[A-Za-z0-9]" inputMode="text" length="6">
<hlm-input-otp-group>
<hlm-input-otp-slot index="0" />
<hlm-input-otp-slot index="1" />
<hlm-input-otp-slot index="2" />
</hlm-input-otp-group>
<hlm-input-otp-separator />
<hlm-input-otp-group>
<hlm-input-otp-slot index="3" />
<hlm-input-otp-slot index="4" />
<hlm-input-otp-slot index="5" />
</hlm-input-otp-group>
</hlm-input-otp>
`,
})
export class InputOtpAlphanumeric {}Masked
Use the mask prop to visually obscure entered characters. The form value remains unmasked. Customize the mask character with maskChar .
import { Component } from '@angular/core';
import { HlmInputOtpImports } from '@spartan-ng/helm/input-otp';
@Component({
selector: 'spartan-input-otp-masked',
imports: [HlmInputOtpImports],
template: `
<hlm-input-otp mask length="6">
<hlm-input-otp-group>
<hlm-input-otp-slot index="0" />
<hlm-input-otp-slot index="1" />
<hlm-input-otp-slot index="2" />
</hlm-input-otp-group>
<hlm-input-otp-separator />
<hlm-input-otp-group>
<hlm-input-otp-slot index="3" />
<hlm-input-otp-slot index="4" />
<hlm-input-otp-slot index="5" />
</hlm-input-otp-group>
</hlm-input-otp>
`,
})
export class InputOtpMasked {}Form
Sync the OTP to an Angular Signal Form by binding [formField] to hlm-input-otp .
import { afterNextRender, Component, computed, type OnDestroy, signal } from '@angular/core';
import { form, FormField, FormRoot, maxLength, minLength, required, submit } from '@angular/forms/signals';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucideRefreshCw } from '@ng-icons/lucide';
import { toast } from '@spartan-ng/brain/sonner';
import { HlmButtonImports } from '@spartan-ng/helm/button';
import { HlmCardImports } from '@spartan-ng/helm/card';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmInputOtpImports } from '@spartan-ng/helm/input-otp';
import { HlmToasterImports } from '@spartan-ng/helm/sonner';
@Component({
selector: 'spartan-input-otp-form',
imports: [
FormRoot,
FormField,
NgIcon,
HlmButtonImports,
HlmToasterImports,
HlmCardImports,
HlmFieldImports,
HlmInputOtpImports,
],
providers: [provideIcons({ lucideRefreshCw })],
host: {
class: 'preview flex min-h-[350px] w-full justify-center items-center max-w-lg',
},
template: `
<hlm-toaster />
<hlm-card class="mx-auto">
<hlm-card-header>
<div hlmCardTitle>Verify your login</div>
<div hlmCardDescription>
Enter the verification code we sent to your email address:
<span class="font-medium">m@example.com</span>
.
</div>
</hlm-card-header>
<form hlmCardContent id="otp-form" [formRoot]="form">
<hlm-field>
<div class="flex items-center justify-between">
<label hlmFieldLabel>Verification code</label>
<button hlmBtn variant="outline" size="xs" [disabled]="isResendDisabled()" (click)="resendOtp()">
<ng-icon name="lucideRefreshCw" />
Resend code
@if (countdown() > 0) {
in {{ countdown() }}s
}
</button>
</div>
<hlm-input-otp
[length]="maxLength"
inputId="otp-verification"
[formField]="form.otp"
[transformPaste]="transformPaste"
(completed)="submit()"
>
<hlm-input-otp-group
class="*:data-[slot=input-otp-slot]:h-12 *:data-[slot=input-otp-slot]:w-11 *:data-[slot=input-otp-slot]:text-xl"
>
<hlm-input-otp-slot index="0" />
<hlm-input-otp-slot index="1" />
<hlm-input-otp-slot index="2" />
</hlm-input-otp-group>
<hlm-input-otp-separator />
<hlm-input-otp-group
class="*:data-[slot=input-otp-slot]:h-12 *:data-[slot=input-otp-slot]:w-11 *:data-[slot=input-otp-slot]:text-xl"
>
<hlm-input-otp-slot index="3" />
<hlm-input-otp-slot index="4" />
<hlm-input-otp-slot index="5" />
</hlm-input-otp-group>
</hlm-input-otp>
<hlm-field-description>
<a href="#">I no longer have access to this email address.</a>
</hlm-field-description>
</hlm-field>
</form>
<hlm-card-footer>
<hlm-field>
<button type="submit" form="otp-form" hlmBtn [disabled]="form().invalid()">Verify</button>
<div class="text-muted-foreground text-sm">
Having trouble signing in?
<a href="#" class="hover:text-primary underline underline-offset-4 transition-colors">Contact support</a>
</div>
</hlm-field>
</hlm-card-footer>
</hlm-card>
`,
})
export class InputOtpFormExample implements OnDestroy {
private _intervalId?: NodeJS.Timeout;
public readonly countdown = signal(60);
public readonly isResendDisabled = computed(() => this.countdown() > 0);
public maxLength = 6;
/** Overrides global formatDate */
public transformPaste = (pastedText: string) => pastedText.replaceAll('-', '');
private readonly _model = signal({
otp: '',
});
public readonly form = form(
this._model,
(schemaPath) => {
required(schemaPath.otp, { message: 'OTP is required' });
minLength(schemaPath.otp, this.maxLength, { message: `OTP must be ${this.maxLength} characters` });
maxLength(schemaPath.otp, this.maxLength, { message: `OTP must be ${this.maxLength} characters` });
},
{
submission: {
action: async () => {
const model = this._model();
console.log(model);
toast('OTP submitted', {
description: `Your OTP ${model.otp} has been submitted`,
});
},
},
},
);
constructor() {
afterNextRender(() => this.startCountdown());
}
submit() {
submit(this.form);
}
resendOtp() {
// add your api request here to resend OTP
this.resetCountdown();
}
ngOnDestroy() {
this.stopCountdown();
}
private resetCountdown() {
this.countdown.set(60);
this.startCountdown();
}
private startCountdown() {
this.stopCountdown();
this._intervalId = setInterval(() => {
this.countdown.update((countdown) => Math.max(0, countdown - 1));
if (this.countdown() === 0) {
this.stopCountdown();
}
}, 1000);
}
private stopCountdown() {
if (this._intervalId) {
clearInterval(this._intervalId);
this._intervalId = undefined;
}
}
}RTL
To enable RTL support in spartan-ng, see the RTL configuration guide.
import { Component, computed, inject } from '@angular/core';
import { TranslateService, Translations } from '@spartan-ng/app/app/shared/translate.service';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmInputOtpImports } from '@spartan-ng/helm/input-otp';
@Component({
selector: 'spartan-input-otp-rtl',
imports: [HlmFieldImports, HlmInputOtpImports],
template: `
<hlm-field class="mx-auto max-w-xs" [dir]="_dir()">
<label hlmFieldLabel for="input-otp-rtl">{{ _t()['verificationCode'] }}</label>
<hlm-input-otp inputId="input-otp-rtl" value="123456" length="6">
<hlm-input-otp-group>
<hlm-input-otp-slot index="0" />
<hlm-input-otp-slot index="1" />
<hlm-input-otp-slot index="2" />
<hlm-input-otp-slot index="3" />
<hlm-input-otp-slot index="4" />
<hlm-input-otp-slot index="5" />
</hlm-input-otp-group>
</hlm-input-otp>
</hlm-field>
`,
})
export class InputOtpRtl {
private readonly _language = inject(TranslateService).language;
private readonly _translations: Translations = {
en: {
dir: 'ltr',
values: {
verificationCode: 'Verification code',
},
},
ar: {
dir: 'rtl',
values: {
verificationCode: 'رمز التحقق',
},
},
he: {
dir: 'rtl',
values: {
verificationCode: 'קוד אימות',
},
},
};
private readonly _translation = computed(() => this._translations[this._language()]);
protected readonly _t = computed(() => this._translation().values);
protected readonly _dir = computed(() => this._translation().dir);
}Brain API
BrnInputOtpInput
Selector: input[brnInputOtpInput]
ExportAs: brnInputOtpInput
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| autocomplete | 'one-time-code' | 'off' | 'one-time-code' | Custom autocomplete attribute applied to the input element |
| inputMode | InputMode | 'numeric' | Virtual keyboard appearance on mobile |
| inputStyles | string | undefined | undefined | Styles applied to the input element to make it invisible and clickable. |
| aria-invalid | boolean | undefined | undefined | Overrides the error state matcher and forceInvalid for aria-invalid. |
| aria-describedby | string | null | null | - |
BrnInputOtpSlot
Selector: brn-input-otp-slot
ExportAs: brnInputOtpSlot
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| index* (required) | number | - | The index of the slot to render the char or a fake caret |
| forceInvalid | boolean | false | Whether to force the input into an invalid state. |
BrnInputOtp
Selector: brn-input-otp, [brnInputOtp]
ExportAs: brnInputOtp
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| forceInvalid | boolean | false | Whether to force the OTP into an invalid state. |
| hostStyles | string | 'position: relative; cursor: text; user-select: none; pointer-events: none;' | Styles applied to the host element. |
| inputId | string | `brn-input-otp-${++BrnInputOtp._id}` | Custom id applied to the input element |
| disabled | boolean | false | Determine if the input is disabled. |
| length* (required) | number | - | The number of slots. |
| slotPattern | string | RegExp | null | null | Optional per-character pattern. Each typed or pasted character must match this pattern. Strings are treated as a single-character regular expression. Examples: '[0-9]', '[A-Za-z0-9]' |
| mask | boolean | false | Whether entered characters should be visually masked. |
| maskChar | string | '•' | The character used to mask entered characters. |
| autofocus | boolean | false | If true, the element is focused automatically on init * |
| transformPaste | (pastedText: string, length: number) => string | (text) => text | Defines how the pasted text should be transformed before saving to model/form. Allows pasting text which contains extra characters like spaces, dashes, etc. and are longer than the length. "XXX-XXX": (pastedText) => pastedText.replaceAll('-', '') "XXX XXX": (pastedText) => pastedText.replaceAll(/\s+/g, '') |
| value | string | null | null | The value controlling the input |
Outputs
| Prop | Type | Default | Description |
|---|---|---|---|
| valueChange | string | - | Emits when the value changes. |
| completed | string | - | Emitted when the input is complete, triggered through input or paste. |
Helm API
HlmInputOtpFakeCaret
Selector: hlm-input-otp-fake-caret
ExportAs: hlmInputOtpFakeCaret
HlmInputOtpGroup
Selector: [hlmInputOtpGroup],hlm-input-otp-group
ExportAs: hlmInputOtpGroup
HlmInputOtpSeparator
Selector: hlm-input-otp-separator
ExportAs: hlmInputOtpSeparator
HlmInputOtpSlot
Selector: hlm-input-otp-slot
ExportAs: hlmInputOtpSlot
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| index* (required) | number | - | The index of the slot to render the char or a fake caret |
| forceInvalid | boolean | false | Whether to force the input into an invalid state. |
HlmInputOtp
Selector: hlm-input-otp
ExportAs: hlmInputOtp
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| inputAutocomplete | 'one-time-code' | 'off' | 'one-time-code' | Custom autocomplete attribute applied to the input element |
| inputMode | InputMode | 'numeric' | Virtual keyboard appearance on mobile |
| inputClass | ClassValue | '' | Classes applied to the input element. |
| inputStyles | string | undefined | undefined | Overrides the default styles applied to the input element. |
| aria-invalid | boolean | undefined | undefined | Overrides the invalid state announced for the input. When not set, follows the error state matcher. |
| aria-describedby | string | null | null | Additional IDs describing the input. |
| length* (required) | number | - | The number of slots. |
| value | string | null | null | The value controlling the input |
| disabled | boolean | false | Determine if the input is disabled. |
| inputId | string | `brn-input-otp-${++BrnInputOtp._id}` | Custom id applied to the input element |
| hostStyles | string | 'position: relative; cursor: text; user-select: none; pointer-events: none;' | Styles applied to the host element. |
| autofocus | boolean | false | If true, the element is focused automatically on init * |
| transformPaste | (pastedText: string, length: number) => string | (text) => text | Defines how the pasted text should be transformed before saving to model/form. Allows pasting text which contains extra characters like spaces, dashes, etc. and are longer than the length. "XXX-XXX": (pastedText) => pastedText.replaceAll('-', '') "XXX XXX": (pastedText) => pastedText.replaceAll(/\s+/g, '') |
| mask | boolean | false | Whether entered characters should be visually masked. |
| maskChar | string | '•' | The character used to mask entered characters. |
| slotPattern | string | RegExp | null | null | Optional per-character pattern. Each typed or pasted character must match this pattern. Strings are treated as a single-character regular expression. Examples: '[0-9]', '[A-Za-z0-9]' |
| forceInvalid | boolean | false | Whether to force the OTP into an invalid state. |
Outputs
| Prop | Type | Default | Description |
|---|---|---|---|
| valueChange | string | - | Emits when the value changes. |
| completed | string | - | Emitted when the input is complete, triggered through input or paste. |
On This Page