PasswordInput
A password field with a button that shows the password, and an indicator of the rules a new password meets.
Password is a password field with a button to show and hide what was typed.
Password shows how far a new password meets the rules your application sets.
Use them together for a new password, or the field alone to sign in.
- At least 12 characters not met
- A digit not met
- An upper-case and a lower-case letter not met
// apps/docs/examples/ui/password-input.tsx'use client';
import {useState} from 'react';
import {PasswordInput} from '@workspace/active-ui/components/composed/password-input';import {PasswordStrengthIndicator} from '@workspace/active-ui/components/composed/password-strength-indicator';import { Field, FieldLabel,} from '@workspace/active-ui/components/primitives/field';
// The rules are the application's: the indicator only shows them.function criteriaFor(password: string) { return [ {id: 'length', label: 'At least 12 characters', met: password.length >= 12}, {id: 'digit', label: 'A digit', met: /\d/.test(password)}, { id: 'case', label: 'An upper-case and a lower-case letter', met: /[a-z]/.test(password) && /[A-Z]/.test(password), }, ];}
/** * A new password with a button to show it, and the rules it has to meet. * Type to see the bar and the rules change; when every rule is met, one * confirmation replaces the list. */export default function PasswordInputExample() { const [password, setPassword] = useState(''); return ( <Field className="w-full max-w-sm"> <FieldLabel htmlFor="new-password">New password</FieldLabel> <PasswordInput id="new-password" autoComplete="new-password" value={password} onChange={(event) => setPassword(event.target.value)} ariaLabelShowPassword="Show password" ariaLabelHidePassword="Hide password" aria-describedby="new-password-rules" /> <PasswordStrengthIndicator id="new-password-rules" criteria={criteriaFor(password)} compliantLabel="The password meets every rule." criterionMetLabel="met" criterionUnmetLabel="not met" /> </Field> );}What the composition adds#
A reveal button that is a real tab stop, named after the action it offers: "Show password" while hidden, "Hide password" while shown.
The same field throughout. Showing switches
typeon the same input, so the value, the cursor,name,idandautostay, and password managers see one field.Complete No password-manager blocking. The field adds no
autoor ignore attributes; you declare the role withComplete="off" auto(Complete current-,password new-).password disabledcovers the button too;readleaves the button working, so the value can still be read.Only Rules as presentation. The indicator receives a list of translated rules with
metfor each, shows a bar and the open rules, and one confirmation once all are met. It validates nothing.
Usage#
import {PasswordInput} from '@workspace/active-ui/components/composed/password-input';import {PasswordStrengthIndicator} from '@workspace/active-ui/components/composed/password-strength-indicator';
<PasswordInput id="new-password" autoComplete="new-password" value={password} onChange={(event) => setPassword(event.target.value)} ariaLabelShowPassword="Show password" ariaLabelHidePassword="Hide password"/><PasswordStrengthIndicator criteria={[{id: 'length', label: 'At least 12 characters', met: password.length >= 12}]} compliantLabel="The password meets every rule." criterionMetLabel="met" criterionUnmetLabel="not met"/>What your application provides#
The rules and the check of each one, and the validation on submit: the indicator only shows.
Both translated button names, and the state words
metandnot metfor screen readers.auto, and for a secret that is not a sign-in credential, the attributes that tell password managers to skip it.Complete
Accessibility and keyboard#
Tabmoves from the field to the reveal button;EnterorSpacetoggles it.The bar and the icons are hidden from assistive technology; each rule carries its state as hidden text, so it is not shown by colour and shape alone.
The indicator is not a live region: it would talk on every keystroke. Link it to the field with
aria-, as in the example, so it is read with the field.describedby
API#
PasswordInput#
Import from @workspace/
Props
- Type
string
Accessible name of the button while the password is visible.
- Type
string
Accessible name of the button while the password is hidden: the action the button then offers.
Supplied translated and required; active- does not translate. An optional prop with an English default is not an i18n-agnostic API: it produces silent English text on every screen where the caller forgets it, and that text turns no check red. Same rule as open on Date.
- Type
Ref<HTMLInputElement>
Allows getting a ref to the component instance. Once the component unmounts, React will set ref. to null (or call the ref with null if you passed a callback ref).
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
Types
The props of Password, each described in its prop list on this page.
interface PasswordInputProps extends React.ComponentProps<'input'> {
ariaLabelShowPassword: string;
ariaLabelHidePassword: string;
}PasswordStrengthIndicator#
PasswordStrengthIndicator
Props
- Type
string
The translated text that appears as soon as all requirements are met.
- Type
readonly PasswordStrength Criterioโฆ
The requirements, in the order in which they are shown. An empty list means: nothing to show, and then this component renders nothing.
- Type
string
Translated state text for a met requirement, for example "Met".
Required, and visible only to assistive technology: the met/unmet distinction is visually in the icon shape, and that is aria-. Without this text a screen reader hears the requirement but not the state.
- Type
string
Translated state text for an open requirement, for example "Not met".
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
Types
One requirement a password must meet, in the form in which this component shows it.
This is presentation, not policy. active- does not know that something like a minimum length exists, knows no password rules and validates nothing; it receives a list of translated rules with a check mark each.
interface PasswordStrengthCriterion {
id: string;
label: string;
met: boolean;
}The props of Password, each described in its prop list on this page.
interface PasswordStrengthIndicatorProps extends Omit<
React.ComponentProps<'div'>,
'children'
> {
criteria: readonly PasswordStrengthCriterion[];
compliantLabel: string;
criterionMetLabel: string;
criterionUnmetLabel: string;
}