aiellie

Search

Find a component, template or page

GitHub
Components

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.

mockup.png
photo.jpg
report.pdfPDF · 1.2 MB
src24 files
mockup.png
photo.jpg
report.pdfPDF · 1.2 MB
src24 files
Open full screen
fig. 01 · open it full screen from the corner

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

  1. Lay out the files

    AttachmentImage takes a src, often an object URL; AttachmentFile takes a Hugeicons glyph and a short description.

    components/message-files.tsx
    import { 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>
      )
    }
  2. Open and remove

    onOpen makes the whole item a button; onRemove adds a small cross in its corner. Leave onRemove off 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

PropTypeDefaultDescription
size"default" | "sm""default"56px squares and chips, or 40px where room is tight.
classNamestringMerged onto the row.

All other div props are forwarded to the row.

AttachmentImage

PropTypeDefaultDescription
srcstringrequiredThe picture.
namestringrequiredRead out in place of the picture, which shows no name of its own.
onOpen() => voidMakes it a button that opens it.
onRemove() => voidAdds a remove button.

AttachmentFile

PropTypeDefaultDescription
iconIconSvgElementrequiredA Hugeicons glyph for its kind.
namestringrequiredThe file's name.
descriptionstringIts kind and size, like PDF · 1.2 MB.
onOpen() => voidMakes it a button that opens it.
onRemove() => voidAdds a remove button.