Password strength meter
A password field with a four-segment meter, a live checklist that ticks itself off, and a show/hide eye that blinks.
"use client";
import { useId, useState } from "react";
import "./password-strength.css";
const CHECKS = [
{ label: "At least 10 characters", test: (v: string) => v.length >= 10 },
{
label: "Upper and lower case letters",
test: (v: string) => /[a-z]/.test(v) && /[A-Z]/.test(v),
},
{ label: "A number", test: (v: string) => /\d/.test(v) },
{
label: "A symbol like ! ? # or %",
test: (v: string) => /[^A-Za-z0-9]/.test(v),
},
];
const LEVELS = ["Too short", "Weak", "Fair", "Good", "Strong"];
/**
* PasswordStrength
* - Score = number of passed checks (0–4). Each meter segment fills with its own color.
* - The checklist doubles as the explanation, so the meter never feels arbitrary.
*/
export function PasswordStrength({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const id = useId();
const [value, setValue] = useState("");
const [show, setShow] = useState(false);
const passed = CHECKS.map((c) => c.test(value));
const score = value ? passed.filter(Boolean).length : 0;
return (
<div className="gu-pass" data-theme={theme} data-score={score}>
<label htmlFor={id}>Choose a password</label>
<div className="gu-pass__field">
<input
id={id}
type={show ? "text" : "password"}
value={value}
onChange={(e) => setValue(e.target.value)}
autoComplete="new-password"
autoCapitalize="off"
autoCorrect="off"
spellCheck={false}
aria-describedby={`${id}-meter ${id}-list`}
/>
<button
type="button"
className="gu-pass__eye"
aria-pressed={show}
aria-label={show ? "Hide password" : "Show password"}
onClick={() => setShow((s) => !s)}
>
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M2 12s3.6-7 10-7 10 7 10 7-3.6 7-10 7S2 12 2 12Z" />
<circle cx="12" cy="12" r="3" />
<path className="gu-pass__slash" d="M4 4l16 16" />
</svg>
</button>
</div>
<div className="gu-pass__meter" id={`${id}-meter`} aria-live="polite">
<div className="gu-pass__bars" aria-hidden="true">
{[1, 2, 3, 4].map((n) => (
<span key={n} data-on={score >= n ? "" : undefined} />
))}
</div>
<span className="gu-pass__level">
{value ? LEVELS[score] : "Strength"}
</span>
</div>
<ul className="gu-pass__list" id={`${id}-list`}>
{CHECKS.map((c, i) => (
<li key={c.label} data-ok={passed[i] ? "" : undefined}>
<svg viewBox="0 0 16 16" aria-hidden="true">
<circle cx="8" cy="8" r="7" />
<path d="m4.8 8.3 2.2 2.2 4.2-4.6" />
</svg>
{c.label}
<span className="gu-sr">
{passed[i] ? " (done)" : " (not yet)"}
</span>
</li>
))}
</ul>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Password strength meter
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A password field with a show/hide eye, a four-segment strength meter and a checklist that ticks itself off as rules are met.
## Layout and look
- Card 380px max, padding 24px, radius 22px. Bold label, 54px monospace input with a 44px eye button inside on the right.
- Meter: four 6px rounded bars plus a level word on the right (Too short, Weak, Fair, Good, Strong). All filled bars take the color of the current score: red, orange, yellow, green.
- Checklist: four rows with an 18px circle; when a rule passes the circle fills green and a white check draws in.
## Behavior and motion
1. Score = number of passed rules (10+ chars, mixed case, a number, a symbol).
2. Bars fill with scaleX from the left (400ms cubic-bezier(0.16, 1, 0.3, 1)).
3. Eye: the slash line draws on/off with stroke-dashoffset; on press the icon squashes vertically like a blink.
## Touch
44px eye target. autocapitalize and autocorrect off.
## Accessibility
- Real label; the input is described by the meter and the checklist. The meter area is aria-live="polite".
- The eye is a toggle button with aria-pressed and a changing label.
- Each rule reads "(done)" or "(not yet)" for screen readers.
- prefers-reduced-motion: no transitions.
## Props
theme. Rules live in the CHECKS array at the top.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### PasswordStrength.tsx
```tsx
"use client";
import { useId, useState } from "react";
import "./password-strength.css";
const CHECKS = [
{ label: "At least 10 characters", test: (v: string) => v.length >= 10 },
{
label: "Upper and lower case letters",
test: (v: string) => /[a-z]/.test(v) && /[A-Z]/.test(v),
},
{ label: "A number", test: (v: string) => /\d/.test(v) },
{
label: "A symbol like ! ? # or %",
test: (v: string) => /[^A-Za-z0-9]/.test(v),
},
];
const LEVELS = ["Too short", "Weak", "Fair", "Good", "Strong"];
/**
* PasswordStrength
* - Score = number of passed checks (0–4). Each meter segment fills with its own color.
* - The checklist doubles as the explanation, so the meter never feels arbitrary.
*/
export function PasswordStrength({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const id = useId();
const [value, setValue] = useState("");
const [show, setShow] = useState(false);
const passed = CHECKS.map((c) => c.test(value));
const score = value ? passed.filter(Boolean).length : 0;
return (
<div className="gu-pass" data-theme={theme} data-score={score}>
<label htmlFor={id}>Choose a password</label>
<div className="gu-pass__field">
<input
id={id}
type={show ? "text" : "password"}
value={value}
onChange={(e) => setValue(e.target.value)}
autoComplete="new-password"
autoCapitalize="off"
autoCorrect="off"
spellCheck={false}
aria-describedby={`${id}-meter ${id}-list`}
/>
<button
type="button"
className="gu-pass__eye"
aria-pressed={show}
aria-label={show ? "Hide password" : "Show password"}
onClick={() => setShow((s) => !s)}
>
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M2 12s3.6-7 10-7 10 7 10 7-3.6 7-10 7S2 12 2 12Z" />
<circle cx="12" cy="12" r="3" />
<path className="gu-pass__slash" d="M4 4l16 16" />
</svg>
</button>
</div>
<div className="gu-pass__meter" id={`${id}-meter`} aria-live="polite">
<div className="gu-pass__bars" aria-hidden="true">
{[1, 2, 3, 4].map((n) => (
<span key={n} data-on={score >= n ? "" : undefined} />
))}
</div>
<span className="gu-pass__level">
{value ? LEVELS[score] : "Strength"}
</span>
</div>
<ul className="gu-pass__list" id={`${id}-list`}>
{CHECKS.map((c, i) => (
<li key={c.label} data-ok={passed[i] ? "" : undefined}>
<svg viewBox="0 0 16 16" aria-hidden="true">
<circle cx="8" cy="8" r="7" />
<path d="m4.8 8.3 2.2 2.2 4.2-4.6" />
</svg>
{c.label}
<span className="gu-sr">
{passed[i] ? " (done)" : " (not yet)"}
</span>
</li>
))}
</ul>
</div>
);
}
```
### password-strength.css
```css
/* PasswordStrength: change these colors to match your brand. */
.gu-pass {
--card: #fbf8f2;
--field: #f3eee3;
--text: #0e0e10;
--muted: #57534b;
--line: rgb(14 14 16 / 0.18);
--empty: rgb(14 14 16 / 0.1);
--focus: #2b35ff;
--s1: #e11d48;
--s2: #ff5a1f;
--s3: #eab308;
--s4: #16a34a;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
display: grid;
gap: 12px;
width: min(100%, 380px);
padding: 24px;
border: 1px solid var(--line);
border-radius: 22px;
background: var(--card);
color: var(--text);
font-family: system-ui, sans-serif;
}
.gu-pass[data-theme="dark"] {
--card: #18181b;
--field: #0e0e10;
--text: #f3eee3;
--muted: #aea89c;
--line: rgb(243 238 227 / 0.18);
--empty: rgb(243 238 227 / 0.12);
--focus: #d7ff3a;
--s1: #fb7185;
--s3: #facc15;
--s4: #4ade80;
}
.gu-pass label {
font-weight: 800;
}
.gu-pass__field {
position: relative;
}
.gu-pass input {
width: 100%;
height: 54px;
padding: 0 54px 0 16px;
border: 1.5px solid var(--line);
border-radius: 14px;
outline: none;
background: var(--field);
color: inherit;
font:
600 1.05rem ui-monospace,
monospace;
letter-spacing: 0.05em;
}
.gu-pass input:focus {
border-color: var(--focus);
box-shadow: 0 0 0 4px color-mix(in srgb, var(--focus) 18%, transparent);
}
.gu-pass__eye {
position: absolute;
top: 5px;
right: 5px;
display: grid;
place-items: center;
width: 44px;
height: 44px;
border: 0;
border-radius: 10px;
background: none;
color: var(--muted);
cursor: pointer;
}
.gu-pass__eye:hover {
color: var(--text);
}
.gu-pass__eye:focus-visible {
outline: 3px solid var(--focus);
}
.gu-pass__eye svg {
width: 22px;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
}
/* The eye "blinks" closed by squashing vertically when toggled */
.gu-pass__eye svg {
transition: transform 0.25s var(--ease);
}
.gu-pass__eye:active svg {
transform: scaleY(0.15);
}
.gu-pass__slash {
stroke-dasharray: 23;
stroke-dashoffset: 0;
transition: stroke-dashoffset 0.3s var(--ease);
}
.gu-pass__eye[aria-pressed="true"] .gu-pass__slash {
stroke-dashoffset: 23;
}
.gu-pass__meter {
display: flex;
align-items: center;
gap: 12px;
}
.gu-pass__bars {
flex: 1;
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 6px;
}
.gu-pass__bars span {
height: 6px;
border-radius: 6px;
background: var(--empty);
overflow: hidden;
position: relative;
}
.gu-pass__bars span::after {
content: "";
position: absolute;
inset: 0;
background: var(--meter, var(--s1));
transform: scaleX(0);
transform-origin: left;
transition:
transform 0.4s var(--ease),
background-color 0.3s;
}
.gu-pass__bars span[data-on]::after {
transform: scaleX(1);
}
.gu-pass[data-score="1"] {
--meter: var(--s1);
}
.gu-pass[data-score="2"] {
--meter: var(--s2);
}
.gu-pass[data-score="3"] {
--meter: var(--s3);
}
.gu-pass[data-score="4"] {
--meter: var(--s4);
}
.gu-pass__level {
min-width: 5.5em;
color: var(--muted);
font-size: 0.85rem;
font-weight: 700;
text-align: right;
}
.gu-pass[data-score="4"] .gu-pass__level {
color: var(--s4);
}
.gu-pass__list {
display: grid;
gap: 8px;
margin: 4px 0 0;
padding: 0;
list-style: none;
font-size: 0.9rem;
color: var(--muted);
}
.gu-pass__list li {
display: flex;
align-items: center;
gap: 10px;
transition: color 0.2s;
}
.gu-pass__list svg {
flex: none;
width: 18px;
fill: none;
stroke-width: 1.6;
}
.gu-pass__list circle {
stroke: var(--line);
transition:
fill 0.25s,
stroke 0.25s;
}
.gu-pass__list path {
stroke: var(--card);
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
stroke-dasharray: 10;
stroke-dashoffset: 10;
transition: stroke-dashoffset 0.3s 0.1s var(--ease);
}
.gu-pass__list li[data-ok] {
color: var(--text);
}
.gu-pass__list li[data-ok] circle {
fill: var(--s4);
stroke: var(--s4);
}
.gu-pass__list li[data-ok] path {
stroke-dashoffset: 0;
}
.gu-sr {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
@media (prefers-reduced-motion: reduce) {
.gu-pass *,
.gu-pass *::after {
transition: none !important;
}
}
```
You might also want
Next piece: Photographer portfolioFloating label form
Components, beginner
One-time code input
Components, intermediate
Copy field
Components, beginner