Skip to content

Instantly share code, notes, and snippets.

@franky47
Created February 10, 2021 07:17
Show Gist options
  • Save franky47/ed0c5765489f313f957bc1f5de7e416e to your computer and use it in GitHub Desktop.
Save franky47/ed0c5765489f313f957bc1f5de7e416e to your computer and use it in GitHub Desktop.
usePasteAnywhere
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