Back to ButtonsYou are on Copy Button.

Copy Button

Copies text and swaps its icon in a quick blur crossfade.

Available inAvailable in React, Vue, Svelte, Solid, JavaScript· All five frameworks
PrequelUI

Libraries

PrequelUI

Motion

Spring + short tween

Reduced motion

Fade in place
copy-button.tsx
Play with the real component, then read the code it ships with.All components
01

Installation

Installing for React·components/prequelui/copy-button.tsx

Add the component

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/copy-button.json
npmthe default on every machine

First component? Set up your project first

Frameworks

The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.

5 runtimes
copy-button.tsx
"use client";

import * as React from "react";
import { AnimatePresence, motion, type Transition } from "motion/react";
import { cn } from "@/lib/utils";
import { Button, type ButtonProps } from "./button";
import {
  blurIcon,
  swapTween,
  useReduceMotion,
  useTempoTransition,
} from "./motion";

type CopyContextValue = { copied: boolean };

const CopyContext = React.createContext<CopyContextValue | null>(null);

function useCopyState() {
  const value = React.useContext(CopyContext);
  if (!value) throw new Error("CopyButtonIcon must be used inside CopyButton");
  return value;
}

export type CopyButtonProps = ButtonProps & {
  content: string;
  copied?: boolean;
  defaultCopied?: boolean;
  onCopiedChange?: (copied: boolean, content?: string) => void;
  delay?: number;
};

export function CopyButton({
  content,
  copied,
  defaultCopied = false,
  onCopiedChange,
  delay = 3000,
  onClick,
  className,
  ...props
}: CopyButtonProps) {
  const [inner, setInner] = React.useState(defaultCopied);
  const isCopied = copied ?? inner;
  const timeout = React.useRef<ReturnType<typeof setTimeout> | undefined>(undefined);

  React.useEffect(() => () => clearTimeout(timeout.current), []);

  const setCopied = (value: boolean) => {
    if (copied === undefined) setInner(value);
    onCopiedChange?.(value, content);
  };

  const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
    onClick?.(event);
    if (event.defaultPrevented || !content || isCopied) return;
    navigator.clipboard
      ?.writeText(content)
      .then(() => {
        setCopied(true);
        clearTimeout(timeout.current);
        timeout.current = setTimeout(() => setCopied(false), delay);
      })
      .catch(() => {
        setCopied(true);
        clearTimeout(timeout.current);
        timeout.current = setTimeout(() => setCopied(false), delay);
      });
  };

  return (
    <CopyContext.Provider value={{ copied: isCopied }}>
      <Button
        data-slot="copy-button"
        data-copied={isCopied ? "" : undefined}
        onClick={handleClick}
        className={cn(
          "border bg-background text-foreground hover:bg-accent",
          className,
        )}
        {...props}
      />
    </CopyContext.Provider>
  );
}

export type CopyButtonIconProps = Omit<React.ComponentProps<"span">, "children"> & {
  icon: React.ReactNode;
  copiedIcon: React.ReactNode;
  transition?: Transition;
};

export function CopyButtonIcon({
  icon,
  copiedIcon,
  transition,
  className,
  ...props
}: CopyButtonIconProps) {
  const { copied } = useCopyState();
  const reduce = useReduceMotion();
  const tween = useTempoTransition(transition ?? swapTween);
  const hidden = reduce
    ? { opacity: 0, filter: blurIcon }
    : { opacity: 0, scale: 0.5, filter: blurIcon };
  const shown = reduce
    ? { opacity: 1, filter: "blur(0px)" }
    : { opacity: 1, scale: 1, filter: "blur(0px)" };

  return (
    <span
      data-slot="copy-button-icon"
      className={cn("relative inline-flex size-4 items-center justify-center", className)}
      {...props}
    >
      <AnimatePresence mode="popLayout" initial={false}>
        <motion.span
          key={copied ? "copied" : "copy"}
          aria-hidden="true"
          initial={hidden}
          animate={shown}
          exit={hidden}
          transition={tween}
          className="absolute inset-0 inline-flex items-center justify-center"
        >
          {copied ? copiedIcon : icon}
        </motion.span>
      </AnimatePresence>
    </span>
  );
}

export function useCopyButton() {
  return useCopyState();
}

Install

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/copy-button.json
npmthe default on every machine

Dependencies

motion lucide-react

The registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.

02

Usage

Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.

copy-button.tsx
import { Check, Copy } from "lucide-react";
import { CopyButton, CopyButtonIcon } from "@/components/prequelui/copy-button";

export default function Demo() {
  return (
    <CopyButton content="npx shadcn@latest add button">
      <CopyButtonIcon icon={<Copy />} copiedIcon={<Check />} />
      Copy command
    </CopyButton>
  );
}
import { CopyButton } from "@/components/prequelui/copy-button";

export function Variant() {
  return (
    <CopyButton tempo={1.2}>
      …
    </CopyButton>
  );
}
03

Animation

Copying swaps one glyph for another in place. The leaving icon shrinks to half, blurs by two pixels and fades in 150 ms while the check takes its place on the same curve; the state returns after three seconds.

  • Icon swap: 150 ms, scale 0.5 → 1, blur 2 px → 0.
  • The button itself keeps the shared press spring.
  • The reset timer is cleared if the button is clicked again.
  • Reduced motion keeps the crossfade and removes scale and blur.
swap 150 msscale 0.5blur 2 pxreset 3 s
04

API Reference

Each part accepts the usual HTML props in addition to the motion props below. No accessibility prop is hidden behind the animation layer.

CopyButton

PropTypeDefault
contentstring—
delaynumber3000
copiedboolean—
defaultCopiedbooleanfalse
onCopiedChange(copied: boolean) => void—

CopyButtonIcon

PropTypeDefault
iconReactNode—
copiedIconReactNode—
transitionTransitioncomponent spring or tween

Next component

Flip Button

A button that turns over on hover to reveal its back face.