Password Input
Installation
npx shadcn@latest add https://neonkit.heyaadi.com/r/password-input.jsonUsage
import { PasswordInput } from "@/components/ui/password-input"
<PasswordInput label="New password" strength rules />Examples
Sign in, inside Field
Field supplies the label, the error and aria-describedby. Hold Caps Lock while typing to see the warning.
Controlled
value and visible are both controlled here: Generate fills a 20-character password and reveals it.
Your own policy
Pass rules for the checklist and evaluate to score by them instead of the built-in estimate.
API reference
PasswordInput
Built on Input, so label, description, size, leading and the other Input props work. The ref points at the input element.
| Prop | Type | Default |
|---|---|---|
value | stringControlled value. | None |
defaultValue | stringStarting value when uncontrolled. A form reset goes back to it. | "" |
onValueChange | (value: string) => voidCalled on every edit. onChange still works. | None |
visible | booleanControlled reveal. Shown as text, the value is set in mono so l, 1, I, O and 0 differ. | None |
defaultVisible | booleanStarting reveal state when uncontrolled. | false |
onVisibleChange | (visible: boolean) => voidCalled when the eye button is pressed. | None |
strength | booleanShows the four-segment meter and a Weak, Fair, Good or Strong label. | false |
rules | booleanShows a checklist. true uses length, a number, upper and lower case, and a symbol. | false |
minLength | numberLength the default rule and the estimate ask for. Set on the input (and so checked by the browser) only when you pass it. | 12 |
evaluate | (value: string, passed: Record<string, boolean>) => PasswordStrengthReplaces the built-in estimate. passed says which rules pass, by id. | None |
capsLockWarning | booleanShows a Caps Lock badge in the field while it's on, and announces it. | true |
error | ReactNodeTurns the field red and shows the message under the checklist. true for the red state alone. | None |
autoComplete | string"new-password" when strength or rules is on, so password managers offer to generate one. | "current-password" |
PasswordRule
| Prop | Type | Default |
|---|---|---|
id | stringKey in the passed record. | None |
label | ReactNodeChecklist text. | None |
test | (value: string) => booleanTrue when the rule is met. | None |
PasswordStrength
evaluatePassword(value, minLength?) is exported too, for gating a submit button.
| Prop | Type | Default |
|---|---|---|
score | 0Segments lit. 0 is empty. | None |
label | stringShown next to the meter and announced when it changes. | None |
Accessibility
- The eye button is a toggle with aria-pressed and a fixed name, Show password. Clicking it keeps focus and the caret in the field.
- The input's aria-describedby lists the Caps Lock status, the strength label, the checklist and the message, so a screen reader reads them after the label. Field's own ids are kept.
- The strength label is a polite live region, so it speaks when the label changes, not on every key.
- The estimate scores length times character variety and takes off for runs (abc, 321), repeats and about 25 common passwords, leetspeak included. It runs on every keystroke and never leaves the page.
- Inside Field, the field turns red with Field's error, because Field marks it aria-invalid.