UNPKG

bootstrap-vue-next

Version:

BootstrapVueNext is an early and lovely component library for Vue 3 & Nuxt 3 based on Bootstrap 5 and Typescript.

1 lines 4.33 kB
{"version":3,"file":"useModalManager-sahbfe2p.mjs","sources":["../src/composables/useModalManager.ts"],"sourcesContent":["import {getSSRHandler, tryOnScopeDispose, unrefElement} from '@vueuse/core'\nimport {\n type ComponentInternalInstance,\n computed,\n getCurrentInstance,\n inject,\n type Ref,\n toValue,\n watch,\n} from 'vue'\nimport {modalManagerPluginKey} from '../utils/keys'\n\nconst modalOpenClassName = 'modal-open'\n\nexport const useSharedModalStack = () => {\n const modalManagerPlugin = inject(modalManagerPluginKey)\n\n /**\n * Removes an item from both the stack and registry\n */\n const dispose = (modal: Readonly<ComponentInternalInstance>): void => {\n modalManagerPlugin?.removeStack(modal)\n modalManagerPlugin?.removeRegistry(modal)\n }\n\n const updateHTMLAttrs = getSSRHandler('updateHTMLAttrs', (selector, attribute, value) => {\n const el =\n typeof selector === 'string'\n ? window?.document.querySelector(selector)\n : unrefElement(selector)\n if (!el) return\n\n if (attribute === 'class') {\n el.classList.toggle(modalOpenClassName, value === modalOpenClassName)\n } else {\n el.setAttribute(attribute, value)\n }\n })\n\n tryOnScopeDispose(() => {\n updateHTMLAttrs('body', 'class', '')\n })\n\n watch(\n () => modalManagerPlugin?.countStack.value,\n (newValue) => {\n if (newValue === undefined) return\n updateHTMLAttrs('body', 'class', newValue > 0 ? modalOpenClassName : '')\n }\n )\n\n return {\n ...modalManagerPlugin,\n dispose,\n }\n}\n\nexport const useModalManager = (modalOpen: Readonly<Ref<boolean>>, initialValue: boolean) => {\n const {pushRegistry, pushStack, removeStack, stack, dispose, countStack} = useSharedModalStack()\n\n const currentModal = getCurrentInstance()\n\n if (!currentModal || currentModal.type.__name !== 'BModal') {\n throw new Error('useModalManager must only use in BModal component')\n }\n\n pushRegistry?.(currentModal)\n\n tryOnScopeDispose(() => {\n dispose(currentModal)\n })\n\n const setInStack = (newValue: boolean, oldValue: boolean) => {\n if (newValue) {\n pushStack?.(currentModal)\n } else if (oldValue && !newValue) {\n removeStack?.(currentModal)\n }\n }\n\n // (initialValue, initialValue) is meant to always only ever trigger the first `if (newValue) {` block. The other block is skipped _always_\n setInStack(initialValue, initialValue)\n\n watch(modalOpen, setInStack)\n\n return {\n activePosition: computed(() =>\n stack?.value.findIndex((el) => toValue(el.exposed?.id) === toValue(currentModal.exposed?.id))\n ),\n activeModalCount: countStack,\n stackWithoutSelf: computed(\n () =>\n stack?.value.filter(\n (el) => toValue(el.exposed?.id) !== toValue(currentModal.exposed?.id)\n ) ?? []\n ),\n }\n}\n"],"names":[],"mappings":";;;;AAYA,MAAM,qBAAqB;AAEpB,MAAM,sBAAsB,MAAM;AACjC,QAAA,qBAAqB,OAAO,qBAAqB;AAKjD,QAAA,UAAU,CAAC,UAAqD;AACpE,6DAAoB,YAAY;AAChC,6DAAoB,eAAe;AAAA,EACrC;AAEA,QAAM,kBAAkB,cAAc,mBAAmB,CAAC,UAAU,WAAW,UAAU;AACjF,UAAA,KACJ,OAAO,aAAa,WAChB,iCAAQ,SAAS,cAAc,YAC/B,aAAa,QAAQ;AAC3B,QAAI,CAAC,GAAI;AAET,QAAI,cAAc,SAAS;AACzB,SAAG,UAAU,OAAO,oBAAoB,UAAU,kBAAkB;AAAA,IAAA,OAC/D;AACF,SAAA,aAAa,WAAW,KAAK;AAAA,IAAA;AAAA,EAClC,CACD;AAED,oBAAkB,MAAM;AACN,oBAAA,QAAQ,SAAS,EAAE;AAAA,EAAA,CACpC;AAED;AAAA,IACE,MAAM,yDAAoB,WAAW;AAAA,IACrC,CAAC,aAAa;AACZ,UAAI,aAAa,OAAW;AAC5B,sBAAgB,QAAQ,SAAS,WAAW,IAAI,qBAAqB,EAAE;AAAA,IAAA;AAAA,EAE3E;AAEO,SAAA;AAAA,IACL,GAAG;AAAA,IACH;AAAA,EACF;AACF;AAEa,MAAA,kBAAkB,CAAC,WAAmC,iBAA0B;AACrF,QAAA,EAAC,cAAc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