hookagain

hookagain

Hooks

useCopyToClipboard

A hook that allows you to copy text to the clipboard.

Installation

npx shadcn@latest add https://hookagain.vercel.app/r/use-copy-to-clipboard.json

Usage

import { useCopyToClipboard } from "@/hooks/use-copy-to-clipboard"
 
function Example() {
  const { isCopied, copyToClipboard } = useCopyToClipboard({
    timeout: 2000, // Reset copied state after 2 seconds
    onCopy: (text) => console.log(`Copied: ${text}`),
  })
 
  return (
    <button onClick={() => copyToClipboard("Hello World!")}>
      {isCopied ? "Copied!" : "Copy Text"}
    </button>
  )
}

On this page