Skip to content

Attachment

Shows a file or an image that belongs to a message or a form, with its name, details, upload state and actions.

2026-pricelist.pdfPDF · 2.4 MB

Installation

pnpm dlx shadcn@latest add @silkware/attachment

To copy the component by hand, install its dependencies:

pnpm add class-variance-authority cn

The attachment actions use the Button, so install it too:

pnpm dlx shadcn@latest add @silkware/button

Then copy the source into components/ui/attachment.tsx:

components/ui/attachment.tsx
import * as React from "react"import { cva, type VariantProps } from "class-variance-authority"import { cn } from "cn"
import { Button } from "@/components/ui/button"
const attachmentVariants = cva(  "group/attachment relative flex w-fit max-w-full min-w-0 shrink-0 flex-wrap rounded-xl border bg-card text-card-foreground transition-colors focus-within:ring-1 focus-within:ring-ring/50 has-[>a,>button]:hover:bg-muted/50 data-[state=error]:border-destructive/30 data-[state=idle]:border-dashed",  {    variants: {      size: {        default:          "gap-2 text-sm has-data-[slot=attachment-content]:px-2.5 has-data-[slot=attachment-content]:py-2 has-data-[slot=attachment-media]:p-2",        sm: "gap-2.5 text-xs has-data-[slot=attachment-content]:px-2 has-data-[slot=attachment-content]:py-1.5 has-data-[slot=attachment-media]:p-1.5",        xs: "gap-1.5 rounded-lg text-xs has-data-[slot=attachment-content]:px-1.5 has-data-[slot=attachment-content]:py-1 has-data-[slot=attachment-media]:p-1",      },      orientation: {        horizontal: "min-w-40 items-center",        vertical: "w-24 flex-col has-data-[slot=attachment-content]:w-30",      },    },  })
function Attachment({  className,  state = "done",  size = "default",  orientation = "horizontal",  ...props}: React.ComponentProps<"div"> &  VariantProps<typeof attachmentVariants> & {    state?: "idle" | "uploading" | "processing" | "error" | "done"  }) {  const resolvedOrientation = orientation ?? "horizontal"
  return (    <div      data-slot="attachment"      data-state={state}      data-size={size}      data-orientation={resolvedOrientation}      className={cn(attachmentVariants({ size, orientation }), className)}      {...props}    />  )}
const attachmentMediaVariants = cva(  "relative flex aspect-square w-10 shrink-0 items-center justify-center overflow-hidden rounded-lg bg-muted text-foreground group-data-[orientation=vertical]/attachment:w-full group-data-[size=sm]/attachment:w-8 group-data-[size=xs]/attachment:w-7 group-data-[size=xs]/attachment:rounded-md group-data-[state=error]/attachment:bg-destructive/10 group-data-[state=error]/attachment:text-destructive group-data-[orientation=vertical]/attachment:*:data-[slot=spinner]:size-6! [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 group-data-[orientation=vertical]/attachment:[&_svg:not([class*='size-'])]:size-6 group-data-[size=xs]/attachment:[&_svg:not([class*='size-'])]:size-3.5",  {    variants: {      variant: {        icon: "",        image:          "opacity-60 group-data-[state=done]/attachment:opacity-100 group-data-[state=idle]/attachment:opacity-100 *:[img]:aspect-square *:[img]:w-full *:[img]:object-cover",      },    },    defaultVariants: {      variant: "icon",    },  })
function AttachmentMedia({  className,  variant = "icon",  ...props}: React.ComponentProps<"div"> & VariantProps<typeof attachmentMediaVariants>) {  return (    <div      data-slot="attachment-media"      data-variant={variant}      className={cn(attachmentMediaVariants({ variant }), className)}      {...props}    />  )}
function AttachmentContent({  className,  ...props}: React.ComponentProps<"div">) {  return (    <div      data-slot="attachment-content"      className={cn(        "max-w-full min-w-0 flex-1 leading-tight group-data-[orientation=vertical]/attachment:px-1",        className      )}      {...props}    />  )}
function AttachmentTitle({  className,  ...props}: React.ComponentProps<"span">) {  return (    <span      data-slot="attachment-title"      className={cn(        "block max-w-full min-w-0 truncate font-medium group-data-[state=processing]/attachment:shimmer group-data-[state=uploading]/attachment:shimmer",        className      )}      {...props}    />  )}
function AttachmentDescription({  className,  ...props}: React.ComponentProps<"span">) {  return (    <span      data-slot="attachment-description"      className={cn(        "mt-0.5 block min-w-0 truncate text-xs text-muted-foreground group-data-[state=error]/attachment:text-destructive/80",        "max-w-full",        className      )}      {...props}    />  )}
function AttachmentActions({  className,  ...props}: React.ComponentProps<"div">) {  return (    <div      data-slot="attachment-actions"      className={cn(        "relative z-20 flex shrink-0 items-center group-data-[orientation=vertical]/attachment:absolute group-data-[orientation=vertical]/attachment:top-3 group-data-[orientation=vertical]/attachment:right-3 group-data-[orientation=vertical]/attachment:gap-1",        className      )}      {...props}    />  )}
function AttachmentAction({  className,  variant,  size = "icon-xs",  ...props}: React.ComponentProps<typeof Button>) {  return (    <Button      data-slot="attachment-action"      variant={variant ?? "ghost"}      size={size}      className={cn(className)}      {...props}    />  )}
function AttachmentTrigger({  className,  render,  type,  children,  ...props}: React.ComponentProps<"button"> & {  render?: (props: React.HTMLAttributes<HTMLElement>) => React.ReactNode}) {  if (render) {    const renderProps = {      ...props,      "data-slot": "attachment-trigger",      className: cn("absolute inset-0 z-10 outline-none", className),      children,    }
    return render(renderProps)  }
  return (    <button      {...props}      type={type ?? "button"}      data-slot="attachment-trigger"      className={cn("absolute inset-0 z-10 outline-none", className)}    >      {children}    </button>  )}
