-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.ts
54 lines (45 loc) · 1.42 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import type { MaybeComputedRef } from '@vueuse/shared'
import { resolveUnref } from '@vueuse/shared'
import { useSupported } from '../useSupported'
import type { ConfigurableNavigator } from '../_configurable'
import { defaultNavigator } from '../_configurable'
export interface UseShareOptions {
title?: string
files?: File[]
text?: string
url?: string
}
interface NavigatorWithShare {
share?: (data: UseShareOptions) => Promise<void>
canShare?: (data: UseShareOptions) => boolean
}
/**
* Reactive Web Share API.
*
* @see https://vueuse.org/useShare
* @param shareOptions
* @param options
*/
export function useShare(shareOptions: MaybeComputedRef<UseShareOptions> = {}, options: ConfigurableNavigator = {}) {
const { navigator = defaultNavigator } = options
const _navigator = (navigator as NavigatorWithShare)
const isSupported = useSupported(() => _navigator && 'canShare' in _navigator)
const share = async (overrideOptions: MaybeComputedRef<UseShareOptions> = {}) => {
if (isSupported.value) {
const data = {
...resolveUnref(shareOptions),
...resolveUnref(overrideOptions),
}
let granted = true
if (data.files && _navigator.canShare)
granted = _navigator.canShare({ files: data.files })
if (granted)
return _navigator.share!(data)
}
}
return {
isSupported,
share,
}
}
export type UseShareReturn = ReturnType<typeof useShare>