Skip to content

Instantly share code, notes, and snippets.

@ErfanEbrahimnia
Created September 3, 2024 18:13
Show Gist options
  • Save ErfanEbrahimnia/15e83fb010381cd22de2fa074f924a39 to your computer and use it in GitHub Desktop.
Save ErfanEbrahimnia/15e83fb010381cd22de2fa074f924a39 to your computer and use it in GitHub Desktop.
Paste files from your clipboard with react-dropzone
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