UNPKG

bootstrap-vue-next

Version:

Seamless integration of Vue 3, Bootstrap 5, and TypeScript for modern, type-safe UI development

1 lines 9.62 kB
{"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/composables/useModal/index.ts"],"sourcesContent":["import {\n type ComponentInternalInstance,\n type ComponentPublicInstance,\n computed,\n type ComputedRef,\n getCurrentInstance,\n inject,\n markRaw,\n type MaybeRef,\n onScopeDispose,\n type Ref,\n ref,\n toValue,\n} from 'vue'\nimport {useSharedModalStack} from '../useModalManager'\n\nimport {orchestratorRegistryKey, type OrchestratorStoreObject} from '../../utils/keys'\nimport type {\n ComponentController,\n ControllerKey,\n ModalOrchestratorArrayValue,\n ModalOrchestratorCreateParamBase,\n} from '../../types'\nimport {buildController, getOrchestratorControllerId} from '../orchestratorShared'\nimport {BModal} from '../../components'\n\nexport const useModal = () => {\n const orchestratorRegistry = inject(orchestratorRegistryKey, null)\n if (!orchestratorRegistry)\n throw Error(\n 'useModal() must be called within setup(), and BApp, useRegistry or plugin must be installed/provided.'\n )\n const {store, _isOrchestratorInstalled} = orchestratorRegistry\n\n /**\n * @returns {ComponentController<typeof BModal, Ref<ModalOrchestratorArrayValue>>}\n */\n // Uses a `function` declaration (rather than a generic arrow function assigned to a const) so\n // that TypeScript preserves per-call generic inference when `create` is returned as part of\n // `useModal()`'s inferred return object.\n function create<\n ComponentProps extends Record<string, unknown> = Record<string, unknown>,\n T extends ModalOrchestratorCreateParamBase<ComponentProps> = ModalOrchestratorCreateParamBase<ComponentProps>,\n >(\n obj: MaybeRef<T> = {} as T\n ): ComponentController<typeof BModal, Ref<ModalOrchestratorArrayValue>> {\n if (!_isOrchestratorInstalled.value)\n throw new Error('BApp component must be mounted to use the modal controller')\n\n const modalComp = markRaw(BModal)\n const resolvedProps = ref(obj)\n const modalStore = computed(() => store.value.modal)\n const {htmlAttributeId, storeId} = getOrchestratorControllerId(resolvedProps.value.id)\n\n const {resolve, controller} = buildController<\n typeof BModal,\n ComputedRef<OrchestratorStoreObject['modal']>\n >(storeId, modalStore)\n\n const value = computed<ModalOrchestratorArrayValue>({\n get: () => {\n const {component = modalComp, options, slots, ...props} = resolvedProps.value\n\n return {\n component,\n options,\n slots,\n fns: {\n resolve,\n setRef: (v: ComponentPublicInstance) => {\n controller.ref = v\n },\n destroy: controller.destroy,\n },\n id: storeId,\n props: {\n ...props,\n id: htmlAttributeId,\n },\n }\n },\n set: (v) => {\n resolvedProps.value = {\n ...resolvedProps.value,\n ...v.props,\n }\n },\n })\n\n modalStore.value.set(storeId, value)\n\n onScopeDispose(async () => {\n await controller[Symbol.asyncDispose]()\n }, true)\n return controller\n }\n\n const {lastStack, stack, registry} = useSharedModalStack()\n /**\n * Show a modal\n * @param id - The id of the modal to show\n */\n const show = (id?: ControllerKey) => {\n if (id === undefined) {\n if (lastStack?.value) {\n lastStack?.value.exposed?.show()\n }\n } else {\n const stackModal = stack?.value.find((modal) => modal.exposed?.id === id)\n if (stackModal) {\n stackModal.exposed?.show()\n return\n }\n const modal = store.value.modal.get(id)\n if (modal?.value) {\n modal.value = {\n ...modal.value,\n props: {\n ...modal.value.props,\n modelValue: true,\n },\n }\n } else {\n stack?.value.forEach((modal) => {\n if (modal.exposed?.id === id) {\n modal.exposed?.show()\n }\n })\n }\n }\n }\n\n /**\n * Hide a modal\n * @param trigger - The trigger to hide the modal\n * @param id - The id of the modal to hide\n */\n const hide = (trigger?: string, id?: ControllerKey) => {\n if (id === undefined) {\n if (lastStack?.value) {\n lastStack?.value.exposed?.hide(trigger)\n }\n } else {\n const stackModal = stack?.value.find((modal) => modal.exposed?.id === id)\n if (stackModal) {\n stackModal.exposed?.hide(trigger)\n return\n }\n const modal = store.value.modal.get(id)\n if (modal?.value) {\n modal.value = {\n ...modal.value,\n props: {\n ...modal.value.props,\n modelValue: false,\n },\n }\n } else {\n stack?.value.forEach((modal) => {\n if (modal.exposed?.id === id) {\n modal.exposed?.hide(trigger, true)\n }\n })\n }\n }\n }\n\n /**\n * Hide all modals\n * @param trigger - The trigger to hide all modals\n */\n const hideAll = (trigger?: string) => {\n stack?.value.forEach((modal) => {\n modal.exposed?.hide(trigger, true)\n })\n }\n\n const get = (id: ControllerKey) => {\n const modal = store.value.modal.get(id)\n if (modal) {\n return {\n modal,\n show() {\n show(id)\n },\n hide(trigger?: string) {\n hide(trigger, id)\n },\n }\n }\n if (registry?.value) {\n for (const [, modal] of registry?.value.entries() ?? []) {\n if (toValue(modal.exposed?.id) === id) {\n return {\n modal,\n show() {\n modal.exposed?.show()\n },\n hide(trigger?: string) {\n modal.exposed?.hide(trigger, true)\n },\n }\n }\n }\n }\n return null\n }\n\n const instance = getCurrentInstance()\n const current = () => {\n const findBModal = (component: ComponentInternalInstance): ComponentInternalInstance | null => {\n if (!component.parent) {\n return null\n }\n if (component.parent.type === BModal) {\n return component.parent\n }\n return findBModal(component.parent)\n }\n if (!instance) {\n return null\n }\n const modalComponent = computed(() => findBModal(instance))\n const modal = computed(() => modalComponent.value?.proxy)\n return {\n show() {\n modalComponent.value?.exposed?.show()\n },\n hide(trigger?: string) {\n modalComponent.value?.exposed?.hide(trigger, true)\n },\n modal,\n }\n }\n\n return {\n show,\n hide,\n hideAll,\n get,\n current,\n create,\n _isOrchestratorInstalled,\n store,\n // Todo: Supports listening events globally in the future\n }\n}\n"],"mappings":";;;;;AA0BA,IAAa,iBAAiB;CAC5B,MAAM,uBAAuB,OAAO,yBAAyB,IAAI;CACjE,IAAI,CAAC,sBACH,MAAM,MACJ,uGACF;CACF,MAAM,EAAC,OAAO,6BAA4B;;;;CAQ1C,SAAS,OAIP,MAAmB,CAAC,GACkD;EACtE,IAAI,CAAC,yBAAyB,OAC5B,MAAM,IAAI,MAAM,4DAA4D;EAE9E,MAAM,YAAY,QAAQ,cAAM;EAChC,MAAM,gBAAgB,IAAI,GAAG;EAC7B,MAAM,aAAa,eAAe,MAAM,MAAM,KAAK;EACnD,MAAM,EAAC,iBAAiB,YAAW,4BAA4B,cAAc,MAAM,EAAE;EAErF,MAAM,EAAC,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