---
title: Attachment for Astro
description: "Astro attachment parts for file and image previews with upload state and actions."
seo:
    title: Attachment for Astro | Shadcn & Tailwind CSS
related:
    - /components/button
    - /components/item
sidebar:
    label: "Attachment"
---

```astro
---
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"
---

<div class="flex w-full max-w-sm flex-col items-center gap-4">
  <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>
</div>
```

## Installation

### Command

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

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

No extra packages are required.

#### src/components/ui/attachment/attachment-action.astro

```astro
---
/**
 * Icon or text action on an attachment. Renders a ghost {@link Button} by default.
 */
import type { ComponentProps } from "astro/types"

import { cn } from "cn"
import { Button } from "../button/index.ts"
import type { AttachmentActionProps } from "./types.ts"

type Props = ComponentProps<typeof Button> & AttachmentActionProps

const {
  class: className,
  variant = "ghost",
  size = "icon-xs",
  ...props
} = Astro.props
---

<Button
  data-slot="attachment-action"
  variant={variant}
  size={size}
  class={cn(className)}
  {...props}
>
  <slot />
</Button>
```

#### src/components/ui/attachment/attachment-actions.astro

```astro
---
/**
 * Aligns one or more attachment actions at the end of the card.
 */
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="attachment-actions"
  class={cn("group-data-[orientation=vertical]/attachment:absolute group-data-[orientation=vertical]/attachment:top-3 group-data-[orientation=vertical]/attachment:right-3 relative z-20 group-data-[orientation=vertical]/attachment:gap-1 flex shrink-0 items-center", className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/attachment/attachment-content.astro

```astro
---
/**
 * Groups the attachment title and description.
 */
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="attachment-content"
  class={cn("leading-tight group-data-[orientation=vertical]/attachment:px-1 max-w-full min-w-0 flex-1", className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/attachment/attachment-description.astro

```astro
---
/**
 * Secondary metadata for an attachment, such as type, size, or status text.
 */
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"span">

const { class: className, ...props } = Astro.props
---

<span
  data-slot="attachment-description"
  class={cn(
    "mt-0.5 text-xs block max-w-full min-w-0 truncate text-muted-foreground group-data-[state=error]/attachment:text-destructive/80",
    className,
  )}
  {...props}
>
  <slot />
</span>
```

#### src/components/ui/attachment/attachment-group.astro

```astro
---
/**
 * Horizontally scrollable row of attachments with snap points and edge fade.
 */
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="attachment-group"
  class={cn(
    "gap-3 scroll-px-1 py-1 flex min-w-0 scroll-fade-x snap-x snap-mandatory scrollbar-none overflow-x-auto overscroll-x-contain *:data-[slot=attachment]:flex-none *:data-[slot=attachment]:snap-start",
    className,
  )}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/attachment/attachment-media.astro

```astro
---
/**
 * Media slot for an icon, spinner, or image preview on an attachment.
 */
import type { HTMLAttributes } from "astro/types"
import type { VariantProps } from "class-variance-authority"

import { cn } from "cn"

import { attachmentMediaVariants } from "./attachment-variants.ts"
import type { AttachmentMediaProps } from "./types.ts"

type Props = HTMLAttributes<"div"> &
  AttachmentMediaProps &
  VariantProps<typeof attachmentMediaVariants>

const { class: className, variant = "icon", ...props } = Astro.props
---

<div
  data-slot="attachment-media"
  data-variant={variant}
  class={cn(attachmentMediaVariants({ variant }), className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/attachment/attachment-title.astro

```astro
---
/**
 * Primary label for an attachment, usually the file name.
 * Shimmers while the root attachment is uploading or processing.
 */
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"span">

const { class: className, ...props } = Astro.props
---

<span
  data-slot="attachment-title"
  class={cn(
    "font-medium block max-w-full min-w-0 truncate group-data-[state=processing]/attachment:shimmer group-data-[state=uploading]/attachment:shimmer",
    className,
  )}
  {...props}
>
  <slot />
</span>
```

#### src/components/ui/attachment/attachment-trigger.astro

```astro
---
/**
 * Full-card control behind attachment actions. Use `as` for a link or other element.
 */
import type { HTMLAttributes } from "astro/types"
import type { HTMLTag, Polymorphic } from "astro/types"

import { cn } from "cn"

type Props<T extends HTMLTag = "button"> = Polymorphic<{ as: T }> &
  HTMLAttributes<T>

const {
  as: Tag = "href" in Astro.props ? "a" : "button",
  class: className,
  ...props
} = Astro.props

const elementProps: Record<string, unknown> = { ...props }
elementProps.type = Tag === "button" ? elementProps.type || "button" : undefined
---

<Tag
  data-slot="attachment-trigger"
  class={cn("absolute inset-0 z-10 outline-none", className)}
  {...elementProps}
>
  <slot />
</Tag>
```

#### src/components/ui/attachment/attachment-variants.ts

```ts
import { cva } from 'class-variance-authority'

/** Root attachment layout, size, and orientation recipes. */
export const attachmentVariants = cva("relative flex w-fit max-w-full min-w-0 shrink-0 flex-wrap rounded-2xl border bg-card text-card-foreground transition-colors focus-within:ring-1 focus-within:ring-ring/30 has-[>a,>button]:hover:bg-muted/50 data-[state=error]:border-destructive/30 data-[state=idle]:border-dashed group/attachment", {
    defaultVariants: {
        orientation: 'horizontal',
        size: 'default',
    },
    variants: {
        orientation: {
            horizontal: "min-w-40 items-center",
            vertical: "w-24 has-data-[slot=attachment-content]:w-30 flex-col",
        },
        size: {
            default: "gap-2 has-data-[slot=attachment-content]:px-2.5 has-data-[slot=attachment-content]:py-2 has-data-[slot=attachment-media]:p-2 text-sm",
            sm: "gap-2.5 has-data-[slot=attachment-content]:px-2 has-data-[slot=attachment-content]:py-1.5 has-data-[slot=attachment-media]:p-1.5 text-xs",
            xs: "gap-1.5 has-data-[slot=attachment-content]:px-1.5 has-data-[slot=attachment-content]:py-1 has-data-[slot=attachment-media]:p-1 text-xs rounded-xl",
        },
    },
})

/** Media slot recipes for icon and image attachments. */
export const attachmentMediaVariants = cva(
    "bg-muted text-foreground w-10 rounded-lg group-data-[size=sm]/attachment:w-8 group-data-[size=xs]/attachment:w-7 group-data-[size=xs]/attachment:rounded-md [&_svg:not([class*='size-'])]:size-4 group-data-[size=xs]/attachment:[&_svg:not([class*='size-'])]:size-3.5 group-data-[orientation=vertical]/attachment:w-full group-data-[orientation=vertical]/attachment:[&_svg:not([class*='size-'])]:size-6 group-data-[orientation=vertical]/attachment:*:data-[slot=spinner]:size-6! group-data-[state=error]/attachment:bg-destructive/10 group-data-[state=error]/attachment:text-destructive relative flex aspect-square shrink-0 items-center justify-center overflow-hidden [&_svg]:pointer-events-none",
    {
        defaultVariants: {
            variant: 'icon',
        },
        variants: {
            variant: {
                icon: '',
                image: "opacity-60 group-data-[state=idle]/attachment:opacity-100 group-data-[state=done]/attachment:opacity-100 *:[img]:aspect-square *:[img]:w-full *:[img]:object-cover",
            },
        },
    },
)
```

#### src/components/ui/attachment/attachment.astro

```astro
---
/**
 * Root container for a file or image attachment.
 * Compose media, content, actions, and an optional full-card trigger inside it.
 *
 * @see https://ui.shadcn.com/docs/components/base/attachment
 */
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

import { attachmentVariants } from "./attachment-variants.ts"
import type { AttachmentProps } from "./types.ts"

type Props = HTMLAttributes<"div"> & AttachmentProps

const {
  class: className,
  state = "done",
  size = "default",
  orientation = "horizontal",
  ...props
} = Astro.props
---

<div
  data-slot="attachment"
  data-state={state}
  data-size={size}
  data-orientation={orientation}
  class={cn(attachmentVariants({ orientation, size }), className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/attachment/index.ts

```ts
/** Attachment parts follow https://ui.shadcn.com/docs/components/base/attachment */
export { default as Attachment } from './attachment.astro'
export { default as AttachmentAction } from './attachment-action.astro'
export { default as AttachmentActions } from './attachment-actions.astro'
export { default as AttachmentContent } from './attachment-content.astro'
export { default as AttachmentDescription } from './attachment-description.astro'
export { default as AttachmentGroup } from './attachment-group.astro'
export { default as AttachmentMedia } from './attachment-media.astro'
export { default as AttachmentTitle } from './attachment-title.astro'
export { default as AttachmentTrigger } from './attachment-trigger.astro'

export type {
    AttachmentActionProps,
    AttachmentMediaProps,
    AttachmentProps,
    AttachmentState,
} from './types.ts'
```

#### src/components/ui/attachment/types.ts

```ts
import type { ButtonProps } from '../button/types.ts'

/** Upload lifecycle state for an attachment card. */
export type AttachmentState =
    | 'idle'
    | 'uploading'
    | 'processing'
    | 'error'
    | 'done'

/**
 * Props for the root attachment container.
 * HTML attributes such as `class` and `id` pass through on the element.
 */
export interface AttachmentProps {
    /**
     * Upload state. Drives border treatment and title shimmer while in progress.
     *
     * @default done
     */
    state?: AttachmentState
    /**
     * Attachment density.
     *
     * @default default
     */
    size?: 'default' | 'sm' | 'xs'
    /**
     * Lay media beside or above the text content.
     *
     * @default horizontal
     */
    orientation?: 'horizontal' | 'vertical'
}

/**
 * Props for the attachment media slot.
 * HTML attributes such as `class` pass through on the element.
 */
export interface AttachmentMediaProps {
    /**
     * Whether the slot holds an icon or an image preview.
     *
     * @default icon
     */
    variant?: 'icon' | 'image'
}

/**
 * Props for an attachment action control.
 * Button attributes such as `aria-label`, `variant`, and `disabled` pass through.
 */
export interface AttachmentActionProps {
    /**
     * Button size for the icon action.
     *
     * @default icon-xs
     */
    size?: ButtonProps['size']
}
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


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

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

```astro
---
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

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

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

```text
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.

```astro
---
import X from "@lucide/astro/icons/x"
import { Image } from "astro:assets"

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

import winterLandscapeOrion from "@/assets/images/winter-landscape-orion.png"
import winterLandcapeTheta from "@/assets/images/winter-landscape-theta.png"
import futuristicStorm from "@/assets/images/futuristic-storm.png"

const images = [
  {
    src: futuristicStorm,
    alt: "Futuristic storm",
    title: "futuristic-storm.png",
    description: "PNG · 820 KB",
  },
  {
    src: winterLandscapeOrion,
    alt: "Winter landscape",
    title: "winter-landscape-orion.png",
    description: "PNG · 820 KB",
  },
  {
    src: winterLandcapeTheta,
    alt: "Winter landscape",
    title: "winter-landscape-theta.png",
    description: "PNG · 820 KB",
  },
]
---

<AttachmentGroup>
  {images.map((image) => (
    <Attachment orientation="vertical">
      <AttachmentMedia variant="image">
        <Image src={image.src} alt={image.alt} />
      </AttachmentMedia>
      <AttachmentContent>
        <AttachmentTitle>{image.title}</AttachmentTitle>
        <AttachmentDescription>{image.description}</AttachmentDescription>
      </AttachmentContent>
      <AttachmentActions>
        <AttachmentAction aria-label={`Remove ${image.title}`}>
          <X />
        </AttachmentAction>
      </AttachmentActions>
    </Attachment>
  ))}
</AttachmentGroup>
```

## 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.

```astro
---
import FileCode from "@lucide/astro/icons/file-code"
import FileText from "@lucide/astro/icons/file-text"

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

<div class="flex w-full max-w-sm flex-col items-center gap-3">
  <Attachment state="uploading">
    <AttachmentMedia>
      <Spinner />
    </AttachmentMedia>
    <AttachmentContent>
      <AttachmentTitle>sales-dashboard.pdf</AttachmentTitle>
      <AttachmentDescription>Uploading · 64%</AttachmentDescription>
    </AttachmentContent>
  </Attachment>
  <Attachment state="processing">
    <AttachmentMedia>
      <Spinner />
    </AttachmentMedia>
    <AttachmentContent>
      <AttachmentTitle>message-renderer.tsx</AttachmentTitle>
      <AttachmentDescription>Processing…</AttachmentDescription>
    </AttachmentContent>
  </Attachment>
  <Attachment state="error">
    <AttachmentMedia>
      <FileText />
    </AttachmentMedia>
    <AttachmentContent>
      <AttachmentTitle>desk-reference.jpg</AttachmentTitle>
      <AttachmentDescription>Upload failed · try again</AttachmentDescription>
    </AttachmentContent>
  </Attachment>
  <Attachment state="idle">
    <AttachmentMedia>
      <FileCode />
    </AttachmentMedia>
    <AttachmentContent>
      <AttachmentTitle>message-renderer.tsx</AttachmentTitle>
      <AttachmentDescription>TypeScript · 12 KB</AttachmentDescription>
    </AttachmentContent>
  </Attachment>
</div>
```

## Sizes

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

```astro
---
import FileText from "@lucide/astro/icons/file-text"

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

<div class="flex w-full max-w-sm flex-col items-center gap-3">
  <Attachment size="default">
    <AttachmentMedia>
      <FileText />
    </AttachmentMedia>
    <AttachmentContent>
      <AttachmentTitle>sales-dashboard.pdf</AttachmentTitle>
      <AttachmentDescription>PDF · 2.4 MB</AttachmentDescription>
    </AttachmentContent>
  </Attachment>
  <Attachment size="sm">
    <AttachmentMedia>
      <FileText />
    </AttachmentMedia>
    <AttachmentContent>
      <AttachmentTitle>sales-dashboard.pdf</AttachmentTitle>
      <AttachmentDescription>PDF · 2.4 MB</AttachmentDescription>
    </AttachmentContent>
  </Attachment>
  <Attachment size="xs">
    <AttachmentMedia>
      <FileText />
    </AttachmentMedia>
    <AttachmentContent>
      <AttachmentTitle>sales-dashboard.pdf</AttachmentTitle>
      <AttachmentDescription>PDF · 2.4 MB</AttachmentDescription>
    </AttachmentContent>
  </Attachment>
</div>
```

## Group

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

```astro
---
import FileText from "@lucide/astro/icons/file-text"
import ImageIcon from "@lucide/astro/icons/image"

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

<AttachmentGroup>
  <Attachment>
    <AttachmentMedia>
      <ImageIcon />
    </AttachmentMedia>
    <AttachmentContent>
      <AttachmentTitle>workspace.png</AttachmentTitle>
      <AttachmentDescription>PNG · 820 KB</AttachmentDescription>
    </AttachmentContent>
  </Attachment>
  <Attachment>
    <AttachmentMedia>
      <FileText />
    </AttachmentMedia>
    <AttachmentContent>
      <AttachmentTitle>research-summary.pdf</AttachmentTitle>
      <AttachmentDescription>PDF · 1.8 MB</AttachmentDescription>
    </AttachmentContent>
  </Attachment>
  <Attachment>
    <AttachmentMedia>
      <FileText />
    </AttachmentMedia>
    <AttachmentContent>
      <AttachmentTitle>release-notes.md</AttachmentTitle>
      <AttachmentDescription>Markdown · 24 KB</AttachmentDescription>
    </AttachmentContent>
  </Attachment>
</AttachmentGroup>
```

## 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.

```astro
---
import FileText from "@lucide/astro/icons/file-text"
import X from "@lucide/astro/icons/x"

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

<Lightbox>
  <Attachment>
    <AttachmentMedia>
      <FileText />
    </AttachmentMedia>
    <AttachmentContent>
      <AttachmentTitle>Attention is All You Need</AttachmentTitle>
      <AttachmentDescription>PDF · arXiv:1706.03762</AttachmentDescription>
    </AttachmentContent>
    <AttachmentActions>
      <AttachmentAction aria-label="Remove Attention is All You Need">
        <X />
      </AttachmentAction>
    </AttachmentActions>
    <AttachmentTrigger
      data-slot="dialog-trigger"
      aria-label="Preview Attention is All You Need"
    />
  </Attachment>
  <LightboxContent
    label="Attention is All You Need"
    mediaClass="mx-auto w-full max-w-3xl *:h-[min(70dvh,720px)]"
  >
    <iframe
      src="https://arxiv.org/pdf/1706.03762"
      title="Attention is All You Need"
      class="size-full border-0"
    ></iframe>

    <span slot="caption">
      <strong>
        <a
          href="https://arxiv.org/abs/1706.03762"
          title="Attention Is All You Need"
          target="_blank"
          rel="noreferrer"
        >
          Attention Is All You Need
        </a>
      </strong>{" "}
      (Vaswani et al., arXiv:1706.03762)
    </span>
  </LightboxContent>
</Lightbox>
```

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.

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

## API Reference

### AttachmentProps

<AutoTypeTable
    path="../../packages/ui/src/components/ui/attachment/types.ts"
    name="AttachmentProps"
/>

### AttachmentMediaProps

<AutoTypeTable
    path="../../packages/ui/src/components/ui/attachment/types.ts"
    name="AttachmentMediaProps"
/>

### AttachmentActionProps

<AutoTypeTable
    path="../../packages/ui/src/components/ui/attachment/types.ts"
    name="AttachmentActionProps"
/>

Read the [installed source](/r/attachment.json) 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](/components/field) for input labels and messages; the attachment view does not perform an upload.
