DownloadButton
Downloads a file. A file that exists is a link with download; one the app must make first shows the band of light while it works.
import { DownloadButton } from '@sypnia/ui/react'
A file that exists and one that is made
Code
import { DownloadButton, toast, Toaster } from '@sypnia/ui/react';
// A file that exists is a link with `download`; one the app has to make first shows it is working.
const csv = 'data:text/csv;charset=utf-8,' + encodeURIComponent('id,total\n1,120\n2,80\n');
export default function Files() {
return (
<>
<div className="sy-row">
<DownloadButton href={csv} filename="orders.csv">Download CSV</DownloadButton>
<DownloadButton variant="quiet" onPress={() => new Promise<void>((r) => setTimeout(() => { r(); toast('The backup is ready', { tone: 'ok' }); }, 1500))}>Download the backup</DownloadButton>
</div>
<Toaster />
</>
);
} When to use it
Use it when
- Exports, backups, invoices.
- Say what is downloaded and in what format: «Download CSV».
Do not use it when
- Opening a file to read in the browser: use a Link.
- Long exports that take minutes: start them and tell the person when they are ready.
For everyone
- With
hrefit is a link; withonPressa button that stays focusable while busy. - Its words say what is downloaded, not only «Download».
Props
| DownloadButton | Type | Default |
|---|---|---|
busy | boolean | |
disabled | boolean | |
filenameThe name the file is saved with. | string | |
href | string | |
onPress | (() => void | Promise<void>) | |
size | "sm" | "md" | "lg" | |
variant | "primary" | "secondary" | "quiet" | secondary |