# Attachment

A file as a compact chip, in a prompt or a sent message: a thumbnail for an image (preview), else the file's icon; the name on one line and its kind and size ("PDF · 2.4 MB", the kit's number format); while uploading, "Uploading 40%" and a bar along its foot; on failure the error in words, the chip's edge in the danger tone. onRemove shows Remove named for the file; href (a link) or onOpen (a button) opens it, named by the file's name and described by its kind and size. AttachmentList is the row of them PromptBox and Message use: items (FileUpload's file shape plus type, kind, preview and href), label, onRemove(id) or onOpen(id); it says once each that a file was removed, uploaded or failed, and after a remove moves focus to the next Remove. Every string is a prop.

Page: https://turtlekit.dev/components/attachment

## Import

```tsx
import { Attachment, AttachmentList } from "turtlekit";
```

## Props

### Attachment

| Prop | Type | Default | What it does |
| --- | --- | --- | --- |
| `item` (required) | `AttachmentItem` |  | A file attached to a prompt or a sent message. |
| `onRemove` | `() => void` |  | In a prompt: removes the file (and cancels its upload). |
| `onOpen` | `() => void` |  | In a message: opens the file (a viewer, a download). |
| `disabled` | `boolean` |  | Turns Remove off (a prompt that can't take changes now); the file still shows and opens. |
| `removeLabel` | `(name: string) => string` | `(name) => `Remove ${name}`` | Remove's name: (name) => `Remove ${name}`. |
| `uploadingLabel` | `(percent: string) => string` | `(percent) => `Uploading ${percent}`` | The words while uploading: (percent) => `Uploading ${percent}`. |
| `failedLabel` | `string` | `Couldn't upload. Remove it and add it again.` | A failed upload's words when the item has no `error`. |
| `progressLabel` | `(name: string) => string` | `(name) => `Uploading ${name}`` | The upload bar's name: (name) => `Uploading ${name}`. |
| `fileLabel` | `string` | `File` | The kind when neither `kind`, the extension nor the type says it. |

### AttachmentList

| Prop | Type | Default | What it does |
| --- | --- | --- | --- |
| `label` (required) | `string` |  | Names the list ("Attached files", "Files in this message"). |
| `items` (required) | `ReadonlyArray<AttachmentItem>` |  | The files, in the order added. |
| `onRemove` | `(id: string) => void` |  | In a prompt: remove a file by its id (and cancel its upload). |
| `onOpen` | `(id: string) => void` |  | In a message: open a file by its id. |
| `removedLabel` | `(name: string) => string` | `(name) => `${name} removed.`` | Said when a file is removed: (name) => `${name} removed.` |
| `uploadedLabel` | `(name: string) => string` | `(name) => `${name} uploaded.`` | Said when a file finishes uploading: (name) => `${name} uploaded.` |
| `removeLabel` | `(name: string) => string` | `(name) => `Remove ${name}`` | Remove's name: (name) => `Remove ${name}`. |
| `uploadingLabel` | `(percent: string) => string` | `(percent) => `Uploading ${percent}`` | The words while uploading: (percent) => `Uploading ${percent}`. |
| `failedLabel` | `string` | `Couldn't upload. Remove it and add it again.` | A failed upload's words when the item has no `error`. |
| `progressLabel` | `(name: string) => string` | `(name) => `Uploading ${name}`` | The upload bar's name: (name) => `Uploading ${name}`. |
| `fileLabel` | `string` | `File` | The kind when neither `kind`, the extension nor the type says it. |

## Shapes its props take

### AttachmentItem

| Prop | Type | Default | What it does |
| --- | --- | --- | --- |
| `id` (required) | `string` |  |  |
| `name` (required) | `string` |  |  |
| `size` | `number` |  | Bytes. |
| `type` | `string` |  | The file's MIME type ("application/pdf", "image/png"), as `File.type` gives it. |
| `kind` | `string` |  | What kind of file it is, when the name's extension does not say it ("Spreadsheet"). |
| `preview` | `string` |  | An image's address (an object URL while it uploads, the stored file's after): shown as the chip's thumbnail. |
| `status` | `"done" \| "error" \| "uploading"` |  | "uploading" shows its progress, "error" its message; default "done". |
| `progress` | `number` |  | 0–100 while uploading. |
| `error` | `string` |  | What went wrong and what to do ("Too large to send. |
| `href` | `string` |  | Where the file opens (in a sent message): the chip becomes a link. |

## Rules it keeps

- `a11y.accessible-name`
- `colour.not-alone`

## Examples

Every example on the page, as one file:

```tsx
import { useEffect, useState } from "react";
import { AttachmentList } from "turtlekit";
import type { AttachmentItem } from "turtlekit";

/**
 * Files on a prompt before it is sent: one uploading with its progress, one ready, one refused with the reason. Each
 * has Remove, named for the file.
 */
export function PromptFiles() {
  const [files, setFiles] = useState<AttachmentItem[]>([
    { id: "notes", name: "meeting-notes-24-nov.docx", size: 86_000, status: "uploading", progress: 0 },
    { id: "budget", name: "q4-budget.xlsx", size: 412_000 },
    {
      id: "video",
      name: "site-walkthrough.mov",
      size: 48_200_000,
      status: "error",
      error: "Too large to send. Files up to 20 MB.",
    },
  ]);
  // A stand-in upload: the file moves on a fifth at a time until it is done.
  useEffect(() => {
    if (!files.some((file) => file.status === "uploading")) return;
    const timer = setTimeout(
      () =>
        setFiles((list) =>
          list.map((file) => {
            if (file.status !== "uploading") return file;
            const progress = (file.progress ?? 0) + 20;
            return progress >= 100 ? { ...file, status: "done", progress: undefined } : { ...file, progress };
          }),
        ),
      700,
    );
    return () => clearTimeout(timer);
  }, [files]);
  return (
    <AttachmentList
      label="Attached files"
      items={files}
      onRemove={(id) => setFiles((list) => list.filter((file) => file.id !== id))}
    />
  );
}

/** Files in a sent message: a screenshot shown as its thumbnail, which opens the picture, and a PDF. */
export function SentFiles() {
  return (
    <AttachmentList
      label="Files in this message"
      items={[
        {
          id: "overview",
          name: "northwind-overview.png",
          type: "image/png",
          size: 248_000,
          preview: "/media/dashboard.png",
          href: "/media/dashboard.png",
        },
        {
          id: "plan",
          name: "website-redesign-plan.pdf",
          type: "application/pdf",
          size: 1_240_000,
        },
      ]}
    />
  );
}
```
