UNPKG

@ark-ui/solid

Version:

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

60 lines (55 loc) 1.61 kB
import { useEnvironmentContext } from "./HDGILMRT.jsx"; import { runIfFn } from "./KGOB2IMX.jsx"; import { ark } from "./UFYZ7HLU.jsx"; // src/components/download-trigger/download-trigger.tsx import { splitProps } from "solid-js"; // src/components/download-trigger/use-download.ts import { downloadFile } from "@zag-js/file-utils"; import { isFunction } from "@zag-js/utils"; var useDownload = (props) => { const env = useEnvironmentContext(); const download = () => { const { fileName, mimeType, data } = runIfFn(props); const saveToDisk = (value) => { downloadFile({ file: value, name: fileName, type: mimeType, win: env().getWindow() }); }; if (isFunction(data)) { const maybePromise = data(); if (maybePromise instanceof Promise) { maybePromise.then(saveToDisk); } else { saveToDisk(maybePromise); } } else { saveToDisk(data); } }; return { download }; }; // src/components/download-trigger/download-trigger.tsx function DownloadTrigger(props) { const [downloadProps, restProps] = splitProps(props, ["fileName", "data", "mimeType", "onClick"]); const { download } = useDownload(() => ({ fileName: downloadProps.fileName, mimeType: downloadProps.mimeType, data: downloadProps.data })); const handleClick = (e) => { if (typeof downloadProps.onClick === "function") { downloadProps.onClick(e); } if (e.defaultPrevented) return; download(); }; return <ark.button {...restProps} type="button" onClick={handleClick} />; } export { useDownload, DownloadTrigger };