Initial commit
This commit is contained in:
29
web/hooks/use-copy-to-clipboard.ts
Normal file
29
web/hooks/use-copy-to-clipboard.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { useState } from 'react'
|
||||
|
||||
type CopiedValue = string | null
|
||||
type CopyFn = (text: string) => Promise<boolean>
|
||||
|
||||
function useCopyToClipboard(): [CopiedValue, CopyFn] {
|
||||
const [copiedText, setCopiedText] = useState<CopiedValue>(null)
|
||||
|
||||
const copy: CopyFn = async text => {
|
||||
if (!navigator?.clipboard) {
|
||||
console.warn('Clipboard not supported')
|
||||
return false
|
||||
}
|
||||
|
||||
try {
|
||||
await navigator.clipboard.writeText(text)
|
||||
setCopiedText(text)
|
||||
return true
|
||||
} catch (error) {
|
||||
console.warn('Copy failed', error)
|
||||
setCopiedText(null)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return [copiedText, copy]
|
||||
}
|
||||
|
||||
export default useCopyToClipboard
|
Reference in New Issue
Block a user