Skip to content

Input OTP

Boxes for a one-time code.

View as Markdown

Example

Sizes

xs, sm, md (default) and lg: set size="sm" on it, or data-size="sm" on any container, as here. See Theming.

xs
sm
md
lg

Installation

npx fizzy-ui add input-otp

Copies input-otp.ts into src/app/ui/ and installs what it needs. Run init once first.

Keyboard

KeyAction
0–9Fill the next box
BackspaceClear the last box
Ctrl/⌘ + VPaste a whole code (spaces and dashes are dropped)
← / →Move through the code, as in any text field

Accessibility

No axe-core violations axe-core 4.14.0 checks this example, closed and with its panels open, on every build; the site is not published otherwise.

  • One native text input under the boxes: screen readers read and edit it like any field, and the boxes are aria-hidden.
  • autocomplete="one-time-code" lets phones fill the code from an SMS; digits bring up the numeric keyboard.
  • Inside fzField the label, hint and error are wired to the input.

Testing

FzInputOtpHarness drives it in your tests, the way a user would, with the Angular CDK harness API. --harness also copies input-otp.harness.ts; its methods are documented in the file.

npx fizzy-ui add input-otp --harness
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { FzInputOtpHarness } from './ui/input-otp.harness';

const inputOtp = await TestbedHarnessEnvironment.loader(fixture).getHarness(FzInputOtpHarness);

Source

The exact file add copies. It is yours to edit.

input-otp.ts
import { Component, ElementRef, booleanAttribute, computed, input, model, numberAttribute, output, signal, viewChild } from '@angular/core';

type FzSize = 'xs' | 'sm' | 'md' | 'lg';

/**
 * A one-time code: `<fz-input-otp [(value)]="code" (complete)="verify($event)" aria-label="Verification code" />`,
 * or inside `fzField` with a label. One real `<input>` (transparent, over the boxes), so pasting
 * "123 456", SMS autofill (`autocomplete="one-time-code"`), the numeric keyboard and screen readers all
 * work as in any text field; the boxes are only a drawing of it. Characters that don't fit the
 * `charset` are dropped. (Not `pattern`: Signal Forms binds that name.)
 */
@Component({
  selector: 'fz-input-otp',
  host: {
    '[attr.aria-label]': 'null',
    '[attr.data-size]': 'size() ?? null',
    class: 'group/otp relative inline-flex w-fit has-disabled:opacity-50',
  },
  template: `
    <div class="flex gap-2" aria-hidden="true">
      @for (char of slots(); track $index) {
        <div
          [attr.data-active]="active($index) || null"
          class="relative flex size-(--fz-h) items-center justify-center rounded-md border bg-background text-(length:--fz-text) font-medium shadow-xs transition-shadow data-active:ring-2 data-active:ring-ring group-has-[input[aria-invalid=true]]/otp:border-destructive"
        >
          {{ char }}
          @if (caret() === $index) {
            <span class="absolute h-1/2 w-px bg-foreground motion-safe:animate-pulse"></span>
          }
        </div>
      }
    </div>
    <input
      #field
      [attr.id]="inputId() ?? null"
      [attr.aria-label]="ariaLabel() ?? null"
      [value]="text()"
      [disabled]="disabled()"
      autocomplete="one-time-code"
      [attr.inputmode]="charset() === 'digits' ? 'numeric' : 'text'"
      [attr.pattern]="charset() === 'digits' ? '[0-9]*' : null"
      spellcheck="false"
      autocapitalize="off"
      class="absolute inset-0 size-full cursor-text bg-transparent text-transparent caret-transparent outline-none selection:bg-transparent disabled:cursor-not-allowed"
      (input)="onInput()"
      (focus)="focused.set(true); sync()"
      (blur)="focused.set(false); touch.emit()"
      (pointerup)="toEnd()"
      (selectionchange)="sync()"
      (keyup)="sync()"
      (select)="sync()"
    />
  `,
})
export class FzInputOtp {
  readonly value = model('');
  readonly length = input(6, { transform: numberAttribute });
  /** `digits` (default) or `alphanumeric` (letters and digits). */
  readonly charset = input<'digits' | 'alphanumeric'>('digits');
  readonly disabled = input(false, { transform: booleanAttribute });
  /** xs, sm, md, lg. Unset, it takes the size of the nearest `data-size` container, else md. */
  readonly size = input<FzSize>();
  /** Id of the inner input, for a `<label for>` outside `fzField`. */
  readonly inputId = input<string>();
  readonly ariaLabel = input<string | undefined>(undefined, { alias: 'aria-label' });
  /** Emits the code once every box is filled. */
  readonly complete = output<string>();
  /** Emits on blur: forms mark the field touched. */
  readonly touch = output();

  protected readonly focused = signal(false);
  private readonly selection = signal<[number, number]>([0, 0]);
  private readonly field = viewChild.required<ElementRef<HTMLInputElement>>('field');

  /** Forms write null (ngModel before its first value, `reset()`): read it as empty. */
  protected readonly text = computed(() => this.value() ?? '');
  protected readonly slots = computed(() => Array.from({ length: this.length() }, (_, i) => this.text()[i] ?? ''));
  /** The box with the blinking caret: the next one to fill, while focused and nothing is selected. */
  protected readonly caret = computed(() => {
    const [start, end] = this.selection();
    return this.focused() && start === end && start === this.text().length && start < this.length() ? start : -1;
  });

  protected active(i: number) {
    if (!this.focused()) return false;
    const [start, end] = this.selection();
    return start === end ? i === Math.min(start, this.length() - 1) : i >= start && i < end;
  }

  protected onInput() {
    const field = this.field().nativeElement;
    const clean = field.value.replace(this.charset() === 'digits' ? /\D/g : /[^\da-z]/gi, '').slice(0, this.length());
    if (clean !== field.value) field.value = clean;
    this.value.set(clean);
    this.sync();
    if (clean.length === this.length()) this.complete.emit(clean);
  }

  // A click lands on a box, not on the (invisible) text: put the caret where typing continues.
  protected toEnd() {
    const field = this.field().nativeElement;
    if (field.selectionStart !== field.selectionEnd) return;
    field.setSelectionRange(field.value.length, field.value.length);
    this.sync();
  }

  protected sync() {
    const field = this.field().nativeElement;
    this.selection.set([field.selectionStart ?? 0, field.selectionEnd ?? 0]);
  }
}

export const FZ_INPUT_OTP = [FzInputOtp] as const;