Components
File dropzone
Takes one file, by a click or by drag and drop, such as a CV.
'use client'
import { useState } from 'react'
import { Field, FieldLabel } from '@dex/shared/components/field'
import { FileDropzone } from '@dex/shared/components/file-dropzone'
export function FileDropzoneDefault() {
const [file, setFile] = useState<File | null>(null)
const [isUploading, setIsUploading] = useState(false)
const idleOrDone = file ? 'done' : 'idle'
// Stands in for the upload request.
const upload = (chosen: File) => {
setFile(chosen)
setIsUploading(true)
window.setTimeout(() => setIsUploading(false), 1500)
}
return (
<Field className="max-w-md">
<FieldLabel htmlFor="dropzone-cv">CV</FieldLabel>
<FileDropzone
status={isUploading ? 'uploading' : idleOrDone}
inputId="dropzone-cv"
accept={['.pdf', '.docx']}
idleLabel="Click to upload or drag and drop"
hintLabel="PDF or DOCX, max 10 MB"
uploadingLabel="Uploading your CV…"
fileName={file?.name}
fileSize={file ? `${(file.size / 1_000_000).toFixed(1)} MB` : undefined}
replaceLabel="Upload a new CV"
onFileSelect={upload}
/>
</Field>
)
}
Uploading your CV…sam-okafor-cv.pdf
'use client'
import { useState } from 'react'
import { Field, FieldLabel } from '@dex/shared/components/field'
import { FileDropzone } from '@dex/shared/components/file-dropzone'
export function FileDropzoneUploading() {
const [fileName, setFileName] = useState('sam-okafor-cv.pdf')
return (
<Field className="max-w-md">
<FieldLabel htmlFor="dropzone-uploading">CV</FieldLabel>
<FileDropzone
status="uploading"
inputId="dropzone-uploading"
accept={['.pdf', '.docx']}
idleLabel="Click to upload or drag and drop"
uploadingLabel="Uploading your CV…"
fileName={fileName}
onFileSelect={(file) => setFileName(file.name)}
/>
</Field>
)
}
'use client'
import { useState } from 'react'
import { Field, FieldLabel } from '@dex/shared/components/field'
import { FileDropzone } from '@dex/shared/components/file-dropzone'
export function FileDropzoneUploaded() {
const [fileName, setFileName] = useState('sam-okafor-cv.pdf')
return (
<Field className="max-w-md">
<FieldLabel htmlFor="dropzone-uploaded">CV</FieldLabel>
<FileDropzone
status="done"
inputId="dropzone-uploaded"
accept={['.pdf', '.docx']}
idleLabel="Click to upload or drag and drop"
uploadingLabel="Uploading your CV…"
fileName={fileName}
replaceLabel="Upload a new CV"
onFileSelect={(file) => setFileName(file.name)}
/>
</Field>
)
}
sam-okafor-portfolio.pdf · 4.8 MB
'use client'
import { useState } from 'react'
import { Field, FieldLabel } from '@dex/shared/components/field'
import { FileDropzone } from '@dex/shared/components/file-dropzone'
export function FileDropzoneWithRemove() {
const [fileName, setFileName] = useState<string | null>('sam-okafor-portfolio.pdf')
return (
<Field className="max-w-md">
<FieldLabel htmlFor="dropzone-remove" optional>
Portfolio
</FieldLabel>
<FileDropzone
status={fileName ? 'done' : 'idle'}
inputId="dropzone-remove"
accept={['.pdf']}
idleLabel="Click to upload or drag and drop"
hintLabel="PDF, max 10 MB"
uploadingLabel="Uploading your portfolio…"
fileName={fileName}
fileSize="4.8 MB"
onFileSelect={(file) => setFileName(file.name)}
onRemove={() => setFileName(null)}
removeLabel="Remove portfolio"
/>
</Field>
)
}
Upload a PDF or DOCX under 10 MB.
'use client'
import { useState } from 'react'
import { Field, FieldControl, FieldLabel, FieldMessage } from '@dex/shared/components/field'
import { FileDropzone } from '@dex/shared/components/file-dropzone'
const MAX_BYTES = 10_000_000
const REJECTED = 'Upload a PDF or DOCX under 10 MB.'
export function FileDropzoneInvalid() {
const [fileName, setFileName] = useState<string | null>(null)
const [error, setError] = useState<string | null>(REJECTED)
// The dropzone hands over whatever was chosen. Checking it is the form's job.
const check = (file: File) => {
const isAccepted = /\.(pdf|docx)$/i.test(file.name) && file.size <= MAX_BYTES
setFileName(isAccepted ? file.name : null)
setError(isAccepted ? null : REJECTED)
}
return (
<Field invalid={Boolean(error)} className="max-w-md">
<FieldLabel>CV</FieldLabel>
<FieldControl>
<FileDropzone
status={fileName ? 'done' : 'idle'}
accept={['.pdf', '.docx']}
idleLabel="Click to upload or drag and drop"
hintLabel="PDF or DOCX, max 10 MB"
uploadingLabel="Uploading your CV…"
fileName={fileName}
replaceLabel="Upload a new CV"
onFileSelect={check}
/>
</FieldControl>
<FieldMessage error={error} />
</Field>
)
}
'use client'
import { useState } from 'react'
import { Field, FieldLabel } from '@dex/shared/components/field'
import { FileDropzone } from '@dex/shared/components/file-dropzone'
export function FileDropzoneDisabled() {
const [fileName, setFileName] = useState<string | null>(null)
return (
<Field disabled className="max-w-md">
<FieldLabel htmlFor="dropzone-disabled">CV</FieldLabel>
<FileDropzone
status={fileName ? 'done' : 'idle'}
inputId="dropzone-disabled"
accept={['.pdf', '.docx']}
idleLabel="Click to upload or drag and drop"
hintLabel="PDF or DOCX, max 10 MB"
uploadingLabel="Uploading your CV…"
fileName={fileName}
disabled
onFileSelect={(file) => setFileName(file.name)}
/>
</Field>
)
}
- The dropzone only draws the three states. The form holds
status, sends the file and checks its size and type, sinceacceptonly filters the file picker and a dropped file can be anything. - Say what is accepted in
hintLabel. When a file is rejected, setinvalidand show a message under the dropzone that says what to upload: "Upload a PDF or DOCX under 10 MB." - Every state is the same box, at least 120px tall with its content centred, so the form below does not move as a file goes from chosen to uploaded.
- Uploading shows a spinner and no percentage, because nothing reports progress.
- Pass
replaceLabelwhen a new file can take the uploaded one's place, andonRemovewhen the file can be taken away. - Give
inputIdan id that is unique on the page, and point the field's label at it withhtmlFor.