Created
February 25, 2021 04:07
-
-
Save zisan34/5a10f4aafd8975af43895f3d01a189bb to your computer and use it in GitHub Desktop.
React-filedrop-component //credit:30 seconds of knowledge
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
function FileDrop(props) { | |
const [drag, setDrag] = React.useState(false); | |
const [filename, setFilename] = React.useState(''); | |
let dropRef = React.createRef(); | |
let dragCounter = 0; | |
const handleDrag = e => { | |
e.preventDefault(); | |
e.stopPropagation(); | |
}; | |
const handleDragIn = e => { | |
e.preventDefault(); | |
e.stopPropagation(); | |
dragCounter++; | |
if (e.dataTransfer.items && e.dataTransfer.items.length > 0) setDrag(true); | |
}; | |
const handleDragOut = e => { | |
e.preventDefault(); | |
e.stopPropagation(); | |
dragCounter--; | |
if (dragCounter === 0) setDrag(false); | |
}; | |
const handleDrop = e => { | |
e.preventDefault(); | |
e.stopPropagation(); | |
setDrag(false); | |
if (e.dataTransfer.files && e.dataTransfer.files.length > 0) { | |
props.handleDrop(e.dataTransfer.files[0]); | |
setFilename(e.dataTransfer.files[0].name); | |
e.dataTransfer.clearData(); | |
dragCounter = 0; | |
} | |
}; | |
React.useEffect(() => { | |
let div = dropRef.current; | |
div.addEventListener("dragenter", handleDragIn); | |
div.addEventListener("dragleave", handleDragOut); | |
div.addEventListener("dragover", handleDrag); | |
div.addEventListener("drop", handleDrop); | |
return function cleanup() { | |
div.removeEventListener("dragenter", handleDragIn); | |
div.removeEventListener("dragleave", handleDragOut); | |
div.removeEventListener("dragover", handleDrag); | |
div.removeEventListener("drop", handleDrop); | |
}; | |
}); | |
return ( | |
<div | |
ref={dropRef} | |
className={ | |
drag ? "filedrop drag" : filename ? "filedrop ready" : "filedrop" | |
} | |
> | |
{filename && !drag ? <div>{filename}</div> : <div>Drop files here!</div>} | |
</div> | |
); | |
} |
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
.filedrop { | |
min-height: 120px; | |
border: 3px solid #D3D3D3; | |
text-align: center; | |
font-size: 24px; | |
padding: 32px; | |
border-radius: 4px; | |
} | |
.filedrop.drag { | |
border: 3px dashed #1E90FF; | |
} | |
.filedrop.ready { | |
border: 3px solid #32CD32; | |
} |
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
ReactDOM.render(<FileDrop handleDrop={console.log}/>, document.getElementById('root')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment