|
| 1 | +import { Resource } from '@tomic/react'; |
| 2 | +import React, { useCallback, useEffect } from 'react'; |
| 3 | +import { useDropzone } from 'react-dropzone'; |
| 4 | +import { FaUpload } from 'react-icons/fa'; |
| 5 | +import styled, { keyframes } from 'styled-components'; |
| 6 | +import { ErrMessage } from '../InputStyles'; |
| 7 | +import { useUpload } from './useUpload'; |
| 8 | + |
| 9 | +export interface FileDropZoneProps { |
| 10 | + parentResource: Resource; |
| 11 | + onFilesUploaded?: (files: string[]) => void; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * A dropzone for adding files. Renders its children by default, unless you're |
| 16 | + * holding a file, an error occurred, or it's uploading. |
| 17 | + */ |
| 18 | +export function FileDropZone({ |
| 19 | + parentResource, |
| 20 | + children, |
| 21 | + onFilesUploaded, |
| 22 | +}: React.PropsWithChildren<FileDropZoneProps>): JSX.Element { |
| 23 | + const { upload, isUploading, error } = useUpload(parentResource); |
| 24 | + const dropzoneRef = React.useRef<HTMLDivElement>(null); |
| 25 | + const onDrop = useCallback( |
| 26 | + async (files: File[]) => { |
| 27 | + const uploaded = await upload(files); |
| 28 | + onFilesUploaded?.(uploaded); |
| 29 | + }, |
| 30 | + [upload], |
| 31 | + ); |
| 32 | + |
| 33 | + const { getRootProps, isDragActive } = useDropzone({ onDrop }); |
| 34 | + |
| 35 | + // Move the dropzone down if the user has scrolled down. |
| 36 | + useEffect(() => { |
| 37 | + if (isDragActive && dropzoneRef.current) { |
| 38 | + const rect = dropzoneRef.current.getBoundingClientRect(); |
| 39 | + |
| 40 | + if (rect.top < 0) { |
| 41 | + dropzoneRef.current.style.top = `calc(${Math.abs(rect.top)}px + 1rem)`; |
| 42 | + } |
| 43 | + } |
| 44 | + }, [isDragActive]); |
| 45 | + |
| 46 | + return ( |
| 47 | + <Root |
| 48 | + {...getRootProps()} |
| 49 | + // For some reason this is tabbable by default, but it does not seem to actually help users. |
| 50 | + // Let's disable it. |
| 51 | + tabIndex={-1} |
| 52 | + > |
| 53 | + {isUploading && <p>{'Uploading...'}</p>} |
| 54 | + {error && <ErrMessage>{error.message}</ErrMessage>} |
| 55 | + {children} |
| 56 | + {isDragActive && ( |
| 57 | + <VisualDropzone ref={dropzoneRef}> |
| 58 | + <TextWrapper> |
| 59 | + <FaUpload /> Drop files here to upload. |
| 60 | + </TextWrapper> |
| 61 | + </VisualDropzone> |
| 62 | + )} |
| 63 | + </Root> |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +const Root = styled.div` |
| 68 | + height: 100%; |
| 69 | + position: relative; |
| 70 | +`; |
| 71 | + |
| 72 | +const fadeIn = keyframes` |
| 73 | + from { |
| 74 | + opacity: 0; |
| 75 | + backdrop-filter: blur(0px); |
| 76 | + } |
| 77 | + to { |
| 78 | + opacity: 1; |
| 79 | + backdrop-filter: blur(10px); |
| 80 | + } |
| 81 | +`; |
| 82 | + |
| 83 | +const VisualDropzone = styled.div` |
| 84 | + position: absolute; |
| 85 | + inset: 0; |
| 86 | + height: 90vh; |
| 87 | + background-color: ${p => |
| 88 | + p.theme.darkMode ? 'rgba(0, 0, 0, 0.8)' : 'rgba(255, 255, 255, 0.8)'}; |
| 89 | + backdrop-filter: blur(10px); |
| 90 | + border: 3px dashed ${p => p.theme.colors.textLight}; |
| 91 | + border-radius: ${p => p.theme.radius}; |
| 92 | + display: grid; |
| 93 | + place-items: center; |
| 94 | + font-size: 1.8rem; |
| 95 | + color: ${p => p.theme.colors.textLight}; |
| 96 | + animation: 0.1s ${fadeIn} ease-in; |
| 97 | +`; |
| 98 | + |
| 99 | +const TextWrapper = styled.div` |
| 100 | + display: flex; |
| 101 | + align-items: center; |
| 102 | + gap: 1rem; |
| 103 | + padding: ${p => p.theme.margin}rem; |
| 104 | +`; |
0 commit comments