Attachment
Shows a file or an image that belongs to a message or a form, with its name, details, upload state and actions.
Installation
pnpm dlx shadcn@latest add @silkware/attachmentTo copy the component by hand, install its dependencies:
pnpm add class-variance-authority cnThe attachment actions use the Button, so install it too:
pnpm dlx shadcn@latest add @silkware/buttonThen copy the source into 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.
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.
Vertical
orientation="vertical" stacks the media above the text in a narrow card, which suits images and short lists.
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.
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.
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.
Accessibility
| Key | Action |
|---|---|
| Tab | Moves 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. |
| Enter | Follows the link or presses the focused trigger or action. |
| Space | Presses the focused trigger button or action. |
The component provides:
- A real
buttonforAttachmentTriggerand a real React AriaButtonfor eachAttachmentAction, with their native keyboard behaviour. Withrenderthe 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
uploadingorprocessing.
You provide:
- An
aria-labelon everyAttachmentAction, naming the action and the file: "Remove order-4417.pdf". The actions are icon buttons with no text. - An
aria-labelonAttachmentTrigger, because it covers the card and has no text of its own: "Open quality-report-q3.pdf". - The reason in
AttachmentDescriptionfor 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
alttext 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 anaria-labelonAttachmentGroup, 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.
Say why the upload failed and offer a retry.
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.
Label each icon action with the verb and the file name.
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.
| Prop | Type | Default | Description |
|---|---|---|---|
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? | string | None | Classes merged after the variant classes. |
AttachmentMedia
Renders a <div> with the icon or the image, in a square tile.
| Prop | Type | Default | Description |
|---|---|---|---|
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? | string | None | Classes 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.
| Prop | Type | Default | Description |
|---|---|---|---|
className? | string | None | Classes merged after the content classes. |
AttachmentTitle
Renders a <span> with the file name. It truncates and shimmers while the state is uploading or processing.
| Prop | Type | Default | Description |
|---|---|---|---|
className? | string | None | Classes 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.
| Prop | Type | Default | Description |
|---|---|---|---|
className? | string | None | Classes 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.
| Prop | Type | Default | Description |
|---|---|---|---|
className? | string | None | Classes 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.
| Prop | Type | Default | Description |
|---|---|---|---|
variant? | ButtonProps variant | "ghost" | The Button variant. |
size? | ButtonProps size | "icon-xs" | The Button size. |
onPress? | (event: PressEvent) => void | None | Called when the action is pressed. |
aria-label? | string | None | The 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.
| Prop | Type | Default | Description |
|---|---|---|---|
render? | (props: HTMLAttributes<HTMLElement>) => ReactNode | None | A 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? | string | None | The name of the trigger. It has no text of its own. |
onClick? | MouseEventHandler | None | Called 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.
| Prop | Type | Default | Description |
|---|---|---|---|
className? | string | None | Classes merged after the layout classes. |