Copy field
Read-only fields for install commands and secret keys: one-click copy with a morphing check, and a reveal toggle that unscrambles hidden keys.
"use client";
import { useEffect, useId, useRef, useState } from "react";
import "./copy-field.css";
type CopyFieldProps = {
label: string;
value: string;
/** Hide the value behind dots with a reveal button (for API keys). */
secret?: boolean;
/** Show a "$" prompt in front (for terminal commands). */
command?: boolean;
theme?: "light" | "dark";
};
const GLYPHS = "ABCDEFGHJKLMNPQRSTUVWXYZ0123456789";
async function copy(text: string) {
try {
await navigator.clipboard.writeText(text);
return true;
} catch {
return false;
}
}
/**
* CopyField
* - Copy button: the copy icon morphs into a drawn check mark for 1.6s.
* - Secret mode: shows dots; "Reveal" unscrambles the real key from random letters.
*/
export function CopyField({
label,
value,
secret = false,
command = false,
theme = "light",
}: CopyFieldProps) {
const id = useId();
const [copied, setCopied] = useState(false);
const [shown, setShown] = useState(!secret);
const [text, setText] = useState(
secret ? "•".repeat(Math.min(value.length, 32)) : value,
);
const raf = useRef(0);
// Unscramble: each character settles on its real letter a little later than the one before.
useEffect(() => {
cancelAnimationFrame(raf.current);
// Instant cases still go through a frame, so state is never set synchronously in the effect.
if (!shown || matchMedia("(prefers-reduced-motion: reduce)").matches) {
const instant = shown ? value : "•".repeat(Math.min(value.length, 32));
raf.current = requestAnimationFrame(() => setText(instant));
return () => cancelAnimationFrame(raf.current);
}
const start = performance.now();
const tick = (now: number) => {
const settled = Math.floor((now - start) / 18);
setText(
value
.split("")
.map((c, i) =>
i < settled || c === "_" || c === "-"
? c
: GLYPHS[(Math.random() * GLYPHS.length) | 0],
)
.join(""),
);
if (settled < value.length) raf.current = requestAnimationFrame(tick);
};
raf.current = requestAnimationFrame(tick);
return () => cancelAnimationFrame(raf.current);
}, [shown, value]);
return (
<div className="gu-copy" data-theme={theme}>
<label htmlFor={id} className="gu-copy__label">
{label}
</label>
<div className="gu-copy__field">
{command && (
<span className="gu-copy__prompt" aria-hidden="true">
$
</span>
)}
<input
id={id}
readOnly
value={text}
aria-label={shown ? undefined : `${label} (hidden)`}
spellCheck={false}
onFocus={(e) => shown && e.currentTarget.select()}
/>
{secret && (
<button
type="button"
className="gu-copy__btn gu-copy__btn--ghost"
aria-pressed={shown}
onClick={() => setShown((s) => !s)}
>
{shown ? "Hide" : "Reveal"}
</button>
)}
<button
type="button"
className="gu-copy__btn"
data-copied={copied || undefined}
aria-label={copied ? "Copied" : `Copy ${label}`}
onClick={async () => {
if (await copy(value)) {
setCopied(true);
setTimeout(() => setCopied(false), 1600);
}
}}
>
<svg viewBox="0 0 20 20" aria-hidden="true">
<path className="gu-copy__sheet" d="M7 7h9v9H7zM4 13V4h9" />
<path
className="gu-copy__tick"
d="m4.5 10.5 3.5 3.5 7.5-8"
pathLength="1"
/>
</svg>
</button>
</div>
<p className="gu-copy__sr" role="status">
{copied ? `${label} copied` : ""}
</p>
</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: Copy field
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
Read-only "copy fields" for an install command and for a secret API key. The copy button's icon morphs into a check that draws itself. The secret field hides the key behind dots, and "Reveal" unscrambles it from random letters into the real value.
## Layout and look
- Label above (14px, weight 700). Field: 1.5px border, radius 14px, background #fbf8f2 (dark #18181b), monospace 15px value, padding 6px 6px 6px 16px; focus-within turns the border blue (#2b35ff, dark #d7ff3a).
- Command variant: an orange "$" prompt before the text.
- Buttons inside the field, 44px tall, radius 10px: copy = dark square (#0e0e10, inverted in dark), reveal = ghost with an inset border.
- Copied: button turns lime #d7ff3a with a dark check.
## Behavior and motion
1. Copy uses navigator.clipboard.writeText. On success the copy sheet icon fades and a check path (pathLength 1, dash 1) draws from dashoffset 1 to 0 in 350ms; resets after 1.6s.
2. Reveal: every frame, characters before index floor(elapsed / 18ms) show the real letter, the rest show random A-Z/0-9 (dashes and underscores stay fixed). Hide switches back to dots.
3. Focusing a revealed field selects all text.
## Touch
44px buttons; long values scroll inside the input.
## Accessibility
- Real <label> + readonly <input>; the hidden key's input gets an aria-label noting it is hidden.
- Reveal uses aria-pressed; a role="status" region announces "copied".
- prefers-reduced-motion: no scramble, no drawing.
## Props
label, value, secret, command, theme.
## 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.
### CopyField.tsx
```tsx
"use client";
import { useEffect, useId, useRef, useState } from "react";
import "./copy-field.css";
type CopyFieldProps = {
label: string;
value: string;
/** Hide the value behind dots with a reveal button (for API keys). */
secret?: boolean;
/** Show a "$" prompt in front (for terminal commands). */
command?: boolean;
theme?: "light" | "dark";
};
const GLYPHS = "ABCDEFGHJKLMNPQRSTUVWXYZ0123456789";
async function copy(text: string) {
try {
await navigator.clipboard.writeText(text);
return true;
} catch {
return false;
}
}
/**
* CopyField
* - Copy button: the copy icon morphs into a drawn check mark for 1.6s.
* - Secret mode: shows dots; "Reveal" unscrambles the real key from random letters.
*/
export function CopyField({
label,
value,
secret = false,
command = false,
theme = "light",
}: CopyFieldProps) {
const id = useId();
const [copied, setCopied] = useState(false);
const [shown, setShown] = useState(!secret);
const [text, setText] = useState(
secret ? "•".repeat(Math.min(value.length, 32)) : value,
);
const raf = useRef(0);
// Unscramble: each character settles on its real letter a little later than the one before.
useEffect(() => {
cancelAnimationFrame(raf.current);
// Instant cases still go through a frame, so state is never set synchronously in the effect.
if (!shown || matchMedia("(prefers-reduced-motion: reduce)").matches) {
const instant = shown ? value : "•".repeat(Math.min(value.length, 32));
raf.current = requestAnimationFrame(() => setText(instant));
return () => cancelAnimationFrame(raf.current);
}
const start = performance.now();
const tick = (now: number) => {
const settled = Math.floor((now - start) / 18);
setText(
value
.split("")
.map((c, i) =>
i < settled || c === "_" || c === "-"
? c
: GLYPHS[(Math.random() * GLYPHS.length) | 0],
)
.join(""),
);
if (settled < value.length) raf.current = requestAnimationFrame(tick);
};
raf.current = requestAnimationFrame(tick);
return () => cancelAnimationFrame(raf.current);
}, [shown, value]);
return (
<div className="gu-copy" data-theme={theme}>
<label htmlFor={id} className="gu-copy__label">
{label}
</label>
<div className="gu-copy__field">
{command && (
<span className="gu-copy__prompt" aria-hidden="true">
$
</span>
)}
<input
id={id}
readOnly
value={text}
aria-label={shown ? undefined : `${label} (hidden)`}
spellCheck={false}
onFocus={(e) => shown && e.currentTarget.select()}
/>
{secret && (
<button
type="button"
className="gu-copy__btn gu-copy__btn--ghost"
aria-pressed={shown}
onClick={() => setShown((s) => !s)}
>
{shown ? "Hide" : "Reveal"}
</button>
)}
<button
type="button"
className="gu-copy__btn"
data-copied={copied || undefined}
aria-label={copied ? "Copied" : `Copy ${label}`}
onClick={async () => {
if (await copy(value)) {
setCopied(true);
setTimeout(() => setCopied(false), 1600);
}
}}
>
<svg viewBox="0 0 20 20" aria-hidden="true">
<path className="gu-copy__sheet" d="M7 7h9v9H7zM4 13V4h9" />
<path
className="gu-copy__tick"
d="m4.5 10.5 3.5 3.5 7.5-8"
pathLength="1"
/>
</svg>
</button>
</div>
<p className="gu-copy__sr" role="status">
{copied ? `${label} copied` : ""}
</p>
</div>
);
}
```
### copy-field.css
```css
/* CopyField: change these colors to match your brand. */
.gu-copy {
--field: #fbf8f2;
--text: #0e0e10;
--muted: #57534b;
--line: rgb(14 14 16 / 0.16);
--btn: #0e0e10;
--btn-text: #f3eee3;
--ok: #d7ff3a;
--ok-text: #0e0e10;
--accent: #ff5a1f;
--focus: #2b35ff;
display: grid;
gap: 8px;
width: min(100%, 520px);
font-family: system-ui, sans-serif;
color: var(--text);
}
.gu-copy[data-theme="dark"] {
--field: #18181b;
--text: #f3eee3;
--muted: #aea89c;
--line: rgb(243 238 227 / 0.16);
--btn: #f3eee3;
--btn-text: #0e0e10;
--focus: #d7ff3a;
}
.gu-copy__label {
font-size: 0.85rem;
font-weight: 700;
}
.gu-copy__field {
display: flex;
align-items: center;
gap: 6px;
padding: 6px 6px 6px 16px;
border: 1.5px solid var(--line);
border-radius: 14px;
background: var(--field);
transition: border-color 0.2s;
}
.gu-copy__field:focus-within {
border-color: var(--focus);
}
.gu-copy__prompt {
color: var(--accent);
font:
700 0.95rem ui-monospace,
"JetBrains Mono",
monospace;
}
.gu-copy input {
flex: 1;
/* width 0 stops the input's default width from widening the field on small screens */
width: 0;
min-width: 0;
padding: 10px 0;
border: 0;
outline: 0;
background: none;
color: inherit;
font:
500 0.95rem ui-monospace,
"JetBrains Mono",
monospace;
letter-spacing: 0.02em;
}
.gu-copy__btn {
display: grid;
flex: none;
place-items: center;
min-width: 44px;
height: 44px;
padding: 0 12px;
border: 0;
border-radius: 10px;
background: var(--btn);
color: var(--btn-text);
font:
700 0.85rem system-ui,
sans-serif;
cursor: pointer;
transition:
background-color 0.25s,
color 0.25s,
transform 0.15s;
}
.gu-copy__btn:active {
transform: scale(0.94);
}
.gu-copy__btn:focus-visible {
outline: 2px solid var(--focus);
outline-offset: 2px;
}
.gu-copy__btn--ghost {
background: transparent;
color: var(--muted);
box-shadow: inset 0 0 0 1.5px var(--line);
}
.gu-copy__btn svg {
width: 20px;
height: 20px;
fill: none;
stroke: currentColor;
stroke-width: 1.9;
stroke-linecap: round;
stroke-linejoin: round;
}
/* Copy icon -> check: the sheet fades, the tick draws itself */
.gu-copy__sheet {
transition: opacity 0.2s;
}
.gu-copy__tick {
stroke-dasharray: 1;
stroke-dashoffset: 1;
transition: stroke-dashoffset 0.35s cubic-bezier(0.16, 1, 0.3, 1) 0.1s;
}
.gu-copy__btn[data-copied] {
background: var(--ok);
color: var(--ok-text);
}
.gu-copy__btn[data-copied] .gu-copy__sheet {
opacity: 0;
}
.gu-copy__btn[data-copied] .gu-copy__tick {
stroke-dashoffset: 0;
}
.gu-copy__sr {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
}
@media (prefers-reduced-motion: reduce) {
.gu-copy__tick,
.gu-copy__sheet,
.gu-copy__btn {
transition: none;
}
}
```
You might also want
Next piece: Cursor trailModal dialog
Components, intermediate
Newsletter signup
Components, beginner
Password strength meter
Components, beginner