Loading...
Please waitLoading...
Please waitUse the useCopyToClipboard hook to copy text to the clipboard and track whether the copy action was successful, with an optional delay to reset the copied state.
pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-copy-to-clipboard
"use client"
import { useCopyToClipboard } from "@/hooks/use-copy-to-clipboard"
import { Button } from "@/components/ui/button"
export function Component() {
const [copy, isCopied] = useCopyToClipboard()
const copyText = async (text: string) => {
await copy(text)
.then(() => {
toast.success("Copied to clipboard")
})
.catch((error) => {
if (error instanceof Error) {
toast.error(error.message)
}
})
}
return (
<Button onClick={() => copyText("Hello, World!")}>
{isCopied ? "Text Copied" : "Click to copy"}
</Button>
)
}
Name | Type | Description | Default Value | Optional |
---|---|---|---|---|
delay | number | Time in milliseconds to reset isCopied . | 2000 | Yes |
Index | Type | Description |
---|---|---|
0 | (text: string) => Promise<boolean> | Function to copy text to the clipboard. |
1 | boolean | Indicates if the last copy was successful, resetting after the delay . |