diff --git a/resources/scripts/components/elements/Modal.tsx b/resources/scripts/components/elements/Modal.tsx index 395f7a5a1..b62918050 100644 --- a/resources/scripts/components/elements/Modal.tsx +++ b/resources/scripts/components/elements/Modal.tsx @@ -20,7 +20,7 @@ export interface ModalProps extends RequiredModalProps { showSpinnerOverlay?: boolean; } -const ModalMask = styled.div` +export const ModalMask = styled.div` ${tw`fixed z-50 overflow-auto flex w-full inset-0`}; background: rgba(0, 0, 0, 0.70); `; diff --git a/resources/scripts/components/server/files/UploadButton.tsx b/resources/scripts/components/server/files/UploadButton.tsx index e328c5dc8..979b0a600 100644 --- a/resources/scripts/components/server/files/UploadButton.tsx +++ b/resources/scripts/components/server/files/UploadButton.tsx @@ -5,39 +5,41 @@ import tw from 'twin.macro'; import Button from '@/components/elements/Button'; import React, { useEffect, useState } from 'react'; import styled from 'styled-components/macro'; +import { ModalMask } from '@/components/elements/Modal'; +import Fade from '@/components/elements/Fade'; +import useEventListener from '@/plugins/useEventListener'; -const ModalMask = styled.div` - ${tw`fixed z-50 overflow-auto flex w-full inset-0`}; - background: rgba(0, 0, 0, 0.70); +const InnerContainer = styled.div` + max-width: 600px; + ${tw`bg-black w-full border-4 border-primary-500 border-dashed rounded p-10 mx-10`} `; export default () => { const { uuid } = useServer(); const [ visible, setVisible ] = useState(false); - const handleEscapeEvent = () => { + useEventListener('dragenter', e => { + e.stopPropagation(); + setVisible(true); + }, true); + + useEventListener('dragexit', e => { + e.stopPropagation(); setVisible(false); - }; + }, true); useEffect(() => { - window.addEventListener('keydown', handleEscapeEvent); + if (!visible) return; - return () => window.removeEventListener('keydown', handleEscapeEvent); + const hide = () => setVisible(false); + + window.addEventListener('keydown', hide); + return () => { + window.removeEventListener('keydown', hide); + }; }, [ visible ]); - const onDragOver = (e: any) => { - e.preventDefault(); - }; - - const onDragEnter = (e: any) => { - e.preventDefault(); - }; - - const onDragLeave = (e: any) => { - e.preventDefault(); - }; - - const onFileDrop = (e: any) => { + const onFileDrop = (e: React.DragEvent) => { e.preventDefault(); if (e.dataTransfer === undefined || e.dataTransfer === null) { @@ -55,12 +57,10 @@ export default () => { formData.append('files', files[i]); } - console.log('getFileUploadUrl'); getFileUploadUrl(uuid) .then(url => { console.log(url); - // `${url}&directory=` axios.post(url, formData, { headers: { 'Content-Type': 'multipart/form-data', @@ -81,20 +81,26 @@ export default () => { return ( <> - { - visible ? - -
-
- -

Drag and drop files to upload

-
-
-
- : - null - } - + + setVisible(false)} + onDrop={onFileDrop} + > +
+ +

+ Drag and drop files to upload. +

+
+
+
+
diff --git a/resources/scripts/plugins/useEventListener.ts b/resources/scripts/plugins/useEventListener.ts index 1328fffff..969549339 100644 --- a/resources/scripts/plugins/useEventListener.ts +++ b/resources/scripts/plugins/useEventListener.ts @@ -1,23 +1,20 @@ import { useEffect, useRef } from 'react'; -export default (eventName: string, handler: (e: Event | CustomEvent | UIEvent | any) => void, element: any = window) => { +export default (eventName: string, handler: (e: Event | CustomEvent | UIEvent | any) => void, options?: boolean | EventListenerOptions) => { const savedHandler = useRef(null); useEffect(() => { savedHandler.current = handler; }, [ handler ]); - useEffect( - () => { - const isSupported = element && element.addEventListener; - if (!isSupported) return; + useEffect(() => { + const isSupported = document && document.addEventListener; + if (!isSupported) return; - const eventListener = (event: any) => savedHandler.current(event); - element.addEventListener(eventName, eventListener); - return () => { - element.removeEventListener(eventName, eventListener); - }; - }, - [ eventName, element ], - ); + const eventListener = (event: any) => savedHandler.current(event); + document.addEventListener(eventName, eventListener, options); + return () => { + document.removeEventListener(eventName, eventListener); + }; + }, [ eventName, document ]); };