Attachments
Show the files attached to a message, and let people remove them.
Use attachments to show files as chips, with an image's preview or a file icon, the name, and the size or what's wrong with the file. The Composer uses them for its draft's files.
- retry-spec.pdf482 kB
- notes.md812 byte
- heap.zipFile is too large
Installation
npx shadcn@latest add @kind-ui/attachmentsComposition
Attachments
└── AttachmentAttachments renders a list, and an Attachment chip for each of its items. To style the chips, pass a function of { rows } as its children that renders an Attachment per row, each with the item's id as its value. Name the list with aria-label.
Usage
Items
Pass items, each with an id, a name and optionally its size in bytes and mediaType. An image shows a preview from its url, or from its file once the chip is on the page. Set error to "type" or "size" to show "File type not accepted" or "File is too large" in place of the size. A chip added later fades in; the ones there at first don't.
Removing
Pass onRemove to let people remove chips. It gets the item's id, and you take the item out of items. Each chip shows a remove button for the pointer, and Delete or Backspace removes the focused chip. Focus then moves to the next chip, or the previous one at the end.
A removed chip stays in place and fades out, hidden from assistive technology and out of reach, then the chips after it slide into its place. Under prefers-reduced-motion, or with motion="reduced", it fades over 120 ms and nothing slides, and with motion="none" it leaves at once. To style the chips and keep the exit, pass Attachments a function of { rows } that renders an Attachment for each row; chips you render from items leave as soon as you remove them.
Keyboard
The list takes one Tab stop. The arrow keys move between chips, and Tab leaves the list.
API Reference
Prop
Type
Agent
An agent connected with createAgent reads the list's items. While onRemove is set, it can run this action:
Prop
Type
Removing doesn't wait for a person to confirm, since a removed file can be attached again. To ask the person first, return their answer from your allow, which also answers every action that does need a person. Inside a composer, an agent removes attachments through the composer's removeAttachment, which waits for a person while agentConfirm is on:
import { createAgent } from "@ui-sdk/react/agent";
const agent = createAgent({
allow: ({ component, action, confirm }) => {
if (confirm) return window.confirm(confirm);
if (component === "kind-attachments") return window.confirm(`Let the agent ${action} an attachment?`);
return true;
},
});Runs in React and Web Components. On <kind-attachments>, items is a property and motion an attribute. Removal needs the can-remove attribute, and each removal fires remove with the item's id.