vuestic-ui
Version:
Vue 3 UI Framework
1 lines • 2.7 kB
Source Map (JSON)
{"version":3,"file":"useToastService.mjs","sources":["../../../../../../src/components/va-toast/hooks/useToastService.ts"],"sourcesContent":["import { computed, getCurrentInstance, onBeforeUnmount, onMounted, Ref, ref, VNode } from 'vue'\nimport { ToastOptions } from '../types'\n\nconst GAP = 5\n\n// Expect as client-side used only\nconst toastInstances = ref([]) as Ref<VNode[]>\n\ntype OptionKeys = keyof ToastOptions;\n\nconst getNodeProps = (vNode: VNode): Record<OptionKeys, any> => {\n return (vNode.component?.props as Record<OptionKeys, any>) || {}\n}\n\nconst getTranslateValue = (item: VNode) => {\n if (item.el) {\n return (item.el.offsetHeight + GAP)\n }\n return 0\n}\n\nexport const useToastService = (props: {\n position: NonNullable<ToastOptions['position']>,\n}) => {\n const currentInstance = getCurrentInstance()!\n\n const yOffset = computed(() => {\n const currentIndex = toastInstances.value.findIndex((instance) => instance === currentInstance.vnode)\n\n if (currentIndex === -1) { return 0 }\n\n return toastInstances.value.slice(currentIndex + 1).reduce((acc, instance) => {\n const {\n position: itemPosition,\n } = getNodeProps(instance)\n\n const { position } = props\n\n if (position === itemPosition) {\n return getTranslateValue(instance) + acc\n }\n\n return acc\n }, 0)\n })\n\n onMounted(() => {\n toastInstances.value.unshift(currentInstance.vnode)\n })\n\n onBeforeUnmount(() => {\n toastInstances.value = toastInstances.value.filter((item) => item !== currentInstance.vnode)\n })\n\n return {\n yOffset,\n updateYOffset: () => {\n toastInstances.value = toastInstances.value.filter((item) => item !== currentInstance.vnode)\n },\n }\n}\n"],"names":[],"mappings":";AAGA,MAAM,MAAM;AAGZ,MAAM,iBAAiB,IAAI,CAAA,CAAE;AAI7B,MAAM,eAAe,CAAC,UAA0C;;AACtD,WAAA,WAAM,cAAN,mBAAiB,UAAqC;AAChE;AAEA,MAAM,oBAAoB,CAAC,SAAgB;AACzC,MAAI,KAAK,IAAI;AACH,WAAA,KAAK,GAAG,eAAe;AAAA,EACjC;AACO,SAAA;AACT;AAEa,MAAA,kBAAkB,CAAC,UAE1B;AACJ,QAAM,kBAAkB;AAElB,QAAA,UAAU,SAAS,MAAM;AACvB,UAAA,eAAe,eAAe,MAAM,UAAU,CAAC,aAAa,aAAa,gBAAgB,KAAK;AAEpG,QAAI,iBAAiB,IAAI;AAAS,aAAA;AAAA,IAAE;AAE7B,WAAA,eAAe,MAAM,MAAM,eAAe,CAAC,EAAE,OAAO,CAAC,KAAK,aAAa;AACtE,YAAA;AAAA,QACJ,UAAU;AAAA,MAAA,IACR,aAAa,QAAQ;AAEnB,YAAA,EAAE,SAAa,IAAA;AAErB,UAAI,aAAa,cAAc;AACtB,eAAA,kBAAkB,QAAQ,IAAI;AAAA,MACvC;AAEO,aAAA;AAAA,OACN,CAAC;AAAA,EAAA,CACL;AAED,YAAU,MAAM;AACC,mBAAA,MAAM,QAAQ,gBAAgB,KAAK;AAAA,EAAA,CACnD;AAED,kBAAgB,MAAM;AACL,mBAAA,QAAQ,eAAe,MAAM,OAAO,CAAC,SAAS,SAAS,gBAAgB,KAAK;AAAA,EAAA,CAC5F;AAEM,SAAA;AAAA,IACL;AAAA,IACA,eAAe,MAAM;AACJ,qBAAA,QAAQ,eAAe,MAAM,OAAO,CAAC,SAAS,SAAS,gBAAgB,KAAK;AAAA,IAC7F;AAAA,EAAA;AAEJ;"}