bootstrap-vue-next
Version:
Seamless integration of Vue 3, Bootstrap 5, and TypeScript for modern, type-safe UI development
1 lines • 4.26 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/composables/useToast/index.ts"],"sourcesContent":["import {\n type ComponentPublicInstance,\n computed,\n type ComputedRef,\n inject,\n markRaw,\n type MaybeRef,\n onScopeDispose,\n ref,\n type Ref,\n} from 'vue'\nimport {orchestratorRegistryKey, type OrchestratorStoreObject} from '../../utils/keys'\nimport type {ContainerPosition} from '../../types/Alignment'\nimport type {\n ComponentController,\n ToastOrchestratorArrayValue,\n ToastOrchestratorCreateParamBase,\n} from '../../types'\nimport {buildController, getOrchestratorControllerId} from '../orchestratorShared'\nimport {BToast} from '../../components'\n\nconst posDefault: ContainerPosition = 'top-end'\n\nexport const useToast = () => {\n const orchestratorRegistry = inject(orchestratorRegistryKey, null)\n if (!orchestratorRegistry)\n throw new Error(\n 'useToast() must be called within setup(), and BApp, useRegistry or plugin must be installed/provided.'\n )\n const {store, _isToastAppend, _isOrchestratorInstalled} = orchestratorRegistry\n\n /**\n * @returns {ComponentController<typeof BToast, ToastOrchestratorParam>}\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 // `useToast()`'s inferred return object.\n function create<\n ComponentProps extends Record<string, unknown> = Record<string, unknown>,\n T extends ToastOrchestratorCreateParamBase<ComponentProps> = ToastOrchestratorCreateParamBase<ComponentProps>,\n >(\n obj: MaybeRef<T> = {} as T\n ): ComponentController<typeof BToast, Ref<ToastOrchestratorArrayValue>> {\n if (!_isOrchestratorInstalled.value)\n throw new Error('The BApp component must be mounted to use the toast composable')\n\n const toastComp = markRaw(BToast)\n const resolvedProps = ref(obj)\n const toastStore = computed(() => store.value.toast)\n const {htmlAttributeId, storeId} = getOrchestratorControllerId(resolvedProps.value.id)\n\n const {resolve, controller} = buildController<\n typeof BToast,\n ComputedRef<OrchestratorStoreObject['toast']>\n >(storeId, toastStore)\n\n const value = computed<ToastOrchestratorArrayValue>({\n get: () => {\n const {component = toastComp, options, slots, ...props} = resolvedProps.value\n\n return {\n component,\n options,\n slots,\n id: storeId,\n fns: {\n resolve,\n setRef: (v: ComponentPublicInstance) => {\n controller.ref = v\n },\n destroy: controller.destroy,\n },\n props: {\n ...props,\n id: htmlAttributeId,\n position: props.position || posDefault,\n },\n }\n },\n set: (v) => {\n resolvedProps.value = {\n ...resolvedProps.value,\n ...v.props,\n }\n },\n })\n\n toastStore.value.set(storeId, value)\n\n onScopeDispose(async () => {\n await controller[Symbol.asyncDispose]()\n }, true)\n\n return controller\n }\n\n return {\n _isToastAppend,\n _isOrchestratorInstalled,\n store,\n create,\n }\n}\n"],"mappings":";;;;;AAqBA,IAAM,aAAgC;AAEtC,IAAa,iBAAiB;CAC5B,MAAM,uBAAuB,OAAO,yBAAyB,IAAI;CACjE,IAAI,CAAC,sBACH,MAAM,IAAI,MACR,uGACF;CACF,MAAM,EAAC,OAAO,gBAAgB,6BAA4B;;;;CAQ1D,SAAS,OAIP,MAAmB,CAAC,GACkD;EACtE,IAAI,CAAC,yBAAyB,OAC5B,MAAM,IAAI,MAAM,gEAAgE;EAElF,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,SAAS,eAAc,gBAG5B,SAAS,UAAU;EAErB,MAAM,QAAQ,SAAsC;GAClD,WAAW;IACT,MAAM,EAAC,YAAY,WAAW,SAAS,OAAO,GAAG,UAAS,cAAc;IAExE,OAAO;KACL;KACA;KACA;KACA,IAAI;KACJ,KAAK;MACH;MACA,SAAS,MAA+B;OACtC,WAAW,MAAM;MACnB;MACA,SAAS,WAAW;KACtB;KACA,OAAO;MACL,GAAG;MACH,IAAI;MACJ,UAAU,MAAM,YAAY;KAC9B;IACF;GACF;GACA,MAAM,MAAM;IACV,cAAc,QAAQ;KACpB,GAAG,cAAc;KACjB,GAAG,EAAE;IACP;GACF;EACF,CAAC;EAED,WAAW,MAAM,IAAI,SAAS,KAAK;EAEnC,eAAe,YAAY;GACzB,MAAM,WAAW,OAAO,aAAa,CAAC;EACxC,GAAG,IAAI;EAEP,OAAO;CACT;CAEA,OAAO;EACL;EACA;EACA;EACA;CACF;AACF"}