@ark-ui/solid
Version:
A collection of unstyled, accessible UI components for Solid, utilizing state machines for seamless interaction.
54 lines (50 loc) • 1.59 kB
JavaScript
import { useEnvironmentContext } from './3P5T77QU.js';
import { runIfFn } from './DT73WLR4.js';
import { ark } from './EPLBB4QN.js';
import { createComponent, mergeProps } from 'solid-js/web';
import { splitProps } from 'solid-js';
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 createComponent(ark.button, mergeProps(restProps, {
type: "button",
onClick: handleClick
}));
}
export { DownloadTrigger, useDownload };