Skip to content

Commit

Permalink
Merge pull request #53 from zkemail/drag-n-drop
Browse files Browse the repository at this point in the history
Added drag and drop feature
  • Loading branch information
Divide-By-0 authored Apr 28, 2023
2 parents dbf40a3 + 79e9391 commit 94a9b88
Show file tree
Hide file tree
Showing 4 changed files with 415 additions and 221 deletions.
44 changes: 44 additions & 0 deletions src/components/DragAndDropTextBox.tsx
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;
51 changes: 51 additions & 0 deletions src/hooks/useDragAndDrop.ts
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,
};
};
Loading

0 comments on commit 94a9b88

Please sign in to comment.