Interactions / Interactions
Copy button
Copies a value and says so in place: the copy icon turns into a tick that draws itself, the label rises to Copied, and both settle back. No toast needed.
FreeReact · Vue · HTMLFigma: Buttons
Code
The component itself, ready to drop into your project. Switch between React, Vue and plain HTML; every version uses the same Tailwind classes on Ochre's tokens.
$
npx shadcn@latest add @ochre/modalcomponents/ochre/copy-button.tsx
"use client";
import * as React from "react";
import { cx } from "@/components/ochre/utils";
export type CopyButtonProps = Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "onCopy" | "value"> & {
// What goes on the clipboard, or a function that returns it (it can be async).
value: string | (() => string | Promise<string>);
label?: React.ReactNode;
copiedLabel?: React.ReactNode;
size?: "s" | "m";
variant?: "secondary" | "tertiary";
iconOnly?: boolean;
// How long the tick stays, in milliseconds.
resetAfter?: number;
onCopied?: (text: string) => void;
};
const sizes = {
s: { box: "h-8 gap-1.5 rounded-8 px-2.5 text-[13px] leading-[18px]", icon: "size-3.5", square: "w-8 px-0" },
m: { box: "h-10 gap-2 rounded-10 px-3.5 text-[14px] leading-5", icon: "size-4", square: "w-10 px-0" },
};
const variants = {
secondary: "border border-border-default bg-surface-card text-action-on-secondary shadow-xs hover:border-border-hover hover:bg-action-secondary-hover",
tertiary: "text-text-muted hover:bg-state-hover hover:text-text-strong",
};
// The copy icon turns into a tick and the label swaps, then both settle back.
export function CopyButton({
value,
label = "Copy",
copiedLabel = "Copied",
size = "m",
variant = "secondary",
iconOnly = false,
resetAfter = 1600,
onCopied,
className,
...rest
}: CopyButtonProps) {
const [state, setState] = React.useState<"idle" | "copied" | "failed">("idle");
const timer = React.useRef<ReturnType<typeof setTimeout>>(undefined);
React.useEffect(() => () => clearTimeout(timer.current), []);
const copy = async () => {
clearTimeout(timer.current);
try {
const text = typeof value === "function" ? await value() : value;
await navigator.clipboard.writeText(text);
setState("copied");
onCopied?.(text);
} catch {
setState("failed");
}
timer.current = setTimeout(() => setState("idle"), resetAfter);
};
const s = sizes[size];
const done = state === "copied";
return (
<button
type="button"
{...rest}
onClick={copy}
aria-label={iconOnly ? (done ? "Copied" : typeof label === "string" ? label : "Copy") : rest["aria-label"]}
className={cx(
"transition-ui press inline-flex shrink-0 select-none items-center justify-center whitespace-nowrap font-medium outline-none focus-visible:shadow-focus",
s.box,
iconOnly && s.square,
variants[variant],
className,
)}
>
<span className={cx("relative inline-grid place-items-center", s.icon)}>
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
className={cx("transition-ui col-start-1 row-start-1 size-full", done ? "rotate-[-20deg] scale-50 opacity-0" : "scale-100 opacity-100")}
>
<rect x="9" y="9" width="12" height="12" rx="2.5" />
<path d="M5 15a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2" />
</svg>
{done ? (
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2.5"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
className="animate-pop-in col-start-1 row-start-1 size-full text-icon-success"
>
<path d="M5 12.5 10 17.5 19.5 7" strokeDasharray="24" className="animate-draw [--draw-length:24]" />
</svg>
) : null}
</span>
{iconOnly ? null : (
<span className="relative inline-grid overflow-hidden">
<span className={cx("transition-ui col-start-1 row-start-1", state === "idle" ? "translate-y-0 opacity-100" : "-translate-y-2.5 opacity-0")}>
{label}
</span>
<span
aria-hidden={state === "idle"}
className={cx("transition-ui col-start-1 row-start-1", state === "idle" ? "translate-y-2.5 opacity-0" : "translate-y-0 opacity-100")}
>
{state === "failed" ? "Couldn't copy" : copiedLabel}
</span>
</span>
)}
<span className="sr-only" aria-live="polite">
{done ? "Copied to clipboard" : state === "failed" ? "Couldn't copy" : ""}
</span>
</button>
);
}
Installation
With the @ochre registry set up, one command adds the component and what it imports (utils). Or copy the code above into components/ochre/copy-button.tsx.
shadcn CLI
npx shadcn@latest add @ochre/copy-buttonExamples
API key
Icon-only in a dense row, and a quiet tertiary version.
Production
ochre_live_7f3a…c19e
Staging
ochre_test_51bd…08aa
Reactsecret.tsx
import { CopyButton } from "@/components/ochre/copy-button";
const keys = [
{ name: "Production", id: "ochre_live_7f3a…c19e", value: "ochre_live_7f3a2b91d04c19e" },
{ name: "Staging", id: "ochre_test_51bd…08aa", value: "ochre_test_51bd77e2f08aa" },
];
export default function CopyButtonSecret() {
return (
<div className="w-full max-w-md divide-y divide-border-subtle rounded-16 border border-border-default bg-surface-card">
{keys.map((k) => (
<div key={k.name} className="flex items-center gap-3 px-4 py-3">
<div className="min-w-0 flex-1">
<p className="text-[14px] font-medium leading-5 text-text-strong">{k.name}</p>
<p className="truncate font-mono text-[12px] leading-4 text-text-subtle">{k.id}</p>
</div>
<CopyButton value={k.value} size="s" iconOnly label={`Copy ${k.name.toLowerCase()} key`} />
<CopyButton value={k.value} size="s" variant="tertiary" label="Copy key" />
</div>
))}
</div>
);
}
Motion
Built on Ochre's ease-out curves. With reduced motion switched on, everything still works, it just doesn't move.
01The copy icon tips and shrinks away while the tick pops in with an ease-out and draws its stroke.
02The label rises out and Copied rises in from below; the button keeps its width because both labels share one cell.
03Pressing gives a little (scale 0.97). After 1.6 seconds everything settles back.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| value | string | () => string | Promise<string> | – | The text to copy, or a function that returns it. |
| label | ReactNode | "Copy" | Resting label. |
| copiedLabel | ReactNode | "Copied" | Label once copied. |
| size | "s" | "m" | "m" | 32 or 40 pixels high. |
| variant | "secondary" | "tertiary" | "secondary" | Bordered, or quiet for toolbars and code blocks. |
| iconOnly | boolean | false | Square, icon only. The aria-label changes to Copied too. |
| resetAfter | number | 1600 | Milliseconds before it settles back. |
| onCopied | (text: string) => void | – | Called after a successful copy. |
Usage
Use it for
- API keys, install commands, links, ids and code: anything people paste somewhere else.
- Icon-only beside a value in a dense row, with an aria-label.
Avoid
- Copying something the person can't see first: show the value next to the button.
- A toast as well: the button already confirms it.