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
npx shadcn@latest add @gnostic/attachment
pnpm dlx shadcn@latest add @gnostic/attachment

Component classes: Default Gnostic.

Complete the Installation guide first. Then copy the component files below.

No extra packages are required.

Copy the files

src/components/ui/attachment/attachment-action.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
---
/**
 * 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
---
/**
 * 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
---
/**
 * 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
---
/**
 * 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
---
/**
 * 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
---
/**
 * 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
---
/**
 * 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
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
---
/**
 * 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
/** 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
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']
}

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.