Craft›Copy Button

Copy Button

A copy button whose icon morphs into a check and whose label swaps to Copied while the button resizes to fit, with blur cross-fades and a spring; announced to screen readers, colour of the check as a prop.

Copy a ready prompt for Claude Code, Codex or Cursor

Seen on

Original to Copia

Dependencies

reacttailwindcssmotionlucide-react

Interaction type

morph

Install via CLI

pnpm dlx shadcn@latest add @copia/copy-button

Without the namespace: npx shadcn@latest add https://copia-ui.vercel.app/r/copy-button.json

How to use

demo.tsx
"use client"

import { useEffect, useState } from "react"
import { CopyButton, type CopyButtonProps } from "./copy-button"

const COMMAND = "npx shadcn add @copia/copy-button"

/** Plays the copied state on a loop until you click it yourself. */
export default function Demo({ successColor = "#30d158", timeout = 1600 }: Pick<CopyButtonProps, "successColor" | "timeout">) {
  const [auto, setAuto] = useState(true)
  const [copied, setCopied] = useState(false)

  useEffect(() => {
    if (!auto || matchMedia("(prefers-reduced-motion: reduce)").matches) return
    let on = false
    const id = setInterval(() => {
      on = !on
      setCopied(on)
    }, 1300)
    return () => clearInterval(id)
  }, [auto])

  return (
    <div className="grid flex-1 place-items-center p-6" onPointerDown={() => setAuto(false)}>
      <div className="flex flex-col items-center gap-4">
        <code className="rounded-lg bg-muted px-3 py-1.5 font-mono text-xs text-muted-foreground">{COMMAND}</code>
        <CopyButton value={COMMAND} copied={auto ? copied : undefined} successColor={successColor} timeout={timeout} />
      </div>
    </div>
  )
}

Customization

Use the sliders button at the top right of the preview to tweak successColor, timeout live and copy the generated JSX.

Props

PropTypeDefaultDescription
valuestring-Text written to the clipboard.
label / copiedLabelstring"Copy" / "Copied"The two labels.
timeoutnumber1600How long the copied state stays, in ms.
copiedboolean-Show the copied state without copying (controlled).
onCopy(value) => void-Called after a successful copy.
successColorstring"#30d158"Colour of the check.

Attribution

Original component. Recreated by Copia.

Read how it was reverse engineered

Source code

copy-button.tsx
"use client"

import { AnimatePresence, motion, useReducedMotion } from "motion/react"
import { Check, Copy } from "lucide-react"
import { useEffect, useRef, useState } from "react"
import { cn } from "@/lib/utils"

export type CopyButtonProps = {
  /** Text written to the clipboard. */
  value: string
  label?: string
  copiedLabel?: string
  /** How long the copied state stays, in ms. */
  timeout?: number
  /** Show the copied state without copying (controlled). */
  copied?: boolean
  onCopy?: (value: string) => void
  /** Colour of the check. */
  successColor?: string
  className?: string
}

const MORPH = { type: "spring", bounce: 0.25, duration: 0.4 } as const
const SWAP = {
  initial: { opacity: 0, scale: 0.6, filter: "blur(4px)" },
  animate: { opacity: 1, scale: 1, filter: "blur(0px)" },
  exit: { opacity: 0, scale: 0.6, filter: "blur(4px)" },
}

/**
 * A copy button whose icon morphs into a check and whose label swaps to
 * "Copied" while the button resizes to fit, then morphs back. The change is
 * announced to screen readers.
 */
export function CopyButton({ value, label = "Copy", copiedLabel = "Copied", timeout = 1600, copied, onCopy, successColor = "#30d158", className }: CopyButtonProps) {
  const [done, setDone] = useState(false)
  const timer = useRef<ReturnType<typeof setTimeout>>(undefined)
  const reduced = useReducedMotion()
  const shown = copied ?? done
  useEffect(() => () => clearTimeout(timer.current), [])

  const copy = async () => {
    try {
      await navigator.clipboard.writeText(value)
    } catch {
      return
    }
    onCopy?.(value)
    setDone(true)
    clearTimeout(timer.current)
    timer.current = setTimeout(() => setDone(false), timeout)
  }

  const transition = reduced ? { duration: 0 } : MORPH
  return (
    <motion.button
      type="button"
      layout
      transition={transition}
      onClick={copy}
      className={cn(
        "inline-flex h-10 items-center gap-2 overflow-hidden rounded-full bg-foreground px-4 text-sm font-medium text-background outline-none focus-visible:ring-2 focus-visible:ring-ring active:scale-[0.97]",
        className,
      )}
    >
      <span className="relative grid size-4 place-items-center">
        <AnimatePresence initial={false} mode="popLayout">
          <motion.span key={shown ? "check" : "copy"} {...SWAP} transition={transition} className="grid" style={{ color: shown ? successColor : undefined }}>
            {shown ? <Check className="size-4" strokeWidth={2.75} /> : <Copy className="size-4" />}
          </motion.span>
        </AnimatePresence>
      </span>
      <AnimatePresence initial={false} mode="popLayout">
        <motion.span key={shown ? "copied" : "copy"} layout="position" {...SWAP} transition={transition} className="whitespace-nowrap">
          {shown ? copiedLabel : label}
        </motion.span>
      </AnimatePresence>
      <span className="sr-only" aria-live="polite">
        {shown ? copiedLabel : ""}
      </span>
    </motion.button>
  )
}

Keep in mind

Most components here are recreations of the best interfaces out there. We don't claim to be the original creators: this is our attempt to reverse engineer, replicate and often add a few features. We credit everyone we can; if we missed someone, let us know.

Contact

Found a bug or a missing credit? Open an issue

License & usage

  • — Free to use and modify in personal and commercial projects (MIT).
  • — No attribution required, but a link back to Copia is appreciated.
  • — Demo images are not part of the license; replace them with your own.
PreviousAgent StatusNext Hover Reel
npx shadcn add @copia/copy-button