UNPKG

sard-uniapp

Version:

sard-uniapp 是一套基于 Uniapp + Vue3 框架开发的兼容多端的 UI 组件库

59 lines (58 loc) 1.62 kB
import { provide, inject, onMounted, onUnmounted, useAttrs, ref, } from 'vue'; import { reactiveComputed } from '../../use'; export const shareSheetContextKey = Symbol('ShareSheetContext'); export function provideShareSheet(context) { provide(shareSheetContextKey, context); } export function useShareSheet() { const items = ref([]); let selectCallback = null; const setSelectCallback = (callback) => { selectCallback = callback; }; provide(shareSheetContextKey, { select: (item) => { const index = items.value.indexOf(item); selectCallback?.(item, index); }, addItem: (item) => { if (!items.value.includes(item)) { items.value.push(item); } }, removeItem: (item) => { const index = items.value.indexOf(item); if (index > -1) { items.value.splice(index, 1); } }, }); return { items, setSelectCallback, }; } export function useShareSheetItem(item) { const context = inject(shareSheetContextKey); if (!context) { throw new Error('ShareSheetItem must be included in ShareSheet.'); } const attrs = useAttrs(); const mergedItem = reactiveComputed(() => { return { ...item, ...attrs, }; }); onMounted(() => { context.addItem(mergedItem); }); onUnmounted(() => { context.removeItem(mergedItem); }); return { select: () => { context.select(mergedItem); }, }; }