function AttachmentGroup({ className, ...props }: React.ComponentProps<"div">) {  return (    <div      data-slot="attachment-group"      className={cn(        "flex min-w-0 scroll-fade-x snap-x snap-mandatory scroll-px-1 scrollbar-none gap-3 overflow-x-auto overscroll-x-contain py-1 *:data-[slot=attachment]:flex-none *:data-[slot=attachment]:snap-start",        className      )}      {...props}    />  )}
export {  Attachment,  AttachmentGroup,  AttachmentMedia,  AttachmentContent,  AttachmentTitle,  AttachmentDescription,  AttachmentActions,  AttachmentAction,  AttachmentTrigger,}

Usage

import {  Attachment,  AttachmentAction,  AttachmentActions,  AttachmentContent,  AttachmentDescription,  AttachmentGroup,  AttachmentMedia,  AttachmentTitle,  AttachmentTrigger,} from "@/components/ui/attachment"
<Attachment>  <AttachmentMedia>    <FileTextIcon />  </AttachmentMedia>  <AttachmentContent>    <AttachmentTitle>2026-pricelist.pdf</AttachmentTitle>    <AttachmentDescription>PDF · 2.4 MB</AttachmentDescription>  </AttachmentContent>  <AttachmentActions>    <AttachmentAction aria-label="Remove 2026-pricelist.pdf">      <XIcon />    </AttachmentAction>  </AttachmentActions></Attachment>

Attachment only draws the card. It does not upload or preview anything: you set state from your own upload code. Use it in the composer of a chat, under a Message, or in a list of files in a form. The chat patterns that use it are on Chat and Prompt Input.

Examples

States

state takes idle, uploading, processing, error and done, which is the default. The title shimmers while a file is uploading or processing, an idle attachment has a dashed border, and an error gets a crimson border and icon tile. Put the reason in the description and a retry action next to remove, so the state does not rest on colour.

supplier-contract.pdfReady to upload
2026-pricelist.xlsxUploading · 64%
quality-report-q3.pdfReading the document
stock-count-verona.csvUpload failed. The file is over 25 MB.
order-4417.pdfUploaded · 1.8 MB

Size

default, sm and xs set the padding, the media size and the text size. Use xs for a row of files inside a message, and leave out the description when it does not fit.

DefaultPDF · 2.4 MB
SmallPDF · 2.4 MB
Extra small

Vertical

orientation="vertical" stacks the media above the text in a narrow card, which suits images and short lists.

order-4417.pdfPDF · 1.4 MB

Image

Set variant="image" on AttachmentMedia and put an img inside it. The image is cropped to a square. While the state is uploading or processing it is dimmed. AttachmentTrigger, rendered as a link, makes the whole card open the image, and the remove action stays separately clickable.

line-3-label.pngPNG · 820 KB
line-4-label.pngPNG · 820 KB
line-5-label.pngPNG · 820 KB

Group

AttachmentGroup lays attachments in one row that scrolls sideways, snaps to each card and fades at the edge. Give the cards a fixed width so the row can overflow.

order-4417.pdfPDF · 1.4 MB
stock-count-verona.csvCSV · 18 KB
2026-pricelist.xlsxXLSX · 212 KB
quality-report-q3.pdfPDF · 3.1 MB

Trigger

AttachmentTrigger stretches over the whole card so the card opens something. Render it as a link with render, or leave the default button and handle the click. The actions sit above it, so remove and retry still work.

quality-report-q3.pdfPDF · 3.1 MB

Accessibility

KeyAction
TabMoves focus through the actions and the trigger in the order they appear in the markup. In the examples the trigger comes last, so the actions come first.
EnterFollows the link or presses the focused trigger or action.
SpacePresses the focused trigger button or action.

The component provides:

  • A real button for AttachmentTrigger and a real React Aria Button for each AttachmentAction, with their native keyboard behaviour. With render the trigger takes the role of the element you render, such as a link.
  • A focus ring on the card when any control inside it has focus.
  • Truncation of long names, so the layout holds; the full name stays in the DOM.
  • A shimmer on the title while the state is uploading or processing.

