Cipher
import { useEffect, useRef, useState } from 'react';
const css = `
.field-cipher { width: 250px; }
.field-cipher__label { display: block; margin-bottom: 6px; color: light-dark(#50535b, #c6c8cd); font-size: 12px; }
.field-cipher__box {
position: relative;
display: flex;
align-items: center;
height: 42px;
border-radius: 10px;
background: light-dark(#f3f4f7, #1a1c1f);
box-shadow: inset 0 0 0 1px light-dark(#dbdde1, #2e3035);
transition: box-shadow 0.2s;
}
.field-cipher__box:focus-within { 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-cipher__input, .field-cipher__mirror { padding: 0 12px; font: 500 15px/1 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 2px; }
.field-cipher__input { flex: 1; min-width: 0; height: 100%; border: 0; outline: 0; background: none; color: transparent; caret-color: light-dark(#373a40, #f1f2f3); }
.field-cipher__input::placeholder { color: light-dark(#5f626a, #a7abb2); letter-spacing: 0; font-family: system-ui, sans-serif; font-size: 14px; }
.field-cipher__mirror {
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 62px;
display: flex;
align-items: center;
overflow: hidden;
color: light-dark(#373a40, #f1f2f3);
white-space: pre;
pointer-events: none;
}
.field-cipher__ch[data-busy] { color: light-dark(#50535b, #c6c8cd); }
.field-cipher__toggle {
margin-right: 6px;
padding: 6px 9px;
border: 0;
border-radius: 7px;
background: light-dark(#ebedf0, #232529);
color: light-dark(#42454c, #e5e6e9);
font-family: inherit; font-size: 11px; font-weight: 600;
cursor: pointer;
}
.field-cipher__toggle:focus-visible { outline: 2px solid light-dark(#5f626a, #a7abb2); outline-offset: 2px; }
`;
const GLYPHS = '#%&@$*+=?/<>{}[]';
const DUR = 360;
const glyph = () => GLYPHS[Math.floor(Math.random() * GLYPHS.length)];
export function CipherInput() {
const [value, setValue] = useState('');
const [reveal, setReveal] = useState(false);
// When each character last started decoding; it scrambles for DUR ms from then.
const born = useRef<number[]>([]);
const [, setFrame] = useState(0);
useEffect(() => {
const still = matchMedia('(prefers-reduced-motion: reduce)').matches;
const id = window.setInterval(() => {
const now = performance.now();
if (!still && born.current.some((t) => now - t < DUR)) setFrame((f) => f + 1);
}, 45);
return () => clearInterval(id);
}, []);
const change = (next: string) => {
// Keep the timings of the characters that survived the edit; new ones start scrambling now.
let p = 0;
while (p < value.length && p < next.length && value[p] === next[p]) p++;
let s = 0;
while (s < value.length - p && s < next.length - p && value[value.length - 1 - s] === next[next.length - 1 - s]) s++;
const now = performance.now();
born.current = [...born.current.slice(0, p), ...Array(next.length - p - s).fill(now), ...born.current.slice(born.current.length - s)];
setValue(next);
};
const flip = () => {
const now = performance.now();
born.current = born.current.map((_, i) => now + i * 30);
setReveal(!reveal);
};
const now = performance.now();
return (
<>
<style>
{css}
</style>
<div className="field-cipher">
<label className="field-cipher__label" htmlFor="field-cipher-input">
Password
</label>
<div className="field-cipher__box">
<input
id="field-cipher-input"
className="field-cipher__input"
type={reveal ? 'text' : 'password'}
value={value}
placeholder="Type a password"
autoComplete="new-password"
spellCheck={false}
onChange={(e) => change(e.target.value)}
/>
<span className="field-cipher__mirror" aria-hidden="true">
{[...value].map((c, i) => {
const age = now - (born.current[i] ?? 0);
const busy = age >= 0 && age < DUR;
return (
<span key={i} className="field-cipher__ch" data-busy={busy || undefined}>
{busy ? glyph() : reveal && age >= DUR ? c : '•'}
</span>
);
})}
</span>
<button type="button" className="field-cipher__toggle" aria-pressed={reveal} onClick={flip}>
{reveal ? 'Hide' : 'Show'}
</button>
</div>
</div>
</>
);
}Markup where the component goes, CSS in your stylesheet, script before </body>. In Vue, Svelte or Angular, call initFieldCipher(el) on the mounted element instead.
<div class="field-cipher">
<label class="field-cipher__label" for="field-cipher-input">Password</label>
<div class="field-cipher__box">
<input id="field-cipher-input" class="field-cipher__input" type="password" placeholder="Type a password" autoComplete="new-password" spellCheck="false" value=""/>
<span class="field-cipher__mirror" aria-hidden="true"></span>
<button type="button" class="field-cipher__toggle" aria-pressed="false">Show</button>
</div>
</div>.field-cipher { width: 250px; }
.field-cipher__label { display: block; margin-bottom: 6px; color: light-dark(#50535b, #c6c8cd); font-size: 12px; }
.field-cipher__box {
position: relative;
display: flex;
align-items: center;
height: 42px;
border-radius: 10px;
background: light-dark(#f3f4f7, #1a1c1f);
box-shadow: inset 0 0 0 1px light-dark(#dbdde1, #2e3035);
transition: box-shadow 0.2s;
}
.field-cipher__box:focus-within { 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-cipher__input, .field-cipher__mirror { padding: 0 12px; font: 500 15px/1 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 2px; }
.field-cipher__input { flex: 1; min-width: 0; height: 100%; border: 0; outline: 0; background: none; color: transparent; caret-color: light-dark(#373a40, #f1f2f3); }
.field-cipher__input::placeholder { color: light-dark(#5f626a, #a7abb2); letter-spacing: 0; font-family: system-ui, sans-serif; font-size: 14px; }
.field-cipher__mirror {
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 62px;
display: flex;
align-items: center;
overflow: hidden;
color: light-dark(#373a40, #f1f2f3);
white-space: pre;
pointer-events: none;
}
.field-cipher__ch[data-busy] { color: light-dark(#50535b, #c6c8cd); }
.field-cipher__toggle {
margin-right: 6px;
padding: 6px 9px;
border: 0;
border-radius: 7px;
background: light-dark(#ebedf0, #232529);
color: light-dark(#42454c, #e5e6e9);
font-family: inherit; font-size: 11px; font-weight: 600;
cursor: pointer;
}
.field-cipher__toggle:focus-visible { outline: 2px solid light-dark(#5f626a, #a7abb2); outline-offset: 2px; }function initFieldCipher(root) {
var input = root.querySelector('.field-cipher__input');
var mirror = root.querySelector('.field-cipher__mirror');
var toggle = root.querySelector('.field-cipher__toggle');
if (!input || !mirror || !toggle) return;
var GLYPHS = '#%&@$*+=?/<>{}[]';
var DUR = 360;
var still = matchMedia('(prefers-reduced-motion: reduce)').matches;
var value = input.value;
var reveal = false;
// When each character last started decoding; it scrambles for DUR ms from then.
var born = value.split('').map(function () { return -Infinity; });
function render() {
var now = performance.now();
var busyAny = false;
mirror.textContent = '';
value.split('').forEach(function (c, i) {
var age = now - born[i];
var busy = !still && age >= 0 && age < DUR;
if (busy) busyAny = true;
var s = document.createElement('span');
s.className = 'field-cipher__ch';
if (busy) s.setAttribute('data-busy', '');
s.textContent = busy ? GLYPHS[Math.floor(Math.random() * GLYPHS.length)] : reveal && age >= DUR ? c : '•';
mirror.appendChild(s);
});
return busyAny;
}
function change() {
var next = input.value;
// Keep the timings of the characters that survived the edit; new ones start scrambling now.
var p = 0;
while (p < value.length && p < next.length && value[p] === next[p]) p++;
var s = 0;
while (s < value.length - p && s < next.length - p && value[value.length - 1 - s] === next[next.length - 1 - s]) s++;
var now = performance.now();
var fresh = [];
for (var i = 0; i < next.length - p - s; i++) fresh.push(now);
born = born.slice(0, p).concat(fresh, born.slice(born.length - s));
value = next;
render();
}
function flip() {
var now = performance.now();
born = born.map(function (_, i) { return now + i * 30; });
reveal = !reveal;
input.type = reveal ? 'text' : 'password';
toggle.setAttribute('aria-pressed', String(reveal));
toggle.textContent = reveal ? 'Hide' : 'Show';
render();
}
var timer = setInterval(render, 45);
input.addEventListener('input', change);
toggle.addEventListener('click', flip);
render();
return function () {
clearInterval(timer);
input.removeEventListener('input', change);
toggle.removeEventListener('click', flip);
};
}
document.querySelectorAll('.field-cipher').forEach(initFieldCipher);Goal: Create a "Cipher" input.
Visual behavior: A password field where every character scrambles through glyphs for a moment before it settles into a dot. Show decodes them all in a wave into the real characters; Hide scrambles them back.
Technique:
- The real input keeps transparent text and a visible caret; an aria-hidden mirror with the same font, size and padding (and font-kerning: none) draws the characters on top, so the caret always lines up.
- Each character has the time it last changed; for 360ms after that it shows a random glyph, then a dot (or the real character when revealed). Edits keep the times of untouched characters by diffing the common prefix and suffix.
- Show and Hide restart every character's clock with a 30ms stagger and switch the input between password and text.
Accessibility: A real <input> with a visible <label>; decoration is aria-hidden. Show/Hide is a real button with aria-pressed. Under prefers-reduced-motion characters settle without scrambling.
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 "Cipher" input.
Visual behavior: A password field where every character scrambles through glyphs for a moment before it settles into a dot. Show decodes them all in a wave into the real characters; Hide scrambles them back.
Technique:
- The real input keeps transparent text and a visible caret; an aria-hidden mirror with the same font, size and padding (and font-kerning: none) draws the characters on top, so the caret always lines up.
- Each character has the time it last changed; for 360ms after that it shows a random glyph, then a dot (or the real character when revealed). Edits keep the times of untouched characters by diffing the common prefix and suffix.
- Show and Hide restart every character's clock with a 30ms stagger and switch the input between password and text.
Accessibility: A real <input> with a visible <label>; decoration is aria-hidden. Show/Hide is a real button with aria-pressed. Under prefers-reduced-motion characters settle without scrambling.
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 password field where every character scrambles through glyphs for a moment before it settles into a dot. Show decodes them all in a wave into the real characters; Hide scrambles them back.
- Dependencies
- None
- Difficulty
- Intermediate
- Triggers
- focus, state change, morph
- Techniques
- text animation, interval, state machine
- Reduced motion
- Respected
- Theme
- Light and dark (follows your page's color-scheme)
A real <input> with a visible <label>; decoration is aria-hidden. Show/Hide is a real button with aria-pressed. Under prefers-reduced-motion characters settle without scrambling.