Primitives
Copy Button
The little copy button with the check-mark flip — done once, consistently.
Motionicon scale-swap (AnimatePresence) on copy
Install
Install Kineto and follow the theme setup once in your app.
bash
npm install kinetotsx
import { CopyButton } from "kineto/copy-button";Preview
npm i kinetotsx
"use client";
import * as React from "react";
import { AnimatePresence, motion } from "motion/react";
import { Check, Copy } from "lucide-react";
import { cn } from "@/lib/cn";
import { spring } from "@/lib/motion-presets";
export interface CopyButtonProps extends Omit<React.HTMLAttributes<HTMLButtonElement>, "onCopy"> {
/** The text to write to the clipboard. */
value: string;
/** How long (ms) to show the success state. */
timeout?: number;
/** Called after a successful copy. */
onCopy?: (value: string) => void;
}
/**
* The little copy button with the check-mark flip — done once, consistently. Writes `value` to the
* clipboard and swaps the icon to a check for `timeout` ms. Everyone reinvents this; here it's canonical.
*/
const CopyButton = React.forwardRef<HTMLButtonElement, CopyButtonProps>(
({ className, value, timeout = 2000, onCopy, ...props }, ref) => {
const [copied, setCopied] = React.useState(false);
const timer = React.useRef<ReturnType<typeof setTimeout>>();
React.useEffect(() => () => clearTimeout(timer.current), []);
const copy = async () => {
try {
await navigator.clipboard.writeText(value);
setCopied(true);
onCopy?.(value);
clearTimeout(timer.current);
timer.current = setTimeout(() => setCopied(false), timeout);
} catch {
// Clipboard unavailable (insecure context / denied) — fail quietly.
}
};
return (
<button
ref={ref}
type="button"
onClick={copy}
aria-label={copied ? "Copied" : "Copy"}
data-copied={copied}
className={cn(
"inline-flex size-8 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring [&_svg]:size-4",
className
)}
{...props}
>
<AnimatePresence mode="wait" initial={false}>
<motion.span
key={copied ? "check" : "copy"}
initial={{ scale: 0.6, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
exit={{ scale: 0.6, opacity: 0 }}
transition={spring.snappy}
className={cn("flex", copied && "text-success")}
>
{copied ? <Check /> : <Copy />}
</motion.span>
</AnimatePresence>
</button>
);
}
);
CopyButton.displayName = "CopyButton";
export { CopyButton };src/components/ui/copy-button.tsx
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| value | string | — | Text written to the clipboard. |
| timeout | number | 2000 | How long (ms) to show the success state. |
| onCopy | (value: string) => void | — | Called after a successful copy. |