Attachment
A file drawn as a card: its preview, name and size, with upload progress or an error in place of the size while it transfers.
Usage
import {
Attachment,
AttachmentActions,
AttachmentContent,
AttachmentMeta,
AttachmentName,
AttachmentPreview,
AttachmentProgress,
AttachmentRemove,
} from 'nyctoui/attachment'
<Attachment status="uploading">
<AttachmentPreview />
<AttachmentContent>
<AttachmentName>report.pdf</AttachmentName>
<AttachmentProgress value={62} />
</AttachmentContent>
<AttachmentActions>
<AttachmentRemove aria-label="Cancel report.pdf" />
</AttachmentActions>
</Attachment>Attachment draws a file; it doesn’t upload one. Pair it with File Upload to show the files someone chose, or use it alone for files already sent. AttachmentProgress takes the place of AttachmentMeta at the same height, so the card doesn’t move as an upload starts and ends.
Good for
- Files attached to a message in a chat or email composer, each with its upload progress.
- Receipts on an expense report, with a thumbnail and the file size.
- Contracts and briefs on a project page, linking to the stored file.
- Files sent in a support ticket thread, after the upload finished.
Examples
States
The demo at the top of the page. status sets data-status; at error the preview and meta turn red, and uploading marks the card busy. The actions are yours: here a retry for the failed file.
Image preview
Give AttachmentPreview a src for a thumbnail. For a file someone just chose, create an object URL and revoke it when the card goes away, as the File Upload demo does.
As a link
Pass render to make the whole card a link to the file. It takes the row hover and the focus ring.
Best practices
- Pair it with File Upload for files someone just chose, and use it alone for files already stored.
- Show progress with
AttachmentProgresswhile the file transfers; it takes the size’s place at the same height, so the card holds still. - Give an image an
AttachmentPreviewwith asrc, and let other files show their type icon. - Say what went wrong when an upload fails, such as “Too large” or “Network error”, and offer a retry or remove action.
- Expect it to upload anything. It draws the file; your app moves the bytes.
- Leave
AttachmentRemovewithout anaria-labelthat names the file, such as “Remove invoice.pdf”, when several cards sit together. - Use it for a gallery of photos. A card per file is for documents and a handful of images, not a camera roll.
API reference
Attachment
The card, a <div> by default.
| Prop | Type | Default |
|---|---|---|
statusSets data-status. error turns the preview and meta red; uploading sets aria-busy. | 'idle' | 'uploading' | 'complete' | 'error' | 'idle' |
renderAnother element to render, such as a link. | ReactElement | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
AttachmentPreview
The 32px tile at the start. Decorative.
| Prop | Type | Default |
|---|---|---|
srcAn image to show in place of the icon. | string | — |
childrenA 16px icon for the file’s type. | ReactNode | file icon |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
AttachmentContent
Stacks the name over the meta or progress.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
AttachmentName
The file name on one line, with an ellipsis.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
AttachmentMeta
Size, type or an error, in 12px text.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
AttachmentProgress
A Progress track and percentage on the meta line. Takes Progress’s props.
| Prop | Type | Default |
|---|---|---|
valueHow far the upload has got, 0 to 100. null while unknown. | number | null | — |
showValueShows the percentage after the track. | boolean | true |
aria-labelWhat screen readers call the bar. | string | 'Upload progress' |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
AttachmentActions
Buttons at the end of the card.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
AttachmentRemove
A small ghost Button with an ×. Takes Button’s props; name it for the file.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |