Attachments
GitHubStandalone file attachment picker and preview list with file-type icons, upload states, and readonly mode.
Installation
bash
pnpm dlx shadcn@latest add @cs/attachmentsUsage
tsx
import { Attachments } from "@/components/cs/attachments";tsx
<Attachments
defaultValue={existingFiles}
value={files}
onAdd={addFile}
onRemove={removeFile}
/>Read-only attachments
Displays existing attachments without allowing users to add or remove files.
tsx
"use client"
import { Attachments } from "@/registry/cs/ui/attachments"
import { NextIntlClientProvider } from "next-intl"
export default function AttachmentsReadonlyPreview() {
return (
<NextIntlClientProvider
locale="en"
messages={{
form: {
attachments: {
add: "Add file",
idle: "Ready",
remove: "Remove {name}",
noFiles: "No files selected",
error: "Error",
done: "Done",
uploading: "Uploading",
processing: "Processing",
},
},
}}
>
<Attachments
readOnly
defaultValue={[
{
uuid: "contract",
name: "signed-contract.pdf",
type: "application/pdf",
src: "",
},
{
uuid: "brief",
name: "project-brief.docx",
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
src: "",
},
]}
/>
</NextIntlClientProvider>
)
}API Reference
Attachments
Displays existing and newly selected file attachments with previews, file-type icons, and upload states. Use onAdd, onRemove, and onDefaultRemove to connect it to application state.
Props
| Name | Type | Default | Description |
|---|---|---|---|
| accept | string | CS_ACCEPT | Accepted file extensions or MIME types passed to the native file picker. |
| containerClassName | string | - | Additional class names applied to the grid wrapper that contains all attachment cards. |
| defaultValue | DefaultFile[] | - | Files already stored by the application and shown before new uploads. |
| disabled | boolean | - | Disables the file picker button. |
| itemClassName | string | - | Additional class names applied to each individual attachment item. |
| onAdd | (file: File) => void | - | Called for every file selected in the native file picker. |
| onDefaultRemove | (file: DefaultFile) => void | - | Called when an existing default file is removed. |
| onRemove | (file: File) => void | - | Called when a selected file is removed. |
| readOnly | boolean | false | Prevents selecting or removing files while keeping existing attachments visible. |
| translationsRootKey | string | "form.attachments" | Translation namespace containing the attachment labels and messages. The namespace must provide add, noFiles, idle, uploading, processing, error, done, and ready strings, plus remove with a {name} interpolation. json { "add": "Add file", "noFiles": "No files selected", "idle": "Ready to upload", "uploading": "Uploading", "processing": "Processing", "error": "Upload failed", "done": "Uploaded", "ready": "Ready", "remove": "Remove {name}" } The default namespace is form.attachments. |
| value | CsFile[] | - | Files selected by the user and tracked through their upload lifecycle. |