# Attachment

A file or picture attached to something: its media, name, what is known of it, how its upload is going and what can be done to it.

Docs: https://ballpoint.st9wd.com/docs/attachment

```bash
npx shadcn@latest add @ballpoint/attachment
```

```tsx
import { Attachment, AttachmentContent, AttachmentDescription, AttachmentMedia, AttachmentTitle } from "@/components/ui/attachment"

<Attachment>
  <AttachmentMedia><FileTextIcon /></AttachmentMedia>
  <AttachmentContent>
    <AttachmentTitle>sales-dashboard.pdf</AttachmentTitle>
    <AttachmentDescription>PDF · 2.4 MB</AttachmentDescription>
  </AttachmentContent>
</Attachment>
```

Props:

- `state`: `"idle" | "uploading" | "processing" | "error" | "done"` (default `"done"`). Uploading and processing pulse the title; error is red pen.
- `size`: `"default" | "sm" | "xs"` (default `"default"`). Padding and type size.
- `orientation`: `"horizontal" | "vertical"` (default `"horizontal"`). Media beside the text, or above it.
- `AttachmentMedia variant`: `"icon" | "image"` (default `"icon"`). An icon circled with the pen, or a picture.
- `AttachmentAction`: `Button props`. A small button; give an icon-only one an aria-label.
- `AttachmentTrigger render`: `ReactElement`. A full-card overlay: render it as a link, or as a dialog's trigger. Give it an aria-label.
- `seed`: `string | number`. Pins the drawing.

Pen settings: `draw`, `roughness`, `passes`, `radius`, `corners`, `weight`, `speed` (see Pen settings).

## Example

```tsx
import { Attachment, AttachmentAction, AttachmentActions, AttachmentContent, AttachmentDescription, AttachmentMedia, AttachmentTitle } from "@/components/ui/attachment";
import { InkIcon } from "@/components/ui/ink-icons";

export default function AttachmentDemo() {
  return (
    <Attachment seed="att-demo" className="w-full max-w-sm">
      <AttachmentMedia seed="att-demo-media">
        <InkIcon name="file-text" />
      </AttachmentMedia>
      <AttachmentContent>
        <AttachmentTitle>sales-dashboard.pdf</AttachmentTitle>
        <AttachmentDescription>PDF · 2.4 MB</AttachmentDescription>
      </AttachmentContent>
      <AttachmentActions>
        <AttachmentAction aria-label="Remove sales-dashboard.pdf" seed="att-demo-x">
          <InkIcon name="close" />
        </AttachmentAction>
      </AttachmentActions>
    </Attachment>
  );
}
```

## States

Uploading and processing pulse the name; an error is in red pen, with the reason in words.

```tsx
import { Attachment, AttachmentAction, AttachmentActions, AttachmentContent, AttachmentDescription, AttachmentMedia, AttachmentTitle } from "@/components/ui/attachment";
import { InkIcon } from "@/components/ui/ink-icons";

const files = [
  { name: "field-sketches.zip", note: "Uploading · 42%", state: "uploading", icon: "upload" },
  { name: "letter-to-ada.docx", note: "Processing…", state: "processing", icon: "file-text" },
  { name: "scan-001.png", note: "Couldn't upload: the file is over 10 MB", state: "error", icon: "alert-circle" },
  { name: "notes.md", note: "Markdown · 4 KB", state: "done", icon: "file" },
] as const;

export default function AttachmentStates() {
  return (
    <div className="flex w-full max-w-sm flex-col gap-3">
      {files.map((file) => (
        <Attachment key={file.name} state={file.state} seed={`att-${file.state}`} className="w-full">
          <AttachmentMedia seed={`att-m-${file.state}`}>
            <InkIcon name={file.icon} />
          </AttachmentMedia>
          <AttachmentContent>
            <AttachmentTitle>{file.name}</AttachmentTitle>
            <AttachmentDescription>{file.note}</AttachmentDescription>
          </AttachmentContent>
          <AttachmentActions>
            <AttachmentAction aria-label={`Remove ${file.name}`} seed={`att-x-${file.state}`}>
              <InkIcon name="close" />
            </AttachmentAction>
          </AttachmentActions>
        </Attachment>
      ))}
    </div>
  );
}
```

## Sizes

Default, small and extra small.

```tsx
import { Attachment, AttachmentContent, AttachmentDescription, AttachmentMedia, AttachmentTitle } from "@/components/ui/attachment";
import { InkIcon } from "@/components/ui/ink-icons";

export default function AttachmentSizes() {
  return (
    <div className="flex flex-col items-start gap-3">
      {(["default", "sm", "xs"] as const).map((size) => (
        <Attachment key={size} size={size} seed={`att-size-${size}`}>
          <AttachmentMedia seed={`att-size-m-${size}`}>
            <InkIcon name="file-text" />
          </AttachmentMedia>
          <AttachmentContent>
            <AttachmentTitle>report-{size}.pdf</AttachmentTitle>
            {size !== "xs" && <AttachmentDescription>PDF · 1.1 MB</AttachmentDescription>}
          </AttachmentContent>
        </Attachment>
      ))}
    </div>
  );
}
```

## Group

A row that scrolls sideways and snaps, with pictures and files together.

```tsx
import { Attachment, AttachmentContent, AttachmentDescription, AttachmentGroup, AttachmentMedia, AttachmentTitle } from "@/components/ui/attachment";
import { InkIcon } from "@/components/ui/ink-icons";

export default function AttachmentGroupExample() {
  return (
    <div className="w-full max-w-sm">
      <AttachmentGroup aria-label="Attachments">
        <Attachment orientation="vertical" seed="attg-a" className="w-40">
          <AttachmentMedia variant="image">
            {/* eslint-disable-next-line @next/next/no-img-element */}
            <img src="/doodle-portrait.svg" alt="" />
          </AttachmentMedia>
          <AttachmentContent className="w-full">
            <AttachmentTitle>portrait.svg</AttachmentTitle>
            <AttachmentDescription>Image · 12 KB</AttachmentDescription>
          </AttachmentContent>
        </Attachment>
        {["notes.md", "budget.xlsx", "scan.pdf"].map((name, i) => (
          <Attachment key={name} orientation="vertical" seed={`attg-${i}`} className="w-40">
            <AttachmentMedia seed={`attg-m-${i}`}>
              <InkIcon name="file-text" />
            </AttachmentMedia>
            <AttachmentContent className="w-full">
              <AttachmentTitle>{name}</AttachmentTitle>
              <AttachmentDescription>File</AttachmentDescription>
            </AttachmentContent>
          </Attachment>
        ))}
      </AttachmentGroup>
    </div>
  );
}
```

## With a trigger

Make the whole card open a dialog or a link; the buttons stay above it.

```tsx
import { Attachment, AttachmentAction, AttachmentActions, AttachmentContent, AttachmentDescription, AttachmentMedia, AttachmentTitle, AttachmentTrigger } from "@/components/ui/attachment";
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import { InkIcon } from "@/components/ui/ink-icons";

export default function AttachmentTriggerExample() {
  return (
    <Dialog>
      <Attachment seed="att-trigger" className="w-full max-w-sm">
        <AttachmentMedia seed="att-trigger-m">
          <InkIcon name="file-text" />
        </AttachmentMedia>
        <AttachmentContent>
          <AttachmentTitle>research-summary.pdf</AttachmentTitle>
          <AttachmentDescription>PDF · 860 KB · Open to preview</AttachmentDescription>
        </AttachmentContent>
        <AttachmentActions>
          <AttachmentAction aria-label="Remove research-summary.pdf" seed="att-trigger-x">
            <InkIcon name="close" />
          </AttachmentAction>
        </AttachmentActions>
        <DialogTrigger render={<AttachmentTrigger aria-label="Preview research-summary.pdf" />} />
      </Attachment>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>research-summary.pdf</DialogTitle>
          <DialogDescription>A preview goes here.</DialogDescription>
        </DialogHeader>
      </DialogContent>
    </Dialog>
  );
}
```

## Pen settings

- `draw`: `"auto" | "mount" | "none"` (default `"auto"`). When the strokes draw themselves in: the first time they scroll into view, as soon as they render, or never (already drawn).
- `roughness`: `number` (default `1`). 0 is ruler-neat, 1 a quick confident hand, 2 a scrawl.
- `passes`: `1 | 2 | 3`. How many times an outline is gone over. Each component picks its own default.
- `radius`: `number | "full"` (default `0`). Corner radius in px; "full" draws a pill.
- `corners`: `"crossed" | "joined"` (default `"crossed"`). Square corners: sides pulled separately past each other, or the box drawn in one motion.
- `weight`: `number` (default `1`). Line weight multiplier. Also settable in CSS as --ink-weight.
- `speed`: `number` (default `1`). Drawing speed multiplier. Also settable in CSS as --ink-speed.
