Download Trigger

A button that saves data, such as an image or a text file, to the user's device.

Usage

Code
import { DownloadTrigger } from '@leverui/components';
Code
<DownloadTrigger data="..." fileName="x.png" mimeType="image/png" />

Examples

Basic

Pass the data you want to download to the data prop, and specify the fileName and mimeType of the file.

Download SVG

Here's an example of how to download an SVG file.

Promise

You can also trigger downloads from a promise that returns a Blob, File, or string.

File Size

Compose the DownloadTrigger with the FormatByte component to display the size of the file in a human-readable format.

Guides

Styled child

Use asChild to render a Button as the trigger. Pass your handlers to getProps so they run before the trigger's. An onClick added after {...getProps()} replaces the trigger's handler.

Code
import { createSignal } from 'solid-js';
import { Button, DownloadTrigger } from '@leverui/components';

export function StyledDownload() {
  const [requested, setRequested] = createSignal(false);

  function handleChildClick() {
    setRequested(true);
  }

  return (
    <>
      <DownloadTrigger
        data="Hello"
        fileName="hello.txt"
        mimeType="text/plain"
        asChild={(getProps) => (
          <Button {...getProps({ onClick: handleChildClick })} variant="outline">
            Download
          </Button>
        )}
      />
      <output>{requested() ? 'Download requested' : 'Ready'}</output>
    </>
  );
}

Behavior

Default type="button", so it does not submit a form. onClick runs first. Call event.preventDefault() there to cancel the download. data, fileName, and mimeType are read on click, so signals can change them.

To download from elsewhere, call useDownload with { data, fileName, mimeType } and attach its download function to a button.

Props

PropDefaultTypeDescription
data—DownloadableData | (() => Promise…)String, Blob, or File, or a function returning one or a promise of one. Called on click.
fileName—stringSaved file name.
mimeType—FileMimeTypeSaved file MIME type.
type'button'stringButton type attribute.
disabled—booleanStop the trigger from starting a download.
asbuttonNative tag nameHTML tag to render.
asChild—Solid render functionRender your own element with the trigger's props.

No styles of its own. Render a Button as its child for sizes, variants, loading state, and color palettes.

Customize with CSS

Set class or style on the trigger, or target .leverui-download-trigger. Import @leverui/components/styles.css when you use it with styled components such as Button.