mirror of
https://github.com/zhigang1992/react.git
synced 2026-02-13 09:11:21 +08:00
54 lines
1.2 KiB
TypeScript
54 lines
1.2 KiB
TypeScript
import usePortal from './use-portal'
|
|
import useWarning from './use-warning'
|
|
import { useCallback } from 'react'
|
|
|
|
export type UseClipboardOptions = {
|
|
onError: Function
|
|
}
|
|
|
|
export type UseClipboardResult = {
|
|
copy: (text: string) => void
|
|
}
|
|
|
|
const defaultOptions: UseClipboardOptions = {
|
|
onError: () => useWarning('Failed to copy.', 'use-clipboard'),
|
|
}
|
|
|
|
const useClipboard = (
|
|
options: UseClipboardOptions = defaultOptions,
|
|
): UseClipboardResult => {
|
|
const el = usePortal('clipboard')
|
|
|
|
const copyText = (el: HTMLElement | null, text: string) => {
|
|
if (!el || !text) return
|
|
const selection = window.getSelection()
|
|
if (!selection) return
|
|
|
|
el.style.whiteSpace = 'pre'
|
|
el.textContent = text
|
|
|
|
const range = window.document.createRange()
|
|
selection.removeAllRanges()
|
|
range.selectNode(el)
|
|
selection.addRange(range)
|
|
try {
|
|
window.document.execCommand('copy')
|
|
} catch (e) {
|
|
options.onError && options.onError()
|
|
}
|
|
|
|
selection.removeAllRanges()
|
|
if (el) {
|
|
el.textContent = ''
|
|
}
|
|
}
|
|
|
|
const copy = useCallback((text: string) => {
|
|
copyText(el ,text)
|
|
}, [el])
|
|
|
|
return { copy }
|
|
}
|
|
|
|
export default useClipboard
|