@molecule/app-password-strength-meter-angular

Feature · password-strength-meter · App (browser) · v1.1.1 · Apache-2.0

Real-time password strength meter — 5-segment colored bar, score label, optional rule checklist, in-package lightweight scorer

npm install @molecule/app-password-strength-meter-angular

npm · Source on GitHub

How it works

@molecule/app-password-strength-meter-angular is a ready-made password-strength-meter feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.

import { Component, signal } from '@angular/core'
import { PasswordStrengthMeter } from '@molecule/app-password-strength-meter-angular'

@Component({
  selector: 'app-signup-form',
  standalone: true,
  imports: [PasswordStrengthMeter],
  template: `
    <input [value]="password()" (input)="onInput($event)" />
    <div
      mol-password-strength-meter
      [password]="password()"
      [onScore]="setScore"
      [minScore]="2"
      [showChecklist]="true"
    ></div>
    <button [disabled]="score() < 2">Sign up</button>
  `,
})
export class SignupForm {
  readonly password = signal('')
  readonly score = signal(0)
  readonly setScore = (next: number): void => this.score.set(next)

  onInput(event: Event): void {
    this.password.set((event.target as HTMLInputElement).value)
  }
}

Works with: @molecule/app-angular, @molecule/app-i18n, @molecule/app-ui, @molecule/app-ui-angular

Reference

Real-time password-strength meter for Angular.

Renders a 5-segment colored bar (red→green), a textual strength label, and an optional rule checklist (length, upper, lower, digit, symbol, no-common). Score is computed by an in-package lightweight scorer — no zxcvbn dependency — using length × character-class entropy with a small built-in common-password dictionary penalty.

Used by signup, password-change, and password-manager flows to gate weak passwords before submit. All styling flows through getClassMap(); all text flows through t(); companion locale bond is @molecule/app-locales-password-strength-meter. It is the Angular counterpart of @molecule/app-password-strength-meter-react: same props and the same DOM for the same input.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

import { Component, signal } from '@angular/core'
import { PasswordStrengthMeter } from '@molecule/app-password-strength-meter-angular'

@Component({
  selector: 'app-signup-form',
  standalone: true,
  imports: [PasswordStrengthMeter],
  template: `
    <input [value]="password()" (input)="onInput($event)" />
    <div
      mol-password-strength-meter
      [password]="password()"
      [onScore]="setScore"
      [minScore]="2"
      [showChecklist]="true"
    ></div>
    <button [disabled]="score() < 2">Sign up</button>
  `,
})
export class SignupForm {
  readonly password = signal('')
  readonly score = signal(0)
  readonly setScore = (next: number): void => this.score.set(next)

  onInput(event: Event): void {
    this.password.set((event.target as HTMLInputElement).value)
  }
}

Type

feature

Installation

npm install @molecule/app-password-strength-meter-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-i18n @molecule/app-ui @molecule/app-ui-angular

API

Interfaces

PasswordChecklist

Per-rule checklist outcomes returned by scorePassword.

interface PasswordChecklist {
  /** Whether the password is at least 12 characters long. */
  length: boolean
  /** Whether the password contains an uppercase letter. */
  upper: boolean
  /** Whether the password contains a lowercase letter. */
  lower: boolean
  /** Whether the password contains a digit. */
  digit: boolean
  /** Whether the password contains a non-alphanumeric symbol. */
  symbol: boolean
  /** Whether the password is free of common-password substrings. */
  noCommon: boolean
}

PasswordScoreResult

Result of scoring a password.

interface PasswordScoreResult {
  /** Discrete strength bucket from 0–4. */
  score: PasswordScore
  /** Estimated entropy in bits used to derive the score. */
  entropyBits: number
  /** Per-rule outcomes for the optional checklist UI. */
  checklist: PasswordChecklist
}

PasswordStrengthMeterProps

Props accepted by PasswordStrengthMeter — the same props @molecule/app-password-strength-meter-react accepts, bound as inputs on div[mol-password-strength-meter].

interface PasswordStrengthMeterProps {
  /** Password value to score (controlled by the parent form). */
  password: string
  /**
   * Fires whenever the score changes — used by signup forms to gate
   * the submit button on `score >= minScore`.
   */
  onScore?: (score: PasswordScore) => void
  /** Minimum acceptable score for surrounding form validation. Default: `2`. */
  minScore?: PasswordScore
  /** Whether the textual strength label (e.g. "Strong") is shown. Default: `true`. */
  showLabel?: boolean
  /** Whether the optional rule checklist is rendered below the bar. Default: `false`. */
  showChecklist?: boolean
  /** Extra classes appended to the outer wrapper. */
  className?: string
}

Types

PasswordScore

Numeric password strength score, zxcvbn-style.

  • 0 very weak, 1 weak, 2 fair, 3 good, 4 strong.
type PasswordScore = 0 | 1 | 2 | 3 | 4

Classes

PasswordStrengthMeter

Real-time password strength meter.

Renders a 5-segment colored bar (red → green) plus an optional textual label and rule checklist. The host IS the outer <div> (div[mol-password-strength-meter]), matching React's root exactly. Scoring is performed by an in-package lightweight scorer (no zxcvbn dependency). All UI text flows through the binding's reactive t() so locale bonds can override translations and a locale switch re-renders the text; all styling flows through getClassMap(). Colors come from molecule's --mol-color-* design tokens.

@Component({
  selector: 'div[mol-password-strength-meter]',
  standalone: true,
  inputs: ['password', 'onScore', 'minScore', 'showLabel', 'showChecklist', 'className'],
  host: {
    '[class]': 'classes()',
    'data-mol-id': 'password-strength-meter',
  },
  // prettier-ignore
  template: `<div [class]="barClasses()" role="progressbar" [attr.aria-label]="barLabel()" [attr.aria-valuemin]="0" [attr.aria-valuemax]="4" [attr.aria-valuenow]="score()" [attr.aria-valuetext]="ariaValueText()" data-mol-id="password-strength-meter-bar" [attr.data-score]="score()" [attr.data-meets-min]="meetsMin() ? 'true' : 'false'"
  >@for (bucket of buckets; track bucket) {
    <span [attr.data-mol-id]="'password-strength-meter-segment-' + bucket" [attr.data-filled]="filled(bucket) ? 'true' : 'false'" [attr.style]="segmentStyle(bucket)"></span>
  }</div>@if (labelShown()) {
    <span [class]="labelClasses()" data-mol-id="password-strength-meter-label" [attr.style]="labelStyle()">{{ labelText() }}</span>
  }@if (checklistShown()) {
    <ul [class]="checklistClasses()" data-mol-id="password-strength-meter-checklist" style="list-style: none; margin: 0px; padding: 0px"
    >@for (field of checklistFields; track field) {
      <li [class]="ruleClasses()" [attr.data-mol-id]="'password-strength-meter-rule-' + field" [attr.data-ok]="ruleOk(field) ? 'true' : 'false'"
      ><span aria-hidden="true" [attr.style]="markerStyle(field)">{{ ruleOk(field) ? '✓' : '✗' }}</span
      ><span>{{ ruleLabel(field) }}</span></li>
    }</ul>
  }`,
})
export class PasswordStrengthMeter implements OnInit, OnChanges {
  /** Password value to score (controlled by the parent form). */
  password = ''
  /**
   * Fires whenever the score changes — used by signup forms to gate
   * the submit button on `score >= minScore`.
   */
  onScore?: (score: PasswordScore) => void
  /** Minimum acceptable score for surrounding form validation (defaults to 2 at read time). */
  minScore?: PasswordScore
  /** Whether the textual strength label is shown (defaults to `true` at read time). */
  showLabel?: boolean
  /** Whether the optional rule checklist is rendered (defaults to `false` at read time). */
  showChecklist?: boolean
  /** Extra classes appended to the outer wrapper. */
  className?: string

  /** The five score buckets, left to right. */
  protected readonly buckets = SCORE_BUCKETS

  /** The checklist rules, in React's order. */
  protected readonly checklistFields = CHECKLIST_FIELDS

  /** The reactive i18n binding, read in the injection context. */
  private readonly i18n = useTranslation()

  /** The bonded ClassMap (styling tokens resolve at render time). */
  protected readonly cm: UIClassMap = getClassMap()

  /** The last score `onScore` was called with (React's effect dependency). */
  private lastScore: PasswordScore = 0

  /** The callback `onScore` was last called with (React's effect dependency). */
  private lastOnScore: ((score: PasswordScore) => void) | undefined

  /** Whether the mount-time `onScore` call has happened. */
  private initialized = false

  /**
   * The score of a password, through the in-package scorer.
   *
   * @returns The discrete strength bucket.
   */
  score(): PasswordScore {
    return scorePassword(this.password).score
  }

  /**
   * The per-rule outcomes for the current password.
   *
   * @returns The checklist.
   */
  checklist(): PasswordChecklist {
    return scorePassword(this.password).checklist
  }

  /**
   * The wrapper class list (React's `cm.cn(cm.stack(2), className)`).
   *
   * @returns The class string.
   */
  classes(): string {
    return this.cm.cn(this.cm.stack(2), this.className)
  }

  /**
   * The bar's class (React's `cm.flex({ gap: 'xs' })`).
   *
   * @returns The class string.
   */
  barClasses(): string {
    return this.cm.flex({ gap: 'xs' })
  }

  /**
   * The label's class list (React's `cm.cn(cm.textSize('sm'), cm.fontWeight('medium'))`).
   *
   * @returns The class string.
   */
  labelClasses(): string {
    return this.cm.cn(this.cm.textSize('sm'), this.cm.fontWeight('medium'))
  }

  /**
   * The checklist list's class (React's `cm.stack(1)`).
   *
   * @returns The class string.
   */
  checklistClasses(): string {
    return this.cm.stack(1)
  }

  /**
   * One checklist row's class list.
   *
   * @returns The class string.
   */
  ruleClasses(): string {
    return this.cm.cn(this.cm.flex({ gap: 'xs', align: 'center' }), this.cm.textSize('sm'))
  }

  /**
   * Whether the textual label renders (React's `showLabel` default `true`).
   *
   * @returns True when the label shows.
   */
  labelShown(): boolean {
    return this.showLabel ?? true
  }

  /**
   * Whether the rule checklist renders (React's `showChecklist` default `false`).
   *
   * @returns True when the checklist shows.
   */
  checklistShown(): boolean {
    return this.showChecklist ?? false
  }

  /**
   * Whether the score meets the minimum (React's `minScore` default `2`).
   *
   * @returns True when `score >= minScore`.
   */
  meetsMin(): boolean {
    return this.score() >= (this.minScore ?? 2)
  }

  /** Whether a segment is filled: the password is non-empty and the bucket is at or below the score. */
  filled(bucket: PasswordScore): boolean {
    return this.password.length > 0 && bucket <= this.score()
  }

  /** React's inline style for one bar segment. */
  segmentStyle(bucket: PasswordScore): string {
    return `background: ${
      this.filled(bucket) ? colorForScore(this.score()) : 'var(--mol-color-border, rgba(0,0,0,0.1))'
    }; border-radius: 3px; flex: 1 1 0%; height: 6px; transition: background-color 150ms ease`
  }

  /** React's inline style for the label, or null (no style attribute) for an empty password. */
  labelStyle(): string | null {
    return this.password.length === 0 ? null : `color: ${colorForScore(this.score())}`
  }

  /** React's inline style for one checklist marker, red for ✗ and green for ✓. */
  markerStyle(field: keyof PasswordChecklist): string {
    return `color: ${
      this.ruleOk(field) ? 'var(--mol-color-success, #16a34a)' : 'var(--mol-color-error, #dc2626)'
    }; display: inline-block; font-weight: 600; width: 1em`
  }

  /** Whether one checklist rule passes. */
  ruleOk(field: keyof PasswordChecklist): boolean {
    return this.checklist()[field]
  }

  /**
   * The strength label's text (React's `t('passwordStrengthMeter.label.N')`).
   *
   * @returns The label text.
   */
  labelText(): string {
    return labelTextForScore(this.score(), this.i18n.t)
  }

  /**
   * The bar's accessible name.
   *
   * @returns The translated `aria-label`.
   */
  barLabel(): string {
    return this.i18n.t('passwordStrengthMeter.barLabel', undefined, {
      defaultValue: 'Password strength',
    })
  }

  /**
   * The bar's accessible value text.
   *
   * @returns The translated `aria-valuetext`.
   */
  ariaValueText(): string {
    return this.i18n.t(
      'passwordStrengthMeter.ariaValueText',
      { label: this.labelText(), score: this.score() },
      { defaultValue: 'Password strength: {{label}} ({{score}} of 4)' },
    )
  }

  /** One checklist rule's label. */
  ruleLabel(field: keyof PasswordChecklist): string {
    return ruleLabelForField(field, this.i18n.t)
  }

  /**
   * React's mount effect: `onScore` fires once with the initial score.
   */
  ngOnInit(): void {
    this.lastScore = this.score()
    this.lastOnScore = this.onScore
    this.initialized = true
    this.onScore?.(this.lastScore)
  }

  /** React's effect dependencies: fires `onScore` again only when the score or the callback changed (a new password in the same bucket stays quiet). */
  ngOnChanges(_changes: SimpleChanges): void {
    if (!this.initialized) return
    const current = this.score()
    const changed = current !== this.lastScore || this.onScore !== this.lastOnScore
    this.lastScore = current
    this.lastOnScore = this.onScore
    if (changed) this.onScore?.(current)
  }
}

(The 7 longest member comments are shortened to their first sentence to fit; the full declaration is in node_modules/@molecule/app-password-strength-meter-angular/dist/password-strength-meter.component.d.ts.)

Functions

scorePassword(password)

Computes a password's strength score and per-rule checklist.

The score is derived from length × log2(poolSize) (Shannon approximation) and then penalized when the password contains any common-password substring. Empty passwords return 0 with the full checklist set to false.

function scorePassword(password: string): PasswordScoreResult
  • password — The password to score.

Returns: The PasswordScoreResult for the supplied password.

Constants

COMMON_PASSWORDS

Common-password dictionary used by the scorer.

Drawn from public top-100 lists of frequently leaked passwords. Scoring matches case-insensitively against substrings of length ≥4 so derivatives like password1! are still penalized.

const COMMON_PASSWORDS: readonly string[]

Injection Notes

Requirements

Peer dependencies:

  • @angular/common ^22.0.0
  • @angular/core ^22.0.0
  • @angular/platform-browser ^22.0.0
  • @molecule/app-angular ^3.5.0 || ^4.0.0
  • @molecule/app-i18n ^1.0.1
  • @molecule/app-ui ^1.0.1
  • @molecule/app-ui-angular ^1.1.0

Runtime Dependencies

  • @angular/common
  • @angular/core
  • @angular/platform-browser
  • @molecule/app-angular
  • @molecule/app-i18n
  • @molecule/app-ui
  • @molecule/app-ui-angular

minScore only surfaces as a data-meets-min attribute on the bar — gate your submit button on the onScore value yourself (as in the example). Segment colors read var(--mol-color-error/warning/info/success) with built-in light fallbacks; molecule scaffolds define these in theme.css. Companion locale bond: @molecule/app-locales-password-strength-meter.

Translations

Translation strings are provided by @molecule/app-locales-password-strength-meter.