AI Attachments UI
Overview
The Attachments component from Vercel's AI Elements provides a unified way to display file attachments and source documents in AI chat UIs, with grid, inline, and list layout variants. It is a set of composable sub-components that render previews, file info, and a remove control, and it accepts AI SDK FileUIPart / SourceDocumentUIPart data. Read this when building file-upload affordances in a prompt input or showing attached/source files inside messages.
Official sources
- Docs: https://elements.ai-sdk.dev/components/attachments
- Repo: https://github.com/vercel/ai-elements
- Setup: https://elements.ai-sdk.dev/docs/setup
Install / setup
npx ai-elements@latest add attachments
Requires shadcn/ui initialized and Tailwind CSS Variables mode (per AI Elements setup).
Core concepts
<Attachments />is the container and takes avariantof"grid" | "inline" | "list"to control layout.<Attachment />wraps a single item and acceptsdata(aFileUIPartorSourceDocumentUIPartwith an id) plus anonRemovecallback.- Composable sub-parts:
<AttachmentPreview />(image/video/icon),<AttachmentInfo />(filename + media type),<AttachmentRemove />(remove button),<AttachmentHoverCard />(hover preview),<AttachmentEmpty />(empty state). AttachmentInfoexposesshowMediaType, andAttachmentRemoveaccepts a customlabelfor accessibility.- Designed to map directly over AI SDK file/source-document parts, so it pairs naturally with Prompt Input and Message.
Best practices
- Choose the layout
variantto match context:gridfor galleries,inlinefor compact prompt-bar chips,listfor detailed/source views. - Provide an accessible
labelonAttachmentRemoveso screen-reader users understand the remove action. - Render attachments by mapping AI SDK
FileUIPartdata rather than inventing a parallel attachment shape. - Use
AttachmentEmptyto communicate the empty state instead of rendering nothing.
Common pitfalls
- Passing data without a stable
id→ each attachment'sdatashould carry an id so list rendering and removal stay correct. - Building custom remove buttons → use
AttachmentRemovewithonRemoveso behavior and a11y are handled. - Forgetting media-type cues → enable
showMediaType/ previews so users can tell files apart at a glance.
Examples
<Attachments variant="grid">
{files.map((file) => (
<Attachment key={file.id} data={file} => remove(file.id)}>
<AttachmentPreview />
<AttachmentInfo showMediaType />
<AttachmentRemove label="Remove attachment" />
</Attachment>
))}
</Attachments>
Further reading
- Prompt Input component: https://elements.ai-sdk.dev/
- AI SDK docs: https://ai-sdk.dev/
Related skills
- ../vercel-ai-sdk-elements — the parent AI Elements library
- ../ai-tool-ui-patterns — rendering tool results in chat