-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from zkemail/drag-n-drop
Added drag and drop feature
- Loading branch information
Showing
4 changed files
with
415 additions
and
221 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from "react"; | ||
import styled from 'styled-components'; | ||
import { useDragAndDrop } from '../hooks/useDragAndDrop'; | ||
|
||
const DragAndDropTextBoxWrapper = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
border: 2px dashed #ccc; | ||
padding: 20px; | ||
`; | ||
|
||
type Props = { | ||
onFileDrop: (file: File) => void; | ||
}; | ||
|
||
const DragAndDropTextBox: React.FC<Props> = ({onFileDrop}) => { | ||
const { | ||
dragging, | ||
handleDragEnter, | ||
handleDragLeave, | ||
handleDragOver, | ||
handleDrop, | ||
handleDragEnd, | ||
} = useDragAndDrop(); | ||
|
||
return ( | ||
<DragAndDropTextBoxWrapper | ||
onDragEnter={handleDragEnter} | ||
onDragLeave={handleDragLeave} | ||
onDragOver={handleDragOver} | ||
onDragEnd={handleDragEnd} | ||
onDrop={(e) => handleDrop(e, onFileDrop)} | ||
> | ||
{dragging ? ( | ||
<div>Drop here</div> | ||
) : ( | ||
<div>Drop .eml file here</div> | ||
)} | ||
</DragAndDropTextBoxWrapper> | ||
); | ||
}; | ||
|
||
export default DragAndDropTextBox; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React, { useCallback, useState } from "react"; | ||
|
||
export const useDragAndDrop = () => { | ||
const [dragging, setDragging] = useState(false); | ||
|
||
const handleDragEnter = useCallback((e: React.DragEvent) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
setDragging(true); | ||
}, []); | ||
|
||
const handleDragLeave = useCallback((e: React.DragEvent) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
setDragging(false); | ||
}, []); | ||
|
||
const handleDragOver = useCallback((e: React.DragEvent) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
}, []); | ||
|
||
const handleDrop = useCallback( | ||
(e: React.DragEvent, onDrop: (file: File) => void) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
setDragging(false); | ||
|
||
const files = e.dataTransfer.files; | ||
|
||
if (files.length > 0) { | ||
onDrop(files[0]); | ||
} | ||
}, | ||
[] | ||
); | ||
|
||
const handleDragEnd = useCallback((e: React.DragEvent) => { | ||
e.preventDefault(); | ||
setDragging(false); | ||
}, []); | ||
|
||
return { | ||
dragging, | ||
handleDragEnter, | ||
handleDragLeave, | ||
handleDragOver, | ||
handleDrop, | ||
handleDragEnd, | ||
}; | ||
}; |
Oops, something went wrong.