Match
import { useId, useState } from 'react';
const css = `
@keyframes field-match-shake { 25% { transform: translateX(-2px); } 75% { transform: translateX(2px); } }
.field-match { display: grid; width: 250px; }
.field-match__label { display: block; margin-bottom: 6px; color: light-dark(#50535b, #c6c8cd); font-size: 12px; }
.field-match__input { width: 100%; height: 40px; padding: 0 12px; border: 0; border-radius: 10px; outline: 0; background: light-dark(#f3f4f7, #1a1c1f); box-shadow: inset 0 0 0 1px light-dark(#dbdde1, #2e3035); color: light-dark(#373a40, #f1f2f3); font: 500 14px/1 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 0.2em; transition: box-shadow 0.2s; }
.field-match__input:focus { box-shadow: inset 0 0 0 1px light-dark(#5f626a, #a7abb2), 0 0 0 3px color-mix(in oklab, light-dark(#373a40, #f1f2f3) 10%, transparent); }
.field-match__input::placeholder { color: light-dark(#6d7179, #7d8087); letter-spacing: 0; font-family: system-ui, sans-serif; }
/* The beads between the fields: one per character of the password, linked once the confirmation matches it. */
.field-match__chain { position: relative; display: flex; align-items: center; gap: 6px; height: 26px; padding: 0 14px; }
.field-match__bead { position: relative; width: 7px; height: 7px; border-radius: 50%; box-shadow: inset 0 0 0 1.2px light-dark(#6d7179, #7d8087); transition: background-color 0.2s, box-shadow 0.2s, transform 0.3s cubic-bezier(0.3, 1.8, 0.5, 1); }
.field-match__bead::after { content: ''; position: absolute; left: 7px; top: 3px; width: 6px; height: 1.2px; background: light-dark(#42454c, #e5e6e9); transform: scaleX(0); transform-origin: 0 50%; transition: transform 0.2s; }
.field-match__bead[data-on] { background: light-dark(#42454c, #e5e6e9); box-shadow: none; transform: scale(1.1); }
.field-match__bead[data-on][data-link]::after { transform: scaleX(1); }
.field-match__bead[data-bad] { box-shadow: inset 0 0 0 1.5px light-dark(#373a40, #f1f2f3); animation: field-match-shake 0.25s 2; }
.field-match__status { margin-left: auto; color: light-dark(#5f626a, #a7abb2); font-size: 11.5px; white-space: nowrap; }
.field-match[data-done] .field-match__status { color: light-dark(#373a40, #f1f2f3); }
.field-match[data-done] .field-match__bead { background: light-dark(#373a40, #f1f2f3); }
@media (prefers-reduced-motion: reduce) {
.field-match__bead { animation: none !important; transition: none; }
}
`;
export function MatchInput() {
const id = useId();
const [a, setA] = useState('');
const [b, setB] = useState('');
const n = Math.min(a.length, 14);
let same = 0;
while (same < b.length && same < a.length && a[same] === b[same]) same++;
const bad = b.length > same;
const done = a.length > 0 && a === b;
const status = !a ? '' : done ? 'Passwords match' : bad ? 'Doesn’t match yet' : b ? `${same} of ${a.length}` : '';
return (
<>
<style>
{css}
</style>
<div className="field-match" data-done={done || undefined}>
<label className="field-match__label" htmlFor={`${id}-a`}>
New password
</label>
<input id={`${id}-a`} className="field-match__input" type="password" value={a} placeholder="At least 8 characters" autoComplete="new-password" onChange={(e) => setA(e.target.value)} />
<div className="field-match__chain">
{Array.from({ length: n }, (_, i) => (
<span
key={i}
className="field-match__bead"
aria-hidden="true"
data-on={i < same || undefined}
data-link={(i < same - 1 || (done && i < n - 1)) || undefined}
data-bad={(bad && i === same) || undefined}
/>
))}
<span id={`${id}-s`} className="field-match__status" aria-live="polite">
{status}
</span>
</div>
<label className="field-match__label" htmlFor={`${id}-b`}>
Confirm password
</label>
<input
id={`${id}-b`}
className="field-match__input"
type="password"
value={b}
aria-describedby={`${id}-s`}
aria-invalid={bad}
autoComplete="new-password"
onChange={(e) => setB(e.target.value)}
/>
</div>
</>
);
}Markup where the component goes, CSS in your stylesheet, script before </body>. In Vue, Svelte or Angular, call initFieldMatch(el) on the mounted element instead.
<div class="field-match">
<label class="field-match__label" for="field-match-a">New password</label>
<input id="field-match-a" class="field-match__input" type="password" placeholder="At least 8 characters" autoComplete="new-password" value=""/>
<div class="field-match__chain">
<span id="field-match-s" class="field-match__status" aria-live="polite"></span>
</div>
<label class="field-match__label" for="field-match-b">Confirm password</label>
<input id="field-match-b" class="field-match__input" type="password" aria-describedby="field-match-s" aria-invalid="false" autoComplete="new-password" value=""/>
</div>@keyframes field-match-shake { 25% { transform: translateX(-2px); } 75% { transform: translateX(2px); } }
.field-match { display: grid; width: 250px; }
.field-match__label { display: block; margin-bottom: 6px; color: light-dark(#50535b, #c6c8cd); font-size: 12px; }
.field-match__input { width: 100%; height: 40px; padding: 0 12px; border: 0; border-radius: 10px; outline: 0; background: light-dark(#f3f4f7, #1a1c1f); box-shadow: inset 0 0 0 1px light-dark(#dbdde1, #2e3035); color: light-dark(#373a40, #f1f2f3); font: 500 14px/1 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 0.2em; transition: box-shadow 0.2s; }
.field-match__input:focus { box-shadow: inset 0 0 0 1px light-dark(#5f626a, #a7abb2), 0 0 0 3px color-mix(in oklab, light-dark(#373a40, #f1f2f3) 10%, transparent); }
.field-match__input::placeholder { color: light-dark(#6d7179, #7d8087); letter-spacing: 0; font-family: system-ui, sans-serif; }
/* The beads between the fields: one per character of the password, linked once the confirmation matches it. */
.field-match__chain { position: relative; display: flex; align-items: center; gap: 6px; height: 26px; padding: 0 14px; }
.field-match__bead { position: relative; width: 7px; height: 7px; border-radius: 50%; box-shadow: inset 0 0 0 1.2px light-dark(#6d7179, #7d8087); transition: background-color 0.2s, box-shadow 0.2s, transform 0.3s cubic-bezier(0.3, 1.8, 0.5, 1); }
.field-match__bead::after { content: ''; position: absolute; left: 7px; top: 3px; width: 6px; height: 1.2px; background: light-dark(#42454c, #e5e6e9); transform: scaleX(0); transform-origin: 0 50%; transition: transform 0.2s; }
.field-match__bead[data-on] { background: light-dark(#42454c, #e5e6e9); box-shadow: none; transform: scale(1.1); }
.field-match__bead[data-on][data-link]::after { transform: scaleX(1); }
.field-match__bead[data-bad] { box-shadow: inset 0 0 0 1.5px light-dark(#373a40, #f1f2f3); animation: field-match-shake 0.25s 2; }
.field-match__status { margin-left: auto; color: light-dark(#5f626a, #a7abb2); font-size: 11.5px; white-space: nowrap; }
.field-match[data-done] .field-match__status { color: light-dark(#373a40, #f1f2f3); }
.field-match[data-done] .field-match__bead { background: light-dark(#373a40, #f1f2f3); }
@media (prefers-reduced-motion: reduce) {
.field-match__bead { animation: none !important; transition: none; }
}function initFieldMatch(root) {
var inputs = root.querySelectorAll('.field-match__input');
var a = inputs[0];
var b = inputs[1];
var chain = root.querySelector('.field-match__chain');
var status = root.querySelector('.field-match__status');
function update() {
var n = Math.min(a.value.length, 14);
var same = 0;
while (same < b.value.length && same < a.value.length && a.value[same] === b.value[same]) same++;
var bad = b.value.length > same;
var done = a.value.length > 0 && a.value === b.value;
// One bead per character of the password, added or removed at the end.
var beads = chain.querySelectorAll('.field-match__bead');
for (var k = beads.length; k < n; k++) {
var bead = document.createElement('span');
bead.className = 'field-match__bead';
bead.setAttribute('aria-hidden', 'true');
chain.insertBefore(bead, status);
}
for (var j = beads.length - 1; j >= n; j--) beads[j].remove();
chain.querySelectorAll('.field-match__bead').forEach(function (el, i) {
el.toggleAttribute('data-on', i < same);
el.toggleAttribute('data-link', i < same - 1 || (done && i < n - 1));
el.toggleAttribute('data-bad', bad && i === same);
});
root.toggleAttribute('data-done', done);
b.setAttribute('aria-invalid', String(bad));
status.textContent = !a.value ? '' : done ? 'Passwords match' : bad ? 'Doesn’t match yet' : b.value ? same + ' of ' + a.value.length : '';
}
root.addEventListener('input', update);
return function () { root.removeEventListener('input', update); };
}
document.querySelectorAll('.field-match').forEach(initFieldMatch);Goal: Create a "Match" input.
Visual behavior: A confirm password pair: beads between the two fields light and link up as each character of the confirmation matches, shake at the first wrong one, and close into a chain when they all match.
Technique:
- One bead per character of the new password (up to fourteen); the beads up to the matching prefix light with data-on and link to the next with a ::after that scales across.
- The first wrong character marks its bead data-bad, which shakes twice; when both match the whole chain links and lights.
- A status line counts the match or says it does not match yet, and the confirmation gets aria-invalid.
Accessibility: Two real password inputs with visible labels and new-password autocomplete; the status is aria-live and describes the confirmation, which carries aria-invalid while it differs. The beads are aria-hidden. Under prefers-reduced-motion they light without shaking or easing.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: neutral only. Black, white and grays, no hues. Use the color values above as written, or my project's closest neutral tokens.
Return a single self-contained component. No dependencies.Goal: Create a "Match" input.
Visual behavior: A confirm password pair: beads between the two fields light and link up as each character of the confirmation matches, shake at the first wrong one, and close into a chain when they all match.
Technique:
- One bead per character of the new password (up to fourteen); the beads up to the matching prefix light with data-on and link to the next with a ::after that scales across.
- The first wrong character marks its bead data-bad, which shakes twice; when both match the whole chain links and lights.
- A status line counts the match or says it does not match yet, and the confirmation gets aria-invalid.
Accessibility: Two real password inputs with visible labels and new-password autocomplete; the status is aria-live and describes the confirmation, which carries aria-invalid while it differs. The beads are aria-hidden. Under prefers-reduced-motion they light without shaking or easing.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: use my project's existing color tokens. The color values above are grayscale placeholders. Map each one by its role (surface, text, border, accent, glow) to my theme, and keep their relative lightness so contrast and the effect still read.
Return a single self-contained component. No dependencies.Paste into Claude, ChatGPT or Cursor after filling in {{USER_STACK}}, {{USER_STYLING}} and {{USER_CONSTRAINTS}}. Neutral keeps the greys you see here; Match my project asks the model to map them onto your own colour tokens.
Free to use in any project, commercial included. MIT-0, no attribution required. Licence
About this component
A confirm password pair: beads between the two fields light and link up as each character of the confirmation matches, shake at the first wrong one, and close into a chain when they all match.
- Dependencies
- None
- Difficulty
- Intermediate
- Triggers
- focus, state change, delay stagger
- Techniques
- css transition, keyframes, pseudo elements, state machine
- Reduced motion
- Respected
- Theme
- Light and dark (follows your page's color-scheme)
Two real password inputs with visible labels and new-password autocomplete; the status is aria-live and describes the confirmation, which carries aria-invalid while it differs. The beads are aria-hidden. Under prefers-reduced-motion they light without shaking or easing.