UNPKG

@arco-design/web-vue

Version:

Arco Design Vue 2.0: A Vue.js 3 UI Library

85 lines (84 loc) 2.07 kB
import { ref, watch, onMounted, onBeforeUnmount, readonly } from "vue"; const POPUP_BASE_Z_INDEX = 1e3; const MESSAGE_BASE_Z_INDEX = 5e3; const Z_INDEX_STEP = 1; class PopupManager { constructor() { this.popupStack = { popup: /* @__PURE__ */ new Set(), dialog: /* @__PURE__ */ new Set(), message: /* @__PURE__ */ new Set() }; this.getNextZIndex = (type) => { const current = type === "message" ? Array.from(this.popupStack.message).pop() || MESSAGE_BASE_Z_INDEX : Array.from(this.popupStack.popup).pop() || POPUP_BASE_Z_INDEX; return current + Z_INDEX_STEP; }; this.add = (type) => { const zIndex = this.getNextZIndex(type); this.popupStack[type].add(zIndex); if (type === "dialog") { this.popupStack.popup.add(zIndex); } return zIndex; }; this.delete = (zIndex, type) => { this.popupStack[type].delete(zIndex); if (type === "dialog") { this.popupStack.popup.delete(zIndex); } }; this.isLastDialog = (zIndex) => { if (this.popupStack.dialog.size > 1) { return zIndex === Array.from(this.popupStack.dialog).pop(); } return true; }; } } const popupManager = new PopupManager(); function usePopupManager(type, { visible, runOnMounted } = {}) { const zIndex = ref(0); const open = () => { zIndex.value = popupManager.add(type); }; const close = () => { popupManager.delete(zIndex.value, type); }; const isLastDialog = () => { if (type === "dialog") { return popupManager.isLastDialog(zIndex.value); } return false; }; watch( () => visible == null ? void 0 : visible.value, (visible2) => { if (visible2) { open(); } else { close(); } }, { immediate: true } ); if (runOnMounted) { onMounted(() => { open(); }); onBeforeUnmount(() => { close(); }); } return { zIndex: readonly(zIndex), open, close, isLastDialog }; } export { usePopupManager as default };