Skip to Content
dimah-s3v2.8.1

Download

Download buttons, a progress bar, and toasts that render useDownload or useFetchDownload.

Download stored files with a button. Download holds one download hook, 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.

  • quarterly-report.pdf8.0 MB
  • brand-assets.zip24.0 MB
  • launch-video.mp416.0 MB

Installation

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

Complete Installation first: the package or registry setup, the stylesheet, and one <Toaster /> in your layout.

Usage

"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

<Download download={download}>
  <DownloadTrigger objectKey={key} />
  <DownloadProgress objectKey={key} />
</Download>
<DownloadToasts download={download} />
PartDescription
DownloadRoot. Holds the hook. Renders no element
DownloadTriggerA Button that downloads one object
DownloadProgressA progress bar for the running fetch download of one object
DownloadToastsStarted, progress, done, and failed toasts. Mount once per hook

Examples

Progress bar

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

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.

HookWhat happensWhile it runs
useDownloadSigns a URL and hands it to the browser, which saves itThe trigger shows a spinner
useFetchDownloadFetches the bytes in the page, then saves themThe trigger fills with the progress; a click cancels

Use useFetchDownload for progress and cancel:

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

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

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

  • 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

Download

PropTypeDefaultDescription
downloadAnyDownloadReturnrequiredA useDownload or useFetchDownload return
childrenReactNoderequiredThe parts

DownloadTrigger

Also accepts every Button prop.

PropTypeDefaultDescription
objectKeystringrequiredKey of the object to download
fileNamestring—Name the browser saves
fileSizenumber | null—Size in bytes. The progress total until the response has one
tooltipReactNode—Tooltip on hover and focus
childrenReactNodeIcon and "Download"Replaces the icon and label

DownloadProgress

Also accepts every div prop except children.

PropTypeDefaultDescription
objectKeystringrequiredKey of the download to show

DownloadToasts

PropTypeDefaultDescription
downloadAnyDownloadReturnrequiredThe hook return to report

useDownloadContext

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

On this page