# Download (https://s3.dimah.dev/docs/ui/components/download)

Download stored files with a button. `Download` holds one
[download hook](https://s3.dimah.dev/docs/react/hooks/download), and every `DownloadTrigger`
inside it downloads one object. Each key has its own state, so one hook
serves a whole list.

For a file card with download and delete actions, use
[Object Card](https://s3.dimah.dev/docs/ui/components/object-card).

```tsx title="download-demo.tsx"
"use client";

import { DownloadIcon } from "lucide-react";
import { useFetchDownload } from "@dimah-s3/react";
import {
  Download,
  DownloadToasts,
  DownloadTrigger,
  FileAttachment,
} from "@dimah-s3/ui";

const FILES = [
  {
    key: "uploads/3f1a/quarterly-report.pdf",
    name: "quarterly-report.pdf",
    type: "application/pdf",
    size: 8 * 1024 * 1024,
  },
  {
    key: "uploads/9b2e/brand-assets.zip",
    name: "brand-assets.zip",
    type: "application/zip",
    size: 24 * 1024 * 1024,
  },
  {
    key: "uploads/c47d/launch-video.mp4",
    name: "launch-video.mp4",
    type: "video/mp4",
    size: 16 * 1024 * 1024,
  },
];

export function DownloadDemo() {
  // Fetches the bytes in the page, with progress and cancel.
  const download = useFetchDownload({ route: "uploads" });

  return (
    <div className="mx-auto w-full max-w-md">
      <Download download={download}>
        <ul className="flex flex-col gap-2">
          {FILES.map((file) => (
            <li key={file.key}>
              <FileAttachment
                state="done"
                fileName={file.name}
                fileSize={file.size}
                contentType={file.type}
                actions={
                  <DownloadTrigger
                    objectKey={file.key}
                    fileName={file.name}
                    fileSize={file.size}
                    variant="ghost"
                    size="icon-xs"
                    aria-label={`Download ${file.name}`}
                    tooltip="Download"
                  >
                    <DownloadIcon />
                  </DownloadTrigger>
                }
              />
            </li>
          ))}
        </ul>
      </Download>
      <DownloadToasts download={download} />
    </div>
  );
}
```

## Installation [#installation]

<Tabs items={["npm","shadcn"]}>
<Tab value="npm">
```tsx
import {
  Download,
  DownloadProgress,
  DownloadToasts,
  DownloadTrigger,
} from "@dimah-s3/ui";
```
</Tab>

<Tab value="shadcn">
```npm
npx shadcn@latest add @dimah-s3/download
```

The parts land in `components/dimah-s3/download/`, one file each, such as
`@/components/dimah-s3/download/download-trigger`.
</Tab>
</Tabs>

Complete [Installation](https://s3.dimah.dev/docs/ui/installation) first: the package or registry
setup, the stylesheet, and one `<Toaster />` in your layout.

## Usage [#usage]

```tsx
"use client";

import { useDownload } from "@dimah-s3/react";
import { Download, DownloadToasts, DownloadTrigger } from "@dimah-s3/ui";

type Invoice = { key: string; name: string };

export function InvoiceList({ invoices }: { invoices: Invoice[] }) {
  const download = useDownload({ route: "invoices" });

  return (
    <>
      <Download download={download}>
        <ul>
          {invoices.map((invoice) => (
            <li key={invoice.key}>
              {invoice.name}
              <DownloadTrigger
                objectKey={invoice.key}
                fileName={invoice.name}
                size="sm"
              />
            </li>
          ))}
        </ul>
      </Download>
      <DownloadToasts download={download} />
    </>
  );
}
```

## Anatomy [#anatomy]

```tsx
<Download download={download}>
  <DownloadTrigger objectKey={key} />
  <DownloadProgress objectKey={key} />
</Download>
<DownloadToasts download={download} />
```

| Part               | Description                                                     |
| ------------------ | --------------------------------------------------------------- |
| `Download`         | Root. Holds the hook. Renders no element                        |
| `DownloadTrigger`  | A `Button` that downloads one object                            |
| `DownloadProgress` | A progress bar for the running fetch download of one object     |
| `DownloadToasts`   | Started, progress, done, and failed toasts. Mount once per hook |

## Examples [#examples]

### Progress bar [#progress-bar]

`DownloadProgress` shows the bytes of a fetch download, next to the trigger
that started it.

```tsx title="download-progress-demo.tsx"
"use client";

import { useFetchDownload } from "@dimah-s3/react";
import {
  Download,
  DownloadProgress,
  DownloadToasts,
  DownloadTrigger,
} from "@dimah-s3/ui";

const REPORT = {
  key: "uploads/5e8b/annual-report-2026.pdf",
  name: "annual-report-2026.pdf",
  size: 18 * 1024 * 1024,
};

export function DownloadProgressDemo() {
  const download = useFetchDownload({ route: "uploads" });

  return (
    <div className="mx-auto flex w-full max-w-sm flex-col items-start gap-3">
      <Download download={download}>
        {/* While the file downloads, the button fills and turns into Cancel. */}
        <DownloadTrigger
          objectKey={REPORT.key}
          fileName={REPORT.name}
          fileSize={REPORT.size}
        >
          Download annual report
        </DownloadTrigger>
        <DownloadProgress objectKey={REPORT.key} />
      </Download>
      <DownloadToasts download={download} />
    </div>
  );
}
```

### Button styles [#button-styles]

`DownloadTrigger` is a `Button`: pass `variant`, `size`, and `children` to
restyle it. At an icon size it shows the icon alone; pass an `aria-label` that
names the file.

```tsx title="download-variants-demo.tsx"
"use client";

import { DownloadIcon, FileDownIcon } from "lucide-react";
import { useDownload } from "@dimah-s3/react";
import { Download, DownloadToasts, DownloadTrigger } from "@dimah-s3/ui";

const KEY = "uploads/2c9a/price-list.pdf";

export function DownloadVariantsDemo() {
  // Signs a URL and hands it to the browser, which saves the file.
  const download = useDownload({ route: "uploads" });

  return (
    <div className="flex flex-wrap items-center justify-center gap-2">
      <Download download={download}>
        <DownloadTrigger objectKey={KEY} fileName="price-list.pdf" />
        <DownloadTrigger
          objectKey={KEY}
          fileName="price-list.pdf"
          variant="default"
        />
        <DownloadTrigger
          objectKey={KEY}
          fileName="price-list.pdf"
          variant="secondary"
          size="sm"
        >
          <FileDownIcon data-icon="inline-start" />
          Price list (PDF)
        </DownloadTrigger>
        <DownloadTrigger
          objectKey={KEY}
          fileName="price-list.pdf"
          variant="link"
        >
          Download the price list
        </DownloadTrigger>
        <DownloadTrigger
          objectKey={KEY}
          fileName="price-list.pdf"
          variant="ghost"
          size="icon"
          aria-label="Download price-list.pdf"
          tooltip="Download"
        >
          <DownloadIcon />
        </DownloadTrigger>
      </Download>
      <DownloadToasts download={download} />
    </div>
  );
}
```

## Navigate or fetch [#navigate-or-fetch]

| Hook               | What happens                                            | While it runs                                        |
| ------------------ | ------------------------------------------------------- | ---------------------------------------------------- |
| `useDownload`      | Signs a URL and hands it to the browser, which saves it | The trigger shows a spinner                          |
| `useFetchDownload` | Fetches the bytes in the page, then saves them          | The trigger fills with the progress; a click cancels |

Use `useFetchDownload` for progress and cancel:

```tsx
const download = useFetchDownload({ route: "exports" });

<Download download={download}>
  <DownloadTrigger objectKey={report.key} fileSize={report.size} />
  <DownloadProgress objectKey={report.key} />
</Download>;
```

## Toasts [#toasts]

Mount `DownloadToasts` once per hook. It shows when a download starts,
finishes, fails, or is cancelled, with a progress toast for fetch downloads.

## Accessibility [#accessibility]

- At an icon size the trigger keeps "Download" as its accessible name.
- While a fetch download runs, the trigger is named `Cancel download of {name}`.
- `DownloadProgress` is a `progressbar` named after the file.

## API Reference [#api-reference]

### Download [#download]

| Prop       | Type                | Default  | Description                                  |
| ---------- | ------------------- | -------- | -------------------------------------------- |
| `download` | `AnyDownloadReturn` | required | A `useDownload` or `useFetchDownload` return |
| `children` | `ReactNode`         | required | The parts                                    |

### DownloadTrigger [#downloadtrigger]

Also accepts every `Button` prop.

| Prop        | Type             | Default             | Description                                                  |
| ----------- | ---------------- | ------------------- | ------------------------------------------------------------ |
| `objectKey` | `string`         | required            | Key of the object to download                                |
| `fileName`  | `string`         | —                   | Name the browser saves                                       |
| `fileSize`  | `number \| null` | —                   | Size in bytes. The progress total until the response has one |
| `tooltip`   | `ReactNode`      | —                   | Tooltip on hover and focus                                   |
| `children`  | `ReactNode`      | Icon and "Download" | Replaces the icon and label                                  |

### DownloadProgress [#downloadprogress]

Also accepts every `div` prop except `children`.

| Prop        | Type     | Default  | Description                 |
| ----------- | -------- | -------- | --------------------------- |
| `objectKey` | `string` | required | Key of the download to show |

### DownloadToasts [#downloadtoasts]

| Prop       | Type                | Default  | Description               |
| ---------- | ------------------- | -------- | ------------------------- |
| `download` | `AnyDownloadReturn` | required | The hook return to report |

### useDownloadContext [#usedownloadcontext]

Returns `{ download }`, the root's hook, inside `Download`. Call
`download.download(key)` and read `download.get(key)` to build your own
trigger.