vuestic-ui
Version:
Vue 3 UI Framework
49 lines (48 loc) • 1.43 kB
JavaScript
import { ref, getCurrentInstance, computed, onMounted, onBeforeUnmount } from "vue";
const GAP = 5;
const toastInstances = ref([]);
const getNodeProps = (vNode) => {
var _a;
return ((_a = vNode.component) == null ? void 0 : _a.props) || {};
};
const getTranslateValue = (item) => {
if (item.el) {
return item.el.offsetHeight + GAP;
}
return 0;
};
const useToastService = (props) => {
const currentInstance = getCurrentInstance();
const yOffset = computed(() => {
const currentIndex = toastInstances.value.findIndex((instance) => instance === currentInstance.vnode);
if (currentIndex === -1) {
return 0;
}
return toastInstances.value.slice(currentIndex + 1).reduce((acc, instance) => {
const {
position: itemPosition
} = getNodeProps(instance);
const { position } = props;
if (position === itemPosition) {
return getTranslateValue(instance) + acc;
}
return acc;
}, 0);
});
onMounted(() => {
toastInstances.value.unshift(currentInstance.vnode);
});
onBeforeUnmount(() => {
toastInstances.value = toastInstances.value.filter((item) => item !== currentInstance.vnode);
});
return {
yOffset,
updateYOffset: () => {
toastInstances.value = toastInstances.value.filter((item) => item !== currentInstance.vnode);
}
};
};
export {
useToastService as u
};
//# sourceMappingURL=useToastService.js.map