@sv-use/core
Version:
A collection of Svelte 5 utilities.
37 lines (36 loc) • 1.26 kB
JavaScript
import { normalizeValue } from '../__internal__/utils.svelte.js';
import { defaultNavigator } from '../__internal__/configurable.js';
/**
* Invokes the native sharing mechanism of the device to share data such as text, URLs, or files.
* @param data The data to share.
* @param options Additional options to customize the behavior.
* @see https://svelte-librarian.github.io/sv-use/docs/core/create-share
*/
export function createShare(data, options = {}) {
const { navigator = defaultNavigator } = options;
const _data = $derived({
files: normalizeValue(data.files),
text: normalizeValue(data.text),
title: normalizeValue(data.title),
url: normalizeValue(data.url)
});
const _navigator = navigator;
const isSupported = $derived(_navigator && 'canShare' in _navigator);
async function share() {
if (!isSupported)
return;
let granted = true;
if (data.files && _navigator.canShare) {
granted = _navigator.canShare({ files: normalizeValue(data.files) });
}
if (granted) {
return _navigator.share(_data);
}
}
return {
get isSupported() {
return isSupported;
},
share
};
}