You provide:

  • An aria-label on every AttachmentAction, naming the action and the file: "Remove order-4417.pdf". The actions are icon buttons with no text.
  • An aria-label on AttachmentTrigger, because it covers the card and has no text of its own: "Open quality-report-q3.pdf".
  • The reason in AttachmentDescription for an error. The state also changes colour, and colour must not be the only signal.
  • An announcement for state changes. The card does not announce them, so use a Toast or a status message when an upload ends or fails.
  • Empty alt text on an image that repeats the title, or a real description when the image carries information.
  • For a group of cards with no controls inside, tabIndex={0}, role="group" and an aria-label on AttachmentGroup, so keyboard users can scroll it. When the cards hold controls, tabbing to them scrolls them into view.

Guidelines

When to use

  • For files a person adds to a message, an order or a form, before and after they are sent.
  • For files an agent attached to a reply, such as a generated price list, with a download action.
  • To show upload progress for several files at once, one card each.

When not to use

  • To show a table of documents with owner and date. Use a Table.
  • To show a single status with no file behind it. Use a Badge.
  • To measure the progress of one long upload on its own. Add a Progress bar, or write the percentage in the description.
  • For a generic list row with a title and an action. Use Item.

Content

  • Show the file name in the title, with its extension, and keep it as the user named it.
  • Put type and size in the description: "PDF · 2.4 MB".
  • Write states as short phrases: "Ready to upload", "Uploading · 64%", "Reading the document".
  • For an error, say what failed and what to do: "Upload failed. The file is over 25 MB."
  • Label actions with a verb and the file: "Remove order-4417.pdf", "Retry upload of stock-count-verona.csv".

Errors

An error uses the destructive colour for the border and the icon tile. Always write the reason in words next to it.

stock-count-verona.csvUpload failed. The file is over 25 MB.
Do

Say why the upload failed and offer a retry.

stock-count-verona.csv
Don’t

Don’t rely on the crimson state alone. Without a reason, the failure is invisible to people who do not see the colour and unhelpful to those who do.

order-4417.pdfPDF · 1.4 MB
Do

Label each icon action with the verb and the file name.

order-4417.pdfPDF · 1.4 MB
Don’t

Don’t leave an icon action without a label. A screen reader announces it as an unnamed button.

API reference

Attachment

Renders a <div> that holds the media, the text and the actions, and exposes the state to its parts as data attributes. It accepts every prop of a div.

PropTypeDefaultDescription
state?"idle" | "uploading" | "processing" | "error" | "done""done"The upload state. It sets the border, the icon tile and the shimmer.
size?"default" | "sm" | "xs""default"The padding, the media size and the text size.
orientation?"horizontal" | "vertical""horizontal"Whether the media sits beside or above the text.
className?stringNoneClasses merged after the variant classes.

AttachmentMedia

Renders a <div> with the icon or the image, in a square tile.

PropTypeDefaultDescription
variant?"icon" | "image""icon"icon holds an icon or a spinner. image crops an img to a square and dims it while the file uploads.
className?stringNoneClasses merged after the media classes.

AttachmentContent

Renders a <div> around the title and the description. It takes the free width and lets the title truncate.

PropTypeDefaultDescription
className?stringNoneClasses merged after the content classes.

AttachmentTitle

Renders a <span> with the file name. It truncates and shimmers while the state is uploading or processing.

PropTypeDefaultDescription
className?stringNoneClasses merged after the title classes.

AttachmentDescription

Renders a <span> with the details in the muted colour. In the error state it uses the destructive colour.

PropTypeDefaultDescription
className?stringNoneClasses merged after the description classes.

AttachmentActions

Renders a <div> that holds the actions at the end of the card. In the vertical orientation it moves to the top corner.

PropTypeDefaultDescription
className?stringNoneClasses merged after the layout classes.

AttachmentAction

Renders a Button that defaults to the ghost variant and the icon-xs size. It accepts every React Aria Button prop.

PropTypeDefaultDescription
variant?ButtonProps variant"ghost"The Button variant.
size?ButtonProps size"icon-xs"The Button size.
onPress?(event: PressEvent) => voidNoneCalled when the action is pressed.
aria-label?stringNoneThe name of the action. Required for an icon action.

AttachmentTrigger

Renders an absolutely positioned <button type="button"> that covers the card. With render it renders your element instead.

PropTypeDefaultDescription
render?(props: HTMLAttributes<HTMLElement>) => ReactNodeNoneA function that receives the props to spread on your element, such as an anchor.
type?"button" | "submit" | "reset""button"The button type when you do not use render.
aria-label?stringNoneThe name of the trigger. It has no text of its own.
onClick?MouseEventHandlerNoneCalled when the card is clicked. The trigger is a native button, so it uses onClick, not onPress.

AttachmentGroup

Renders a <div> that lays attachments out in a row that scrolls sideways, snaps to each card and fades at the edges.

PropTypeDefaultDescription
className?stringNoneClasses merged after the layout classes.