Selection and input
Password input
A password field that coaches as you type: meter segments fill and change colour, requirements check off with a pop, and a show/hide button sits inside.
Examples and variants
With requirements
Type until every requirement is met.
- 8+ characters, not met
- A number, not met
- An uppercase letter, not met
- A symbol, not met
function StrongPassword() {
const [value, setValue] = useState("")
return (
<div className="w-full max-w-xs">
<PasswordInput value={value} onValueChange={setValue} />
</div>
)
}Spec drawing
Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.
Install
With the CLI
The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/password-input.jsonBy hand
No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.
Install the packages it uses.
Terminalnpm install cn lucide-reactCopy this file to the same paths in your project. Imports use the
@/alias for your project root.components/ui/password-input.tsxpassword-input
components/ui/password-input.tsx"use client" import * as React from "react" import { CheckIcon, EyeIcon, EyeOffIcon } from "lucide-react" import { cn } from "cn" /* Password input: a password field that coaches as you type. Meter segments fill and change colour; requirement chips check off one by one with a pop, and when the last one is met, they all pop together. A show/hide button sits inside the field. Requirements are always listed in text. */ type Rule = { label: string; test: (value: string) => boolean } const defaultRules: Rule[] = [ { label: "8+ characters", test: (v) => v.length >= 8 }, { label: "A number", test: (v) => /\d/.test(v) }, { label: "An uppercase letter", test: (v) => /[A-Z]/.test(v) }, { label: "A symbol", test: (v) => /[^A-Za-z0-9]/.test(v) }, ] function PasswordInput({ value, onValueChange, rules = defaultRules, label = "Password", id, className, }: { value: string onValueChange: (value: string) => void rules?: Rule[] label?: string id?: string className?: string }) { const [visible, setVisible] = React.useState(false) const autoId = React.useId() const inputId = id ?? autoId const met = rules.map((rule) => rule.test(value)) const score = met.filter(Boolean).length const all = score === rules.length const strength = all ? "strong" : score >= rules.length - 1 ? "good" : score >= 2 ? "fair" : value ? "weak" : "empty" return ( <div data-slot="password-input" data-strength={strength} className={cn("flex w-full flex-col gap-stack-sm", className)}> <label htmlFor={inputId} className="text-sm font-medium"> {label} </label> <div className="relative flex items-center"> <input id={inputId} type={visible ? "text" : "password"} value={value} autoComplete="new-password" aria-describedby={`${inputId}-rules`} onChange={(event) => onValueChange(event.target.value)} className="h-(--field-height-md) w-full rounded-(--field-corner) bg-(--field-container) ps-(--field-padding-x) pe-(--space-10) text-sm shadow-(--field-elevation) outline-none transition-[background-color,box-shadow] duration-(--duration-fast) focus-visible:bg-(--field-container-focus) focus-visible:shadow-(--field-focus-shadow)" /> <button type="button" aria-label={visible ? "Hide password" : "Show password"} aria-pressed={visible} onClick={() => setVisible((v) => !v)} className="liquid-press absolute end-1 inline-flex size-(--size-control-sm) items-center justify-center rounded-(--shape-full) text-(--field-action-label) outline-none hover:bg-(--field-action-container-hover) hover:text-(--field-action-label-hover) active:bg-(--field-action-container-pressed) focus-visible:shadow-(--focus-ring) [&_svg]:size-(--size-icon-md)" > <span key={String(visible)} className="icon-morph inline-flex">{visible ? <EyeOffIcon /> : <EyeIcon />}</span> </button> </div> <div aria-hidden className="grid grid-cols-4 gap-1"> {rules.map((_, i) => ( <span key={i} data-on={i < score || undefined} className="password-segment h-(--password-segment-height) rounded-(--shape-full) bg-(--password-segment)" /> ))} </div> <ul id={`${inputId}-rules`} aria-label="Requirements" data-all={all || undefined} className="password-rules flex flex-wrap gap-1.5"> {rules.map((rule, i) => ( <li key={rule.label} data-met={met[i] || undefined} className="password-rule inline-flex h-6 items-center gap-1 rounded-(--shape-full) px-2 text-caption normal-case" > {met[i] ? <CheckIcon aria-hidden className="size-3" /> : null} {rule.label} <span className="sr-only">{met[i] ? ", met" : ", not met"}</span> </li> ))} </ul> <p role="status" className="sr-only"> {value ? `Password strength: ${strength}` : ""} </p> </div> ) } export { PasswordInput } export type { Rule as PasswordRule }
Imports
What the examples on this page import:
import { Chip, ChipRemove } from "@/components/ui/chip"
import { MagneticText } from "@/components/ui/magnetic-text"
import { PasswordInput } from "@/components/ui/password-input"
import { Progress } from "@/components/ui/progress"
import { tokenMs } from "@/lib/motion/token-ms"
import { motionLevel } from "@/lib/overlay"Anatomy
- 1Field · Text field tokens; show/hide button inside.
- 2Meter · --password-segment-height, --password-segment; weak, fair, good, strong colours.
- 3Requirements · --password-rule, --password-rule-met, --password-rule-met-label.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
value / onValueChange | string | — | Controlled value. |
rules | { label, test(value) }[] | — | Defaults: 8+ characters, a number, an uppercase letter, a symbol. |
label, id | string | — | Label and input id. |
Tokens
Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.
| Token | Points to | Light | Dark |
|---|---|---|---|
--password-segment-height | var(--space-1) | 4px | same |
--password-segment | var(--surface-container-high) | oklch(0.94 0.007 95) | oklch(0.27 0 0) |
--password-weak | var(--color-danger) | oklch(0.55 0.2 25) | oklch(0.68 0.18 22) |
--password-fair | var(--color-warning) | oklch(0.72 0.14 75) | oklch(0.78 0.15 75) |
--password-good | var(--color-info) | oklch(0.6 0.17 248) | oklch(0.72 0.14 245) |
--password-strong | var(--color-success) | oklch(0.52 0.14 150) | oklch(0.72 0.17 150) |
--password-rule | var(--surface-container) | oklch(0.965 0.005 95) | oklch(0.12 0 0) |
--password-rule-met | var(--color-success-subtle) | color-mix(in oklch, var(--ref-success) 10%, transparent) | color-mix(in oklch, var(--ref-success) 16%, transparent) |
--password-rule-met-label | var(--color-success-fg) | oklch(0.48 0.13 150) | oklch(0.8 0.15 150) |
Motion
| Trigger | What happens | Tokens |
|---|---|---|
| Type | Segments fill and stretch; met chips pop; all pop together at the end. | --duration-fast, --duration-medium |
Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.
Accessibility
- Requirements are text, linked with aria-describedby; strength is announced politely.
- autocomplete=new-password.
Usage
Do
- Show the rules before people type.
Don’t
- Don't rely on colour alone; the text says met or not met.
Code
"use client"
import * as React from "react"
import { CheckIcon, EyeIcon, EyeOffIcon } from "lucide-react"
import { cn } from "cn"
/*
Password input: a password field that coaches as you type. Meter segments
fill and change colour; requirement chips check off one by one with a pop,
and when the last one is met, they all pop together. A show/hide button
sits inside the field. Requirements are always listed in text.
*/
type Rule = { label: string; test: (value: string) => boolean }
const defaultRules: Rule[] = [
{ label: "8+ characters", test: (v) => v.length >= 8 },
{ label: "A number", test: (v) => /\d/.test(v) },
{ label: "An uppercase letter", test: (v) => /[A-Z]/.test(v) },
{ label: "A symbol", test: (v) => /[^A-Za-z0-9]/.test(v) },
]
function PasswordInput({
value,
onValueChange,
rules = defaultRules,
label = "Password",
id,
className,
}: {
value: string
onValueChange: (value: string) => void
rules?: Rule[]
label?: string
id?: string
className?: string
}) {
const [visible, setVisible] = React.useState(false)
const autoId = React.useId()
const inputId = id ?? autoId
const met = rules.map((rule) => rule.test(value))
const score = met.filter(Boolean).length
const all = score === rules.length
const strength = all ? "strong" : score >= rules.length - 1 ? "good" : score >= 2 ? "fair" : value ? "weak" : "empty"
return (
<div data-slot="password-input" data-strength={strength} className={cn("flex w-full flex-col gap-stack-sm", className)}>
<label htmlFor={inputId} className="text-sm font-medium">
{label}
</label>
<div className="relative flex items-center">
<input
id={inputId}
type={visible ? "text" : "password"}
value={value}
autoComplete="new-password"
aria-describedby={`${inputId}-rules`}
onChange={(event) => onValueChange(event.target.value)}
className="h-(--field-height-md) w-full rounded-(--field-corner) bg-(--field-container) ps-(--field-padding-x) pe-(--space-10) text-sm shadow-(--field-elevation) outline-none transition-[background-color,box-shadow] duration-(--duration-fast) focus-visible:bg-(--field-container-focus) focus-visible:shadow-(--field-focus-shadow)"
/>
<button
type="button"
aria-label={visible ? "Hide password" : "Show password"}
aria-pressed={visible}
onClick={() => setVisible((v) => !v)}
className="liquid-press absolute end-1 inline-flex size-(--size-control-sm) items-center justify-center rounded-(--shape-full) text-(--field-action-label) outline-none hover:bg-(--field-action-container-hover) hover:text-(--field-action-label-hover) active:bg-(--field-action-container-pressed) focus-visible:shadow-(--focus-ring) [&_svg]:size-(--size-icon-md)"
>
<span key={String(visible)} className="icon-morph inline-flex">{visible ? <EyeOffIcon /> : <EyeIcon />}</span>
</button>
</div>
<div aria-hidden className="grid grid-cols-4 gap-1">
{rules.map((_, i) => (
<span key={i} data-on={i < score || undefined} className="password-segment h-(--password-segment-height) rounded-(--shape-full) bg-(--password-segment)" />
))}
</div>
<ul id={`${inputId}-rules`} aria-label="Requirements" data-all={all || undefined} className="password-rules flex flex-wrap gap-1.5">
{rules.map((rule, i) => (
<li
key={rule.label}
data-met={met[i] || undefined}
className="password-rule inline-flex h-6 items-center gap-1 rounded-(--shape-full) px-2 text-caption normal-case"
>
{met[i] ? <CheckIcon aria-hidden className="size-3" /> : null}
{rule.label}
<span className="sr-only">{met[i] ? ", met" : ", not met"}</span>
</li>
))}
</ul>
<p role="status" className="sr-only">
{value ? `Password strength: ${strength}` : ""}
</p>
</div>
)
}
export { PasswordInput }
export type { Rule as PasswordRule }