react-garden
Version:
React + TypeScript + ThreeJS app using Material UI on NextJS, Apollo Client, GraphQL + WordPress REST APIs, for ThreeD web development.. a part of the threed.ai code family.
79 lines (68 loc) • 2.35 kB
JavaScript
// ** React Imports
import { useState } from 'react'
// ** MUI Imports
import Box from '@mui/material/Box'
import Link from '@mui/material/Link'
import { styled } from '@mui/material/styles'
import Typography from '@mui/material/Typography'
// ** Third Party Imports
import { useDropzone } from 'react-dropzone'
// Styled component for the upload image inside the dropzone area
const Img = styled('img')(({ theme }) => ({
[theme.breakpoints.up('md')]: {
marginRight: theme.spacing(15.75)
},
[theme.breakpoints.down('md')]: {
marginBottom: theme.spacing(4)
},
[theme.breakpoints.down('sm')]: {
width: 160
}
}))
// Styled component for the heading inside the dropzone area
const HeadingTypography = styled(Typography)(({ theme }) => ({
marginBottom: theme.spacing(5),
[theme.breakpoints.down('sm')]: {
marginBottom: theme.spacing(4)
}
}))
const FileUploaderSingle = () => {
// ** State
const [files, setFiles] = useState([])
// ** Hook
const { acceptedFiles, getRootProps, getInputProps } = useDropzone({
multiple: false,
accept: {
'image/*': ['.png', '.jpg', '.jpeg', '.gif']
},
onDrop: acceptedFiles => {
setFiles(acceptedFiles.map(file => Object.assign(file)))
}
})
const handleLinkClick = event => {
event.preventDefault()
}
const img = files.map(file => (
<img key={file.name} alt={file.name} className='single-file-image' src={URL.createObjectURL(file)} />
))
return (
<Box {...getRootProps({ className: 'dropzone' })} sx={acceptedFiles.length ? { height: 450 } : {}}>
<input {...getInputProps()} />
<Box sx={{ display: 'flex', flexDirection: ['column', 'column', 'row'], alignItems: 'center' }}>
<Img alt='Upload img' src='/images/misc/upload.png' />
<Box sx={{ display: 'flex', flexDirection: 'column', textAlign: ['center', 'center', 'inherit'] }}>
<HeadingTypography variant='h5'>Drop files here or click to upload.</HeadingTypography>
<Typography color='textSecondary'>
Drop files here or click{' '}
<Link href='/' onClick={handleLinkClick}>
browse
</Link>{' '}
thorough your machine
</Typography>
</Box>
</Box>
{files.length ? img : null}
</Box>
)
}
export default FileUploaderSingle