Skip to content

useShare

A composable for the Web Share API. Share text, URLs, or files via native share sheet (mobile) or fallback. Pairs with useClipboard when share is unavailable.

Basic Usage

vue
<template>
  <BvButton v-if="isSupported" @click="handleShare">Share</BvButton>
</template>

<script setup>
import { BvButton } from "@baklavue/ui";
import { useShare, useNotification } from "@baklavue/composables";

const { share, isSupported } = useShare();
const { success } = useNotification();

const handleShare = async () => {
  const ok = await share({ title: "Report", url: "https://example.com/report" });
  if (ok) success({ description: "Shared!" });
};
</script>

With Default Data

vue
<script setup>
import { useShare, useNotification } from "@baklavue/composables";

const { success } = useNotification();
const { share, isSupported } = useShare({
  data: { title: "Check this out", url: window.location.href },
  onSuccess: () => success({ description: "Shared!" }),
});

await share(); // uses default data
</script>

API

Options

OptionTypeDescription
dataShareDataDefault data to share when share() is called without arguments
onSuccess() => voidCallback when share succeeds
onError(error?: Error) => voidCallback when share fails or is aborted

ShareData

PropertyTypeDescription
titlestringTitle of the shared content
textstringText to share
urlstringURL to share
filesFile[]Files to share (when supported)

Return Value

PropertyTypeDescription
share(data?: ShareData) => Promise<boolean>Share data. Uses default when called without args.
isSupportedComputedRef<boolean>Whether Web Share API is available
canShareComputedRef<boolean>Whether the current data can be shared
sharedRef<boolean>True after successful share
errorRef<Error | null>Last error if share failed

TypeScript Support

typescript
import { useShare, type ShareData, type UseShareOptions } from "@baklavue/composables";

const { share, isSupported } = useShare();
await share({ title: "Report", url: "https://example.com" });