Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Attachment for Astro

Astro attachment parts for file and image previews with upload state and actions.

sales-dashboard.pdfPDF · 2.4 MB

Installation

bunx --bun shadcn@latest add @gnostic/attachment

The Attachment parts display a file or image attachment, its media, name, and metadata, with optional actions and upload state. Use them for files and images in chat composers, message threads, and upload lists.

Usage

import {
    Attachment,
    AttachmentAction,
    AttachmentActions,
    AttachmentContent,
    AttachmentDescription,
    AttachmentMedia,
    AttachmentTitle,
} from "@/components/ui/attachment"
---
import FileText from "@lucide/astro/icons/file-text"
import X from "@lucide/astro/icons/x"

import {
  Attachment,
  AttachmentAction,
  AttachmentActions,
  AttachmentContent,
  AttachmentDescription,
  AttachmentMedia,
  AttachmentTitle,
} from "@/components/ui/attachment"
---

<Attachment>
  <AttachmentMedia>
    <FileText />
  </AttachmentMedia>
  <AttachmentContent>
    <AttachmentTitle>sales-dashboard.pdf</AttachmentTitle>
    <AttachmentDescription>PDF · 2.4 MB</AttachmentDescription>
  </AttachmentContent>
  <AttachmentActions>
    <AttachmentAction aria-label="Remove sales-dashboard.pdf">
      <X />
    </AttachmentAction>
  </AttachmentActions>
</Attachment>

Composition

Attachment
├── AttachmentMedia
├── AttachmentContent
│   ├── AttachmentTitle
│   └── AttachmentDescription
├── AttachmentActions
│   └── AttachmentAction
└── AttachmentTrigger

Use AttachmentGroup to lay out multiple attachments in a scrollable row:

AttachmentGroup
├── Attachment
└── Attachment

Image

Set variant="image" on AttachmentMedia and render an <img> or Image inside it. Use orientation="vertical" to stack the media above the content.

Futuristic storm
futuristic-storm.pngPNG · 820 KB
Winter landscape
winter-landscape-orion.pngPNG · 820 KB
Winter landscape
winter-landscape-theta.pngPNG · 820 KB

States

Set state on Attachment to reflect the upload lifecycle. uploading and processing shimmer the title, and error switches to a destructive treatment. Keep the failure reason in AttachmentDescription so the state is not conveyed by color alone.

sales-dashboard.pdfUploading · 64%
message-renderer.tsxProcessing…
desk-reference.jpgUpload failed · try again
message-renderer.tsxTypeScript · 12 KB

Sizes

Use size to switch between default, sm, and xs.

sales-dashboard.pdfPDF · 2.4 MB
sales-dashboard.pdfPDF · 2.4 MB
sales-dashboard.pdfPDF · 2.4 MB

Group

Wrap attachments in AttachmentGroup to lay them out in a horizontally scrollable, snapping row with an edge fade.

workspace.pngPNG · 820 KB
research-summary.pdfPDF · 1.8 MB
release-notes.mdMarkdown · 24 KB

Trigger

Add an AttachmentTrigger to make the whole card open a link or dialog. It fills the card behind the actions, so the actions stay clickable. Give icon-only actions and the trigger an aria-label that names the file and the action.

Attention is All You NeedPDF · arXiv:1706.03762

For a dialog or lightbox, put the attachment inside its root and set data-slot="dialog-trigger" on AttachmentTrigger. The dialog controller binds the trigger within that root. No ID lookup or custom click handler is needed.

<Dialog>
  <Attachment>
    <!-- media, content, actions -->
    <AttachmentTrigger
      data-slot="dialog-trigger"
      aria-label="Preview research-summary.pdf"
    />
  </Attachment>
  <DialogContent>{/* ... */}</DialogContent>
</Dialog>

API Reference

AttachmentProps

PropType
state?AttachmentState

Upload state. Drives border treatment and title shimmer while in progress.

TypeAttachmentState
Defaultdone
size?'default' | 'sm' | 'xs'

Attachment density.

Type'default' | 'sm' | 'xs'
Defaultdefault
orientation?'horizontal' | 'vertical'

Lay media beside or above the text content.

Type'horizontal' | 'vertical'
Defaulthorizontal

AttachmentMediaProps

PropType
variant?'icon' | 'image'

Whether the slot holds an icon or an image preview.

Type'icon' | 'image'
Defaulticon

AttachmentActionProps

PropType
size?ButtonProps['size']

Button size for the icon action.

TypeButtonProps['size']
Defaulticon-xs

Read the installed source for the complete prop types.

Theme and layout

Attachment uses shared component recipes and semantic colors. Choose the size and orientation that fit the surrounding form or file list. Keep file names readable and actions distinct from the preview. Use Field for input labels and messages; the attachment view does not perform an upload.