Copy Field
Ways to copy a value: a field, a labelled button, an install command per package manager, and a hidden secret.
Field
A read-only value with a copy button that confirms in place and announces the result.
$npx shadcn@latest add https://stillui.netlify.app/r/copy-field.jsonhttps://stillui.dev/invite/7f3k-29qaInstallation
$npx shadcn@latest add @stillui/copy-fieldOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/copy-field
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { useEffect, useRef, useState } from "react";
import { cn } from "@/lib/utils";
const SWAP = { duration: 0.14, ease: [0.16, 1, 0.3, 1] } as const;
type CopyFieldProps = {
value: string;
/** Shown before the value, e.g. "$" for a shell command. Not copied. */
prefix?: string;
/** Accessible name for the copy button. */
label?: string;
className?: string;
onCopy?: (value: string) => void;
};
export function CopyField({ value, prefix, label = "Copy", className, onCopy }: CopyFieldProps) {
const reduce = useReducedMotion();
const [state, setState] = useState<"idle" | "copied" | "failed">("idle");
const timer = useRef<number | undefined>(undefined);
useEffect(() => () => window.clearTimeout(timer.current), []);
const copy = async () => {
try {
await navigator.clipboard.writeText(value);
setState("copied");
onCopy?.(value);
} catch {
setState("failed");
}
window.clearTimeout(timer.current);
timer.current = window.setTimeout(() => setState("idle"), 1600);
};
const icon = { initial: { opacity: 0, scale: reduce ? 1 : 0.6, filter: reduce ? "none" : "blur(2px)" }, animate: { opacity: 1, scale: 1, filter: "blur(0px)" }, exit: { opacity: 0, scale: reduce ? 1 : 0.6, filter: reduce ? "none" : "blur(2px)" } };
return (
<div className={cn("flex h-10 w-full min-w-0 items-center rounded-lg bg-muted pl-3.5 pr-1", className)}>
<code className="min-w-0 flex-1 truncate font-mono text-[13px]">
{prefix ? <span className="mr-2 select-none text-faint-foreground">{prefix}</span> : null}
{value}
</code>
<button
type="button"
onClick={copy}
aria-label={state === "copied" ? "Copied" : label}
className="relative grid size-8 shrink-0 place-items-center rounded-md text-muted-foreground transition-[color,background-color,box-shadow,scale] active:scale-[0.92] hover:bg-background hover:text-foreground"
>
<AnimatePresence initial={false} mode="popLayout">
<motion.svg
key={state}
viewBox="0 0 16 16"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
className={cn("size-4", state === "copied" && "text-success", state === "failed" && "text-destructive")}
aria-hidden
{...icon}
transition={SWAP}
>
{state === "copied" ? (
<motion.path
d="M3.5 8.5l3 3 6-7"
initial={{ pathLength: reduce ? 1 : 0 }}
animate={{ pathLength: 1 }}
transition={{ duration: 0.22, ease: [0.16, 1, 0.3, 1] }}
/>
) : state === "failed" ? (
<path d="M4.5 4.5l7 7m0-7l-7 7" />
) : (
<>
<rect x="5.5" y="5.5" width="8" height="8" rx="1.75" />
<path d="M10.5 3.25V3.5a1 1 0 0 0-1-1h-5.5a1.5 1.5 0 0 0-1.5 1.5v5.5a1 1 0 0 0 1 1h.25" />
</>
)}
</motion.svg>
</AnimatePresence>
</button>
<span role="status" aria-live="polite" className="sr-only">
{state === "copied" ? "Copied to clipboard" : state === "failed" ? "Copy failed" : ""}
</span>
</div>
);
}States
- Idle
- Long values truncate; the full value is still copied.
- Copied
- Icon swaps to a drawn check for 1.6s. Screen readers hear “Copied to clipboard”.
- Failed
- Clipboard blocked: the icon shows a cross and the failure is announced.
Command
An install command with a switch for the package manager. Copies whichever command is showing.
$npm install motionInstallation
$npx shadcn@latest add @stillui/copy-commandOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/copy-field#command
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { useEffect, useId, useRef, useState } from "react";
import { cn } from "@/lib/utils";
const GLIDE = { type: "spring", stiffness: 500, damping: 40 } as const;
type CopyCommandProps = {
/** One command per tool, e.g. { npm: "npm i motion", pnpm: "pnpm add motion" }. */
commands: Record<string, string>;
/** The package manager showing. Leave it out and the component keeps its own. */
tool?: string;
onToolChange?: (tool: string) => void;
className?: string;
};
/** An install command with a switch for the package manager. Copies whichever one is showing. */
export function CopyCommand({ commands, tool: controlled, onToolChange, className }: CopyCommandProps) {
const reduce = useReducedMotion();
const tools = Object.keys(commands);
const [internal, setInternal] = useState(tools[0]);
const tool = controlled && commands[controlled] ? controlled : internal;
const setTool = (next: string) => {
if (controlled === undefined) setInternal(next);
onToolChange?.(next);
};
const [copied, setCopied] = useState(false);
const timer = useRef<number | undefined>(undefined);
const indicatorId = useId();
useEffect(() => () => window.clearTimeout(timer.current), []);
const copy = async () => {
try {
await navigator.clipboard.writeText(commands[tool]);
setCopied(true);
window.clearTimeout(timer.current);
timer.current = window.setTimeout(() => setCopied(false), 1600);
} catch {}
};
return (
<div className={cn("w-full overflow-hidden rounded-xl bg-muted", className)}>
<div className="flex items-center justify-between pl-2 pr-1 pt-1">
<div role="group" aria-label="Package manager" className="flex">
{tools.map((name) => (
<button
key={name}
type="button"
aria-pressed={tool === name}
onClick={() => setTool(name)}
className={cn(
"relative px-2.5 py-2 font-mono text-[12px] transition-[color,background-color,box-shadow,scale] active:scale-[0.96]",
tool === name ? "text-foreground" : "text-muted-foreground hover:text-foreground",
)}
>
{name}
{tool === name ? (
<motion.span
layoutId={indicatorId}
className="absolute inset-x-2 bottom-0 h-0.5 rounded-full bg-foreground"
transition={reduce ? { duration: 0 } : GLIDE}
/>
) : null}
</button>
))}
</div>
<button
type="button"
onClick={copy}
aria-label={copied ? "Copied" : `Copy ${tool} command`}
className="grid size-8 place-items-center rounded-md text-muted-foreground transition-[color,background-color,box-shadow,scale] active:scale-[0.92] hover:bg-background hover:text-foreground"
>
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" className={cn("size-4", copied && "text-success")} aria-hidden>
{copied ? (
<path d="M3.5 8.5l3 3 6-7" />
) : (
<>
<rect x="5.5" y="5.5" width="8" height="8" rx="1.75" />
<path d="M10.5 3.25V3.5a1 1 0 0 0-1-1h-5.5a1.5 1.5 0 0 0-1.5 1.5v5.5a1 1 0 0 0 1 1h.25" />
</>
)}
</svg>
</button>
</div>
<div className="relative h-11 overflow-hidden px-4">
<AnimatePresence mode="popLayout" initial={false}>
<motion.code
key={tool}
className="absolute inset-x-4 top-0 flex h-full items-center truncate font-mono text-[13px]"
initial={{ opacity: 0, y: reduce ? 0 : 6 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: reduce ? 0 : -6 }}
transition={{ duration: reduce ? 0 : 0.16 }}
>
<span className="mr-2 select-none text-faint-foreground">$</span>
{commands[tool]}
</motion.code>
</AnimatePresence>
</div>
<span role="status" aria-live="polite" className="sr-only">
{copied ? "Copied to clipboard" : ""}
</span>
</div>
);
}States
- Switching
- A short underline glides to the chosen tool and the command slides into place.
- Copied
- The icon turns into a green check for 1.6 seconds.
- Keyboard
- The tools are a labelled group of toggle buttons; the copy button names the tool it copies.
- Screen readers
- A copy is announced through a live region.
- Reduced motion
- The underline jumps and the command swaps without sliding.
Notes
Remember the reader’s choice with localStorage if your docs show many commands.
Secret
A hidden value such as an API key, with buttons to reveal it and copy it. Copying works without revealing.
API key
••••••••••••••••••••••••3f9aKeep this private. Anyone with the key can use your account.
Installation
$npx shadcn@latest add @stillui/copy-secretOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/copy-field#secret
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { useEffect, useRef, useState } from "react";
import { cn } from "@/lib/utils";
type CopySecretProps = {
value: string;
/** Characters left visible at the end while hidden, so people can tell keys apart. */
visibleEnd?: number;
label?: string;
className?: string;
};
function mask(value: string, visibleEnd: number) {
return "•".repeat(Math.max(8, Math.min(24, value.length - visibleEnd))) + value.slice(-visibleEnd);
}
/** A hidden value, like an API key, with buttons to reveal it and copy it. Copying never needs revealing. */
export function CopySecret({ value, visibleEnd = 4, label = "Secret", className }: CopySecretProps) {
const reduce = useReducedMotion();
const [revealed, setRevealed] = useState(false);
const [copied, setCopied] = useState(false);
const timer = useRef<number | undefined>(undefined);
useEffect(() => () => window.clearTimeout(timer.current), []);
const copy = async () => {
try {
await navigator.clipboard.writeText(value);
setCopied(true);
window.clearTimeout(timer.current);
timer.current = window.setTimeout(() => setCopied(false), 1600);
} catch {}
};
return (
<div className={cn("flex h-10 w-full min-w-0 items-center rounded-lg bg-muted pl-3.5 pr-1", className)}>
<span className="sr-only">{label}</span>
<code className="relative min-w-0 flex-1 truncate font-mono text-[13px]">
<AnimatePresence mode="popLayout" initial={false}>
<motion.span
key={revealed ? "shown" : "hidden"}
className="block truncate"
initial={{ opacity: 0, filter: reduce ? "none" : "blur(4px)" }}
animate={{ opacity: 1, filter: "blur(0px)" }}
exit={{ opacity: 0, filter: reduce ? "none" : "blur(4px)" }}
transition={{ duration: reduce ? 0 : 0.18 }}
>
{revealed ? value : mask(value, visibleEnd)}
</motion.span>
</AnimatePresence>
</code>
<button
type="button"
onClick={() => setRevealed((r) => !r)}
aria-label={revealed ? `Hide ${label}` : `Show ${label}`}
aria-pressed={revealed}
className="grid size-8 shrink-0 place-items-center rounded-md text-muted-foreground transition-[color,background-color,box-shadow,scale] active:scale-[0.92] hover:bg-background hover:text-foreground"
>
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" className="size-4" aria-hidden>
<path d="M1.75 8S4 3.75 8 3.75 14.25 8 14.25 8 12 12.25 8 12.25 1.75 8 1.75 8z" />
<circle cx="8" cy="8" r="2" />
{/* A slash draws across the eye while the value is visible. */}
<motion.path d="M2.5 2.5l11 11" initial={false} animate={{ pathLength: revealed ? 1 : 0, opacity: revealed ? 1 : 0 }} transition={{ duration: reduce ? 0 : 0.2 }} />
</svg>
</button>
<button
type="button"
onClick={copy}
aria-label={copied ? "Copied" : `Copy ${label}`}
className="grid size-8 shrink-0 place-items-center rounded-md text-muted-foreground transition-[color,background-color,box-shadow,scale] active:scale-[0.92] hover:bg-background hover:text-foreground"
>
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" className={cn("size-4", copied && "text-success")} aria-hidden>
{copied ? (
<path d="M3.5 8.5l3 3 6-7" />
) : (
<>
<rect x="5.5" y="5.5" width="8" height="8" rx="1.75" />
<path d="M10.5 3.25V3.5a1 1 0 0 0-1-1h-5.5a1.5 1.5 0 0 0-1.5 1.5v5.5a1 1 0 0 0 1 1h.25" />
</>
)}
</svg>
</button>
<span role="status" aria-live="polite" className="sr-only">
{copied ? "Copied to clipboard" : ""}
</span>
</div>
);
}States
- Hidden
- Dots, plus the last four characters so people can tell keys apart.
- Revealed
- The value sharpens out of a blur and a slash draws across the eye icon.
- Copied
- The copy icon turns into a green check. The full value is copied even while hidden.
- Keyboard
- The reveal button is a toggle with aria-pressed; both buttons name the secret.
- Reduced motion
- No blur or drawing; the value swaps instantly.
Notes
Hiding on screen protects against people looking over a shoulder, not against anything else. Treat the value as secret on the server too.