utility
useClipboard
Copies text to the clipboard and provides a transient 'copied' state that resets automatically after a configurable delay.
useClipboard.ts
import { useState, useCallback } from "react"
interface UseClipboardReturn {
copied: boolean
copy: (text: string) => Promise<void>
}
export function useClipboard(resetInterval = 2000): UseClipboardReturn {
const [copied, setCopied] = useState(false)
const copy = useCallback(
async (text: string) => {
try {
await navigator.clipboard.writeText(text)
setCopied(true)
setTimeout(() => setCopied(false), resetInterval)
} catch {
setCopied(false)
}
},
[resetInterval]
)
return { copied, copy }
}Was this helpful?
Sign in to give feedback
