asafefe.dev

use-copy-to-clipboard

A hook that copies text and reports a transient copied state.

Installation

$ pnpm dlx shadcn@latest add @soultel/use-copy-to-clipboard

No namespace set up? npx shadcn@latest add https://asafefe.dev/r/use-copy-to-clipboard.json works too.

Manual installation
  1. 1. Copy the source from the Code tab into hooks/use-copy-to-clipboard.ts.

Usage

example.tsx6 lines
import { useCopyToClipboard } from "@/hooks/use-copy-to-clipboard"

export function Example() {
  const { copied, copy } = useCopyToClipboard()
  return <button onClick={() => copy("hello")}>{copied ? "Copied" : "Copy"}</button>
}

Props

PropTypeDefaultDescription
timeoutnumber2000How long `copied` stays true, in ms.