From 279d83122f77cdb140e7ef3dce0d8fc5dce04b44 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Wed, 15 Apr 2020 21:15:16 +1000 Subject: [PATCH] Added drag and drop support to map upload --- src/components/AddMapButton.js | 3 +- src/modals/AddMapModal.js | 75 ++++++++++++++++++++++++++++++---- 2 files changed, 69 insertions(+), 9 deletions(-) diff --git a/src/components/AddMapButton.js b/src/components/AddMapButton.js index bac0be2..5e67203 100644 --- a/src/components/AddMapButton.js +++ b/src/components/AddMapButton.js @@ -18,8 +18,7 @@ function AddMapButton({ onMapChanged }) { const mapDataRef = useRef(null); const [mapSource, setMapSource] = useState(null); - function handleImageUpload(event) { - const file = event.target.files[0]; + function handleImageUpload(file) { const url = URL.createObjectURL(file); let image = new Image(); image.onload = function () { diff --git a/src/modals/AddMapModal.js b/src/modals/AddMapModal.js index fd1a735..641fc42 100644 --- a/src/modals/AddMapModal.js +++ b/src/modals/AddMapModal.js @@ -1,5 +1,13 @@ -import React, { useRef } from "react"; -import { Box, Button, Image as UIImage, Flex, Label, Input } from "theme-ui"; +import React, { useRef, useState } from "react"; +import { + Box, + Button, + Image as UIImage, + Flex, + Label, + Input, + Text, +} from "theme-ui"; import Modal from "../components/Modal"; @@ -22,6 +30,30 @@ function AddMapModal({ fileInputRef.current.click(); } } + + const [dragging, setDragging] = useState(false); + function handleImageDragEnter(event) { + event.preventDefault(); + event.stopPropagation(); + setDragging(true); + } + + function handleImageDragLeave(event) { + event.preventDefault(); + event.stopPropagation(); + setDragging(false); + } + + function handleImageDrop(event) { + event.preventDefault(); + event.stopPropagation(); + const file = event.dataTransfer.files[0]; + if (file.type.startsWith("image")) { + onImageUpload(file); + } + setDragging(false); + } + return ( - onImageUpload(event.target.files[0])} type="file" accept="image/*" style={{ display: "none" }} ref={fileInputRef} /> - + + )} + {dragging && ( + { + e.preventDefault(); + e.stopPropagation(); + e.dataTransfer.dropEffect = "copy"; + }} + onDrop={handleImageDrop} + > + Drop map to upload + + )}