-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
61 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,52 @@ | ||
import {useState, useCallback, useRef} from 'react'; | ||
import useUpdateEffect from './useUpdateEffect'; | ||
import {useCallback} from 'react'; | ||
import useSetState from './useSetState' | ||
import useRefMounted from './useRefMounted'; | ||
const writeTextDefault = require('copy-to-clipboard'); | ||
import * as writeText from 'copy-to-clipboard'; | ||
|
||
export type WriteText = (text: string) => Promise<void>; // https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText | ||
export interface UseCopyToClipboardOptions { | ||
writeText?: WriteText; | ||
onCopy?: (text: string) => void; | ||
onError?: (error: any, text: string) => void; | ||
export interface CopyToClipboardState { | ||
value?: string, | ||
noUserInteraction: boolean, | ||
error?: Error, | ||
} | ||
export type UseCopyToClipboard = (text?: string, options?: UseCopyToClipboardOptions) => [boolean, () => void]; | ||
|
||
const useCopyToClipboard: UseCopyToClipboard = (text = '', options) => { | ||
const {writeText = writeTextDefault, onCopy, onError} = (options || {}) as UseCopyToClipboardOptions; | ||
const useCopyToClipboard = (): [CopyToClipboardState, (value: string) => void] => { | ||
const mounted = useRefMounted(); | ||
const [state, setState] = useSetState<CopyToClipboardState>({ | ||
value: undefined, | ||
error: undefined, | ||
noUserInteraction: true | ||
}); | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
if (typeof text !== 'string') { | ||
console.warn('useCopyToClipboard hook expects first argument to be string.'); | ||
} | ||
} | ||
const copyToClipboard = useCallback((value) => { | ||
try { | ||
if (!value) { | ||
throw new Error('No value to copy to clipboard') | ||
} | ||
|
||
const mounted = useRefMounted(); | ||
const latestText = useRef(text); | ||
const [copied, setCopied] = useState(false); | ||
const copyToClipboard = useCallback(async () => { | ||
if (latestText.current !== text) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.warn('Trying to copy stale text.'); | ||
if (typeof value !== "string") { | ||
throw new Error(`Cannot copy typeof ${typeof value} to clipboard, must be a string`); | ||
} | ||
return; | ||
} | ||
|
||
try { | ||
await writeText(text); | ||
const noUserInteraction = writeText(value); | ||
if (!mounted.current) return; | ||
setCopied(true); | ||
onCopy && onCopy(text); | ||
|
||
setState({ | ||
value, | ||
error: undefined, | ||
noUserInteraction | ||
}); | ||
} catch (error) { | ||
if (!mounted.current) return; | ||
console.error(error); | ||
setCopied(false); | ||
onError && onError(error, text); | ||
} | ||
}, [text]); | ||
|
||
useUpdateEffect(() => { | ||
setCopied(false); | ||
latestText.current = text; | ||
}, [text]); | ||
setState({ | ||
value: undefined, | ||
error, | ||
noUserInteraction: true | ||
}); | ||
} | ||
}, []); | ||
|
||
return [copied, copyToClipboard]; | ||
return [state, copyToClipboard]; | ||
} | ||
|
||
export default useCopyToClipboard; |