Created
September 3, 2024 18:13
-
-
Save ErfanEbrahimnia/15e83fb010381cd22de2fa074f924a39 to your computer and use it in GitHub Desktop.
Paste files from your clipboard with react-dropzone
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 { useRef, type ClipboardEvent } from "react"; | |
import { useDropzone } from "react-dropzone"; | |
function Dropzone() { | |
const inputRef = useRef<HTMLInputElement>(null) | |
const { | |
getRootProps, | |
getInputProps, | |
} = useDropzone({...}); | |
async function handlePaste(event: ClipboardEvent<HTMLInputElement>) { | |
const files = event.clipboardData?.files; | |
if (inputRef.current && files.length) { | |
inputRef.current.files = files; | |
inputRef.current.dispatchEvent(new Event("change", { bubbles: true })); | |
} | |
} | |
return ( | |
<div {...dropzoneRootProps} onPaste={handlePaste}> | |
<input {...getInputProps()} ref={inputRef} /> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment