Attachments
Files in a row that scrolls: an image as a small square of itself, anything else as a chip with its icon, name and details, each able to open and to be removed.
Installation
npx shadcn@latest add "@aiellie/attachments"shadcn needs the @aiellie registry in components.json first. npx aiellie add attachments adds it on its first run, then installs the same way.
Attachments is the row of files on a message: square thumbnails for pictures, chips with an icon, a name and a size for everything else. The row scrolls sideways when it runs out of room, fading at whichever edge has more. Each item can open, to a File preview, and be removed while the message is still a draft.
Getting started
Lay out the files
AttachmentImagetakes asrc, often an object URL;AttachmentFiletakes a Hugeicons glyph and a short description.components/message-files.tsximport { File02Icon } from "@hugeicons/core-free-icons" import { AttachmentFile, AttachmentImage, Attachments, } from "@/components/aiellie/attachments" export function MessageFiles() { return ( <Attachments> <AttachmentImage src="/mockup.png" name="mockup.png" /> <AttachmentFile icon={File02Icon} name="report.pdf" description="PDF · 1.2 MB" /> </Attachments> ) }Open and remove
onOpenmakes the whole item a button;onRemoveadds a small cross in its corner. LeaveonRemoveoff once the message is sent.<AttachmentFile icon={File02Icon} name={file.name} description={describe(item)} onOpen={() => preview(item)} onRemove={() => remove(item)} />
Anatomy
<div data-slot="attachments" data-size="default"> {/* scrolls sideways, snapping */}
<div data-slot="attachment-image"> {/* a 56px square */}
<img />
<button data-slot="attachment-open" /> {/* only with onOpen, covering it */}
<button data-slot="attachment-remove" /> {/* only with onRemove */}
</div>
<div data-slot="attachment-file"> {/* 56px tall, up to 224px wide */}
<span><svg /></span> {/* the icon on a muted square */}
<span>{name}<span>{description}</span></span>
</div>
</div>The remove button shows on hover and focus, and always on a touch screen, so it never covers a thumbnail someone is only looking at. Its label names the file: "Remove report.pdf". Pressing an item that opens shrinks it a little. The edges fade only while there is more to scroll that way, with a mask, so the row works on any background.
size="sm" makes items 40px, for a composer where room is tight.
Examples
From a file input
Add some files: pictures get thumbnails, the rest get chips.
API reference
Attachments
| Prop | Type | Default | Description |
|---|---|---|---|
size | "default" | "sm" | "default" | 56px squares and chips, or 40px where room is tight. |
className | string | Merged onto the row. |
All other div props are forwarded to the row.
AttachmentImage
| Prop | Type | Default | Description |
|---|---|---|---|
src | string | required | The picture. |
name | string | required | Read out in place of the picture, which shows no name of its own. |
onOpen | () => void | Makes it a button that opens it. | |
onRemove | () => void | Adds a remove button. |
AttachmentFile
| Prop | Type | Default | Description |
|---|---|---|---|
icon | IconSvgElement | required | A Hugeicons glyph for its kind. |
name | string | required | The file's name. |
description | string | Its kind and size, like PDF · 1.2 MB. | |
onOpen | () => void | Makes it a button that opens it. | |
onRemove | () => void | Adds a remove button. |