File upload
A recipe, not a package namespace. A native <input type="file"> is the control, useDropzone adds drag and drop and file checks, and your code owns the upload. There is no FileUpload.* export.
On this page
Images
The label is the drop target, so a click anywhere on it opens the picker and its text names the input. Rejected files are listed in an ErrorMessage.Root that the input's aria-describedby points to, and the input gets aria-invalid. Accepted files are listed below it.
import {
Description,
ErrorMessage,
Stack,
Text,
useDropzone,
type FileRejection,
type FileRejectionReason,
} from '@tounsoo/todae';
import { useId, useState } from 'react';
const MAX_SIZE = 2_000_000;
const REASONS: Record<FileRejectionReason, string> = {
type: 'is not an image',
'too-large': 'is over 2 MB',
'too-small': 'is empty',
'too-many': 'is past the 3 file limit',
};
export default function FileUploadImages() {
const hintId = useId();
const errorId = useId();
const [files, setFiles] = useState<File[]>([]);
const [rejected, setRejected] = useState<FileRejection[]>([]);
const zone = useDropzone({
accept: 'image/*',
multiple: true,
maxFiles: 3,
maxSize: MAX_SIZE,
minSize: 1,
onFiles: (accepted, failed) => {
setFiles(accepted);
setRejected(failed);
},
});
return (
<Stack.Root>
<label
{...zone.getRootProps()}
style={{
display: 'grid',
justifyItems: 'center',
gap: 'var(--todae-space-3)',
padding: 'var(--todae-space-6)',
// A solid border while dragging, so the state shows by more than color.
border: `2px ${zone.dragging ? 'solid' : 'dashed'} var(--todae-color-border-default)`,
borderRadius: 'var(--todae-radius-md)',
background: zone.dragging ? 'var(--todae-color-bg-subtle)' : undefined,
}}
>
<Text.Root as="span">Drop images here, or choose them</Text.Root>
<input
{...zone.getInputProps()}
name="images"
aria-invalid={rejected.length > 0 || undefined}
aria-describedby={rejected.length > 0 ? `${hintId} ${errorId}` : hintId}
/>
</label>
<Description.Root id={hintId}>Up to 3 images, 2 MB each.</Description.Root>
{rejected.length > 0 ? (
<ErrorMessage.Root id={errorId}>
{rejected.map(({ file, reasons }) => `${file.name} ${reasons.map((reason) => REASONS[reason]).join(' and ')}.`).join(' ')}
</ErrorMessage.Root>
) : null}
{files.length > 0 ? (
<ul aria-label="Chosen images">
{files.map((file, index) => (
<li key={`${index}:${file.name}`}>{file.name}</li>
))}
</ul>
) : null}
</Stack.Root>
);
}
Style the drop state with [data-dragging] or the dragging value. Errors are not announced when they appear; use useAnnouncer if they need to be.
Uploading
Send the files from onFiles, or leave them in the input and let the form submit them: the input holds only the accepted files. Show progress with Progress.Root, as on the Progress page.
Accessible composition
Todae handles: useDropzone keeps the native <input type="file"> as the control, so its role, the keyboard and the picker come from the browser. getInputProps() sets type, accept, multiple and disabled, and getRootProps() adds only drag handlers, a paste handler with paste, and data-dragging. After the checks, the input holds only the accepted files. The hook sets no ARIA and announces nothing.
You supply: What the recipe's markup does: the <label> text that names the input, a hint joined with aria-describedby, rejection reasons written out in words, aria-invalid and the ErrorMessage.Root id added to aria-describedby when files are rejected, and a labelled list of the accepted files. An announcement of rejections, if they must be heard. Upload progress and results in text, such as a <progress> with a label.
Watch for: Hiding the input with display: none or hidden to style the drop target: dragging is pointer-only, so keyboard and screen reader users lose the only way in. Keep it focusable and show its focus. An aria-describedby that still points at an ErrorMessage.Root you no longer render. Errors shown by a red border alone.
When to use
When: A form that takes files, picked or dropped, with checks on type, size and count.
When not: Files attached to a chat message → Composer and Attachment. A picker with no drop target and no checks → <input type="file"> alone.