UNPKG

@ark-ui/vue

Version:

A collection of unstyled, accessible UI components for Vue, utilizing state machines for seamless interaction.

28 lines (27 loc) 869 B
import { DEFAULT_ENVIRONMENT, useEnvironmentContext } from "../../providers/environment/use-environment-context.js"; import { toValue } from "vue"; import { downloadFile } from "@zag-js/file-utils"; //#region src/components/download-trigger/use-download.ts var useDownload = (props) => { const env = useEnvironmentContext(DEFAULT_ENVIRONMENT); const download = () => { const { fileName, mimeType, data } = toValue(props); const win = env?.value.getWindow() || window; const saveToDisk = (value) => { downloadFile({ file: value, name: fileName, type: mimeType, win }); }; if (typeof data === "function") { const maybePromise = data(); if (maybePromise instanceof Promise) maybePromise.then(saveToDisk); else saveToDisk(maybePromise); } else saveToDisk(data); }; return { download }; }; //#endregion export { useDownload };