Download react-use/src/useCopyToClipboard.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.95 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useCopyToClipboard.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useCopyToClipboard.ts
-
curl -L -o useCopyToClipboard.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useCopyToClipboard.ts
1.95 kB
| import writeText from 'copy-to-clipboard'; | |
| import { useCallback } from 'react'; | |
| import useMountedState from './useMountedState'; | |
| import useSetState from './useSetState'; | |
| export interface CopyToClipboardState { | |
| value?: string; | |
| noUserInteraction: boolean; | |
| error?: Error; | |
| } | |
| const useCopyToClipboard = (): [CopyToClipboardState, (value: string) => void] => { | |
| const isMounted = useMountedState(); | |
| const [state, setState] = useSetState<CopyToClipboardState>({ | |
| value: undefined, | |
| error: undefined, | |
| noUserInteraction: true, | |
| }); | |
| const copyToClipboard = useCallback((value) => { | |
| if (!isMounted()) { | |
| return; | |
| } | |
| let noUserInteraction; | |
| let normalizedValue; | |
| try { | |
| // only strings and numbers casted to strings can be copied to clipboard | |
| if (typeof value !== 'string' && typeof value !== 'number') { | |
| const error = new Error( | |
| `Cannot copy typeof ${typeof value} to clipboard, must be a string` | |
| ); | |
| if (process.env.NODE_ENV === 'development') console.error(error); | |
| setState({ | |
| value, | |
| error, | |
| noUserInteraction: true, | |
| }); | |
| return; | |
| } | |
| // empty strings are also considered invalid | |
| else if (value === '') { | |
| const error = new Error(`Cannot copy empty string to clipboard.`); | |
| if (process.env.NODE_ENV === 'development') console.error(error); | |
| setState({ | |
| value, | |
| error, | |
| noUserInteraction: true, | |
| }); | |
| return; | |
| } | |
| normalizedValue = value.toString(); | |
| noUserInteraction = writeText(normalizedValue); | |
| setState({ | |
| value: normalizedValue, | |
| error: undefined, | |
| noUserInteraction, | |
| }); | |
| } catch (error) { | |
| setState({ | |
| value: normalizedValue, | |
| error, | |
| noUserInteraction, | |
| }); | |
| } | |
| }, []); | |
| return [state, copyToClipboard]; | |
| }; | |
| export default useCopyToClipboard; | |