Created
February 10, 2021 07:17
-
-
Save franky47/ed0c5765489f313f957bc1f5de7e416e to your computer and use it in GitHub Desktop.
usePasteAnywhere
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
import * as React from 'react' | |
export function usePasteAnywhere(callback: (text: string) => void) { | |
React.useEffect(() => { | |
const body = document.getElementsByTagName('body')[0] | |
const onPaste = (e: ClipboardEvent) => { | |
const data = e.clipboardData?.getData('text/plain') | |
if (data) { | |
callback(data) | |
} | |
} | |
body.addEventListener('paste', onPaste) | |
return () => { | |
body.removeEventListener('paste', onPaste) | |
} | |
}, [callback]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment