Molecules · Attachments
Attachments
The files attached to one item: a grid of tiles, each with a preview for an image, the name as a link, the size and a Remove button, and a drop zone with a Choose files button. An image pasted while focus is in the component is added too. It never uploads anything itself: it hands you the files, and you pass back what is attached.
- Best for
- Files attached to one record: screenshots on a ticket, a contract on a deal, receipts on an expense.
- Not for
- A file that is one field of a form with a Save (a profile photo): use a labelled file input in a FormField.
This page as Markdown, a plain-text copy for a coding agent (an AI tool that writes code).
Examples
Add by dropping, choosing or pasting; remove from the tile
- gate-2-error.pdf180 KB
- firmware-notes.txt2.1 KB
Attachments: Drop files here, paste an image, or
Code
tsx
const START: Attachment[] = [ { id: 'a1', name: 'gate-2-error.pdf', size: 184_320, type: 'application/pdf' }, { id: 'a2', name: 'firmware-notes.txt', size: 2_140, type: 'text/plain' },]function LiveAttachmentsExample() { const [files, setFiles] = useState(START) return ( <Attachments className="max-w-2xl" label="Attachments" files={files} maxSize={10 * 1024 * 1024} onAdd={added => setFiles(current => [ ...current, ...added.map(file => ({ id: `${file.name}-${file.lastModified}`, name: file.name, size: file.size, type: file.type, href: URL.createObjectURL(file), })), ])} onRemove={id => setFiles(current => current.filter(file => file.id !== id))} /> )}<LiveAttachmentsExample />Uploading, failed and read-only
- Uploading depot-count.csvdepot-count.csvUploading
- shelf-photo.heic3.2 MBThis type cannot be previewed or opened here.
Code
tsx
<Attachments className="max-w-2xl" label="Attachments on R-1042" files={[ { id: 'u1', name: 'depot-count.csv', size: 0, status: 'uploading' }, { id: 'u2', name: 'shelf-photo.heic', size: 3_400_000, status: 'error', error: 'This type cannot be previewed or opened here.' }, ]}/>Anatomy
| Part | What it is |
|---|---|
| List | A ul named by label, laid out as a grid of tiles at least 11rem wide. |
| Tile | A preview area on surface.sunken (the image, a file icon, or a Spinner while uploading), then the name, the size in the data font, and any error in accent.hostile.text. |
| Remove | A ghost icon button on each tile, named "Remove <file name>". |
| Drop zone | A dashed area with the Choose files button. Its border turns accent.signal.indicator while files are dragged over it. |
| Refusals | A polite live region under the zone that names each file it refused, and why: the wrong type, or too large. |
Props
- labelstring
- Required. Names the list and the drop zone: "Attachments".
- filesreadonly Attachment[]
- Required. { id, name, size, type?, href?, previewSrc?, status?, error? }. size is in bytes; status is 'uploading' or 'error'. An image type previews from previewSrc, or from href.
- onAdd(files: File[]) => void
- Turns on the drop zone, Choose files and paste. Gets only the files that passed accept and maxSize. Upload them, and add them to files.
- onRemove(id: string) => void
- Turns on a Remove button on each file.
- acceptstring
- The file types it takes, as the accept attribute: "image/*,.pdf". Also checked on dropped and pasted files.
- maxSizenumber
- The largest file it takes, in bytes. A larger one is refused with a message.
- dropLabelstringDefault 'Drop files here, paste an image, or'
- The words in the drop zone, before the button.
- browseLabelstringDefault 'Choose files'
- The button's words.
- emptyMessagestringDefault 'No attachments'
- Shown when nothing is attached and nothing can be added (no onAdd).
- renderLink(props: { href, className, children }) => ReactNodeDefault an anchor that opens a new tab
- How a file's name links to it.
- classNamestring
- Merged with the root classes. When Tailwind utility classes clash, the later one wins.
- refRef<HTMLDivElement>
- The root div.
- formatBytes()(bytes: number) => string
- 1536 becomes "1.5 KB". Binary units, one decimal under 10.
- acceptsFile()(file: { name, type }, accept?: string) => boolean
- Whether a file matches an accept string: extensions, exact types and type/* wildcards.
Keyboard
| Keys | Result |
|---|---|
| Tab | Moves through each file's link and Remove button, then Choose files. |
| CtrlV | With focus in the component but not in a text field, adds the pasted images. |
Do and don't
Do
- Show an upload in progress as a file with status 'uploading', so it has its place in the grid at once.
- Set maxSize to what your server takes, so a refusal happens before the upload.
- Say in the page what happens to attached files, if someone other than the person reads them (an agent, a reviewer).
Don't
- Don't use it for a single field in a form with a Save. That is a file input in a FormField.
- Don't remove a file without a way back if it was the only copy. Keep it until the person saves, or offer Undo in a Toast.