Password Strength
The <smart-password-strength> component renders a three-bar strength indicator with an optional hint list of unmet requirements. It follows the Base + Standard + Wrapper pattern with an InjectionToken-based extension mechanism. It renders a default PasswordStrengthStandardComponent which can be replaced via PASSWORD_STRENGTH_STANDARD_COMPONENT_TOKEN.
Usage
<input
type="password"
autocomplete="new-password"
aria-label="Password"
[value]="password()"
(input)="onPasswordInput($event)"
/>
<smart-password-strength
[passwordToCheck]="password()"
[showHint]="true"
(passwordStrength)="onPasswordStrength($event)"
/>
@if (strong()) {
<p>The password is strong.</p>
}
The preview renders the code above in an application that registers every preset (provideSmartPresets(), see Installation) and the English translations; without the presets, a component that has one renders its standard implementation instead, for most components unstyled markup.
Components
PasswordStrengthComponent (<smart-password-strength>)
Main wrapper component. Renders PasswordStrengthStandardComponent by default. When PASSWORD_STRENGTH_STANDARD_COMPONENT_TOKEN is provided, renders the injected component via NgComponentOutlet, passes it passwordToCheck, showHint and class (resolved to the names that component declares, so the class arrives in the inherited cssClass input) and forwards the passwordStrength output of the injected instance through its own output. The host element is display: contents.
There is no preset for this component: provideSmartPresets() does not register anything for PASSWORD_STRENGTH_STANDARD_COMPONENT_TOKEN, and the standard meter is already styled.
PasswordStrengthStandardComponent (<smart-password-strength-standard>)
Default concrete implementation. Renders:
- three bars whose Tailwind classes come from
barClasses()(filled left to right by strength index: one red bar for poor, two orange for not good, three yellow for good, all gray while not rated), - a message
<p>with a translation keyINPUT.PASSWORD-STRENGTH.{poor|notGood|good}when applicable, - a hint
<ul>(shown whileshowHint()istrue) listing unmet requirements viaINPUT.ERRORS.*keys, - dark-mode variants via
smart:dark:*classes.
The container is a third of the width (smart:w-1/3, full width below sm), with the class input appended.
PasswordStrengthBaseComponent (abstract)
Abstract base directive for extending custom implementations. Exposes signal inputs, the output, the pure strength algorithm, and computed signals for classes and labels.
API
Inputs
| Input | Type | Default | Description |
|---|---|---|---|
passwordToCheck | InputSignal<string> | required | Password value to evaluate |
showHint | InputSignal<boolean> | required | Show a hint list of unmet requirements |
class | InputSignal<string> | '' | External CSS classes (alias for cssClass) |
Outputs
| Output | Type | Description |
|---|---|---|
passwordStrength | OutputEmitterRef<boolean> | Emits true when strength is maximal (lower+upper+symbol+length), false otherwise; it emits once on start and again whenever strength() changes. |
Exposed base signals (available when extending the base)
| Signal | Type | Description |
|---|---|---|
result() | { lowerLetters; upperLetters; symbols; passLength } | Flags for detected character classes and the length rule (more than 6 characters); the interface is not exported. |
strength() | number | The score: 10 (poor), 20 (not good), 30 (strong); any other value (an empty password, digits only) is not rated. |
strengthIndex() | 0 | 1 | 2 | 3 | 0=poor, 1=notGood, 2=good, 3=not rated. |
msg() | '' | 'poor' | 'notGood' | 'good' | Translation-key suffix for the message. |
barClasses() | string[] (length 3) | Tailwind class string per bar. |
msgClass() | string | Tailwind color class for the message <p>. |
containerClasses() | string | Base container classes with cssClass() appended. |
PASSWORD_STRENGTH_STANDARD_COMPONENT_TOKEN
InjectionToken that allows replacing the default PasswordStrengthStandardComponent with a custom implementation. Provide a Type<PasswordStrengthBaseComponent> to override.
Extending the base class
import {
ChangeDetectionStrategy,
Component,
computed,
signal,
ViewEncapsulation,
} from '@angular/core';
import {
PASSWORD_STRENGTH_STANDARD_COMPONENT_TOKEN,
PasswordStrengthBaseComponent,
PasswordStrengthComponent,
} from '@smartsoft001/angular';
const VERDICTS: Record<string, string> = {
poor: 'Poor',
notGood: 'Could be better',
good: 'Strong',
};
@Component({
selector: 'docs-custom-password-strength',
template: `
<div [class]="containerClasses()">
<div class="docs-password-strength__bars">
@for (barClass of barClasses(); track $index) {
<span class="docs-password-strength__bar" [class]="barClass"></span>
}
</div>
@if (verdict()) {
<p class="docs-password-strength__msg" [class]="msgClass()">
{{ verdict() }}
</p>
}
@if (showHint() && missing().length) {
<ul class="docs-password-strength__hints">
@for (requirement of missing(); track requirement) {
<li class="docs-password-strength__hint-item">{{ requirement }}</li>
}
</ul>
}
</div>
`,
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CustomPasswordStrengthComponent extends PasswordStrengthBaseComponent {
// The base class already computes strength, barClasses, msgClass and
// containerClasses (with the `class` passed to the wrapper), and emits
// passwordStrength from its own effect.
readonly verdict = computed(() => VERDICTS[this.msg()] ?? '');
readonly missing = computed(() => {
const { lowerLetters, upperLetters, symbols, passLength } = this.result();
return [
lowerLetters ? '' : 'a lowercase letter',
upperLetters ? '' : 'an uppercase letter',
symbols ? '' : 'a special character',
passLength ? '' : 'more than 6 characters',
].filter(Boolean);
});
}
@Component({
selector: 'docs-password-strength-custom-example',
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [PasswordStrengthComponent],
// The token swaps the standard meter for the custom one everywhere below
// this component, so consumers keep writing `<smart-password-strength>`.
providers: [
{
provide: PASSWORD_STRENGTH_STANDARD_COMPONENT_TOKEN,
useValue: CustomPasswordStrengthComponent,
},
],
template: `
<smart-password-strength
[passwordToCheck]="password()"
[showHint]="true"
(passwordStrength)="strong.set($event)"
/>
`,
})
export class PasswordStrengthCustomExampleComponent {
readonly password = signal('abc');
readonly strong = signal(false);
}
Translation Keys
The default template reads the following keys through TranslatePipe:
INPUT.PASSWORD-STRENGTH.poor— strength === 10INPUT.PASSWORD-STRENGTH.notGood— strength === 20INPUT.PASSWORD-STRENGTH.good— strength === 30INPUT.ERRORS.invalidMinLength— shown when password length ≤ 6 (suffixed with7)INPUT.ERRORS.upperLetters— shown when no uppercase letter detectedINPUT.ERRORS.lowerLetters— shown when no lowercase letter detectedINPUT.ERRORS.symbols— shown when no symbol detected
Source
The component lives in packages/shared/angular/src/lib/components/password-strength and is documented for Claude Code by the angular-components-password-strength skill.
SmartPasswordStrength rates passwordToCheck on four rules (lower-case letters, upper-case letters, symbols, more than 6 characters) and shows a three-bar meter with a translated message (INPUT.PASSWORD-STRENGTH.poor / notGood / good). With showHint it lists the rules the password does not meet yet. onPasswordStrength(strong) reports once on mount and again whenever the score changes; strong is true once all rules are met. There is no preset: the standard meter is already styled.
Usage
import { useState } from 'react';
import { SmartPasswordStrength } from '@smartsoft001/react';
// A placeholder, not a real credential: the meter rates whatever is typed.
const EXAMPLE_PASSWORD_VALUE = 'placeholder';
export function PasswordStrengthUsageExample() {
const [password, setPassword] = useState(EXAMPLE_PASSWORD_VALUE);
const [strong, setStrong] = useState(false);
return (
<>
<input
type="password"
autoComplete="new-password"
aria-label="Password"
value={password}
onChange={(event) => setPassword(event.target.value)}
/>
<SmartPasswordStrength
passwordToCheck={password}
showHint
onPasswordStrength={setStrong}
/>
{strong && <p>The password is strong.</p>}
</>
);
}
The preview renders the code above in an application that registers every preset (SMART_PRESET_COMPONENTS on SmartProvider, see Installation) and the English translations; without the presets, a component that has one renders its standard implementation instead, for most components unstyled markup.
Exports
All from @smartsoft001/react.
| Export | Kind | What it is |
|---|---|---|
SmartPasswordStrength | component | Renders the implementation registered as components['password-strength'] on SmartProvider, SmartPasswordStrengthStandard by default. |
SmartPasswordStrengthStandard | component | The default password strength meter. |
usePasswordStrength | hook | The rating every password strength variant shares. |
Props and Types
SmartPasswordStrengthProps
Props of <SmartPasswordStrength>.
| Prop | Type | Default | Description |
|---|---|---|---|
passwordToCheck | string | required | The password to rate. |
showHint | boolean | required | Lists the rules the password does not meet yet. |
className? | string | — | Classes appended to the container. |
onPasswordStrength? | (strong: boolean) => void | — | Called whenever the strength changes: true once the password is strong (lower and upper letters, a symbol and more than 6 characters). |
PasswordStrengthResult
Which rules the password meets.
| Field | Type | Default | Description |
|---|---|---|---|
lowerLetters | boolean | required | Contains a lower-case letter. |
upperLetters | boolean | required | Contains an upper-case letter. |
symbols | boolean | required | Contains a symbol. |
passLength | boolean | required | Longer than 6 characters. |
Related types
PasswordStrengthMessage:'' \| 'poor' \| 'notGood' \| 'good'— The message key suffix (''while empty).
Replacing the Implementation
SmartPasswordStrength renders the component registered under the 'password-strength' key of SmartProvider's components, and SmartPasswordStrengthStandard when nothing is registered there. Every SmartPasswordStrength below the provider then renders the registered component, which receives the same props.
There is no preset for this component: register a component of your own that takes SmartPasswordStrengthProps, as shown below, as components={{ 'password-strength': MyPasswordStrength }}. Pass components as a stable object (a module constant or a memoised value), or the context changes on every render.
The usePasswordStrength hook
The rating every password strength variant shares.
| Returns | Type | Description |
|---|---|---|
result | PasswordStrengthResult | The rules the password meets. |
strength | number | The score: 10 (poor), 20 (not good), 30 (strong); any other value (an empty password, digits only) is not rated. |
strengthIndex | 0 | 1 | 2 | 3 | 0 poor, 1 not good, 2 good, 3 not rated. |
msg | PasswordStrengthMessage | The message key suffix for INPUT.PASSWORD-STRENGTH.<msg>. |
barClasses | string[] | The classes of the three bars (red, orange or yellow fill per strength, gray when empty). |
msgClass | string | The colour class of the message. |
containerClasses | string | The container classes, with className appended. |
import { useState } from 'react';
import {
cn,
SmartPasswordStrength,
SmartPasswordStrengthProps,
SmartProvider,
usePasswordStrength,
} from '@smartsoft001/react';
const VERDICTS: Record<string, string> = {
poor: 'Poor',
notGood: 'Could be better',
good: 'Strong',
};
export function CustomPasswordStrength(props: SmartPasswordStrengthProps) {
// usePasswordStrength rates the password, computes the bar and message
// classes and calls onPasswordStrength whenever the rating changes.
const { result, msg, barClasses, msgClass, containerClasses } =
usePasswordStrength(props);
const verdict = VERDICTS[msg] ?? '';
const missing = [
result.lowerLetters ? '' : 'a lowercase letter',
result.upperLetters ? '' : 'an uppercase letter',
result.symbols ? '' : 'a special character',
result.passLength ? '' : 'more than 6 characters',
].filter(Boolean);
return (
<div className={containerClasses}>
<div className="docs-password-strength__bars">
{barClasses.map((barClass, index) => (
<span
key={index}
className={cn('docs-password-strength__bar', barClass)}
></span>
))}
</div>
{verdict && (
<p className={cn('docs-password-strength__msg', msgClass)}>{verdict}</p>
)}
{props.showHint && missing.length > 0 && (
<ul className="docs-password-strength__hints">
{missing.map((requirement) => (
<li key={requirement} className="docs-password-strength__hint-item">
{requirement}
</li>
))}
</ul>
)}
</div>
);
}
// A module constant: a new object on every render would change the context.
const components = { 'password-strength': CustomPasswordStrength };
export function PasswordStrengthCustomExample() {
const [password, setPassword] = useState('abc');
const [strong, setStrong] = useState(false);
// Every SmartPasswordStrength below the provider renders CustomPasswordStrength.
return (
<SmartProvider components={components}>
<input
type="password"
autoComplete="new-password"
aria-label="Password"
value={password}
onChange={(event) => setPassword(event.target.value)}
/>
<SmartPasswordStrength
passwordToCheck={password}
showHint
onPasswordStrength={setStrong}
/>
{strong && <p>The password is strong.</p>}
</SmartProvider>
);
}
Styling
- The standard meter fills one red bar for poor, two orange bars for not good and three yellow bars for good (gray while not rated), with
smart:dark:variants; the message takes the same colour. The container is a third of the width (smart:w-1/3, full width belowsm).
Source
The component lives in packages/shared/react/src/lib/components/password-strength and is documented for Claude Code by the react-components-password-strength skill.