bootstrap-vue-next
Version:
Seamless integration of Vue 3, Bootstrap 5, and TypeScript for modern, type-safe UI development
1 lines • 7.21 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/composables/useToast/index.ts"],"sourcesContent":["import {\n inject,\n isReadonly,\n isRef,\n markRaw,\n onScopeDispose,\n type Ref,\n shallowRef,\n toValue,\n watch,\n} from 'vue'\nimport {orchestratorRegistryKey} from '../../utils/keys'\nimport type {ContainerPosition} from '../../types/Alignment'\nimport type {\n OrchestratorCreateOptions,\n PromiseWithComponent,\n ToastOrchestratorArrayValue,\n ToastOrchestratorCreateParam,\n ToastOrchestratorParam,\n} from '../../types/ComponentOrchestratorTypes'\nimport {buildPromise} 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 }\n\n const {store, _isToastAppend, _isOrchestratorInstalled} = orchestratorRegistry\n\n /**\n * @returns {PromiseWithComponent<typeof BToast, ToastOrchestratorParam>} Returns a promise object with methods to control the toast (show, hide, toggle, get, set, destroy)\n */\n const create = (\n obj: ToastOrchestratorCreateParam = {},\n options: OrchestratorCreateOptions = {}\n ): PromiseWithComponent<typeof BToast, ToastOrchestratorParam> => {\n if (!_isOrchestratorInstalled.value) {\n throw new Error('The BApp component must be mounted to use the toast composable')\n }\n\n const resolvedProps = isRef(obj) ? obj : shallowRef(obj)\n\n const _self = resolvedProps.value?.id || Symbol('Toast controller')\n const promise = buildPromise<\n typeof BToast,\n ToastOrchestratorParam,\n ToastOrchestratorArrayValue\n >(_self, store as Ref<ToastOrchestratorArrayValue[]>)\n\n promise.stop = watch(\n resolvedProps,\n (_newValue) => {\n const newValue = {...toValue(_newValue)}\n const previousIndex = store.value.findIndex((el) => el._self === _self)\n const previous =\n previousIndex === -1\n ? {_component: markRaw(BToast)}\n : (store.value[previousIndex] as ToastOrchestratorArrayValue)\n const v: ToastOrchestratorArrayValue = {\n ...previous,\n type: 'toast',\n _self,\n promise,\n options,\n }\n if (newValue.props) {\n Object.assign(v, newValue.props)\n newValue.props = undefined\n }\n if (newValue.pos) {\n v.position = newValue.pos\n newValue.pos = undefined\n }\n for (const key in newValue) {\n if (key.startsWith('on')) {\n v[key as keyof ToastOrchestratorCreateParam] =\n newValue[key as keyof ToastOrchestratorCreateParam]\n } else if (key === 'component' && newValue.component) {\n v._component = markRaw(newValue.component)\n } else if (key === 'slots' && newValue.slots) {\n v.slots = markRaw(newValue.slots)\n } else {\n v[key as keyof ToastOrchestratorCreateParam] = toValue(\n newValue[key as keyof ToastOrchestratorCreateParam]\n )\n }\n }\n v.position = v.position || posDefault\n v.modelValue = v.modelValue ?? 5000\n v['onUpdate:modelValue'] = (val: boolean) => {\n newValue['onUpdate:modelValue']?.(val)\n const {modelValue} = toValue(obj)\n if (isRef(obj) && !isRef(modelValue)) obj.value.modelValue = val\n if (isRef(modelValue) && !isReadonly(modelValue)) {\n ;(modelValue as Ref<ToastOrchestratorArrayValue['modelValue']>).value = val\n }\n if (v.modelValue !== val) {\n const toast = store.value.find((el) => el._self === _self)\n if (toast) {\n toast.modelValue = val\n }\n }\n }\n if (previousIndex === -1) {\n if (\n resolvedProps.value?.appendToast !== undefined\n ? resolvedProps.value.appendToast\n : _isToastAppend.value\n ) {\n store.value.push(v)\n } else {\n store.value.unshift(v)\n }\n } else {\n store.value.splice(previousIndex, 1, v)\n }\n },\n {\n deep: true,\n immediate: true,\n }\n )\n onScopeDispose(() => {\n const toast = store.value.find((el) => el._self === _self)\n if (toast) {\n toast.promise.value.destroy?.()\n }\n }, true)\n return promise.value\n }\n\n /*\n * @deprecated The `show` method is deprecated. Use the `create` method instead.\n */\n const show = (\n obj: ToastOrchestratorCreateParam = {}\n ): PromiseWithComponent<typeof BToast, ToastOrchestratorParam> => {\n \n console.warn(\n '[BootstrapVueNext] useToast: The `show` method is deprecated. Use the `create` method instead.'\n )\n return create(obj)\n }\n\n return {\n _isToastAppend,\n _isOrchestratorInstalled,\n store,\n create,\n show,\n }\n}\n\n/**\n * @deprecated use useToast() instead.\n * @returns {ReturnType<typeof useToast>} The toast controller\n */\nexport const useToastController = useToast\n"],"mappings":";;;;;;AAuBA,IAAM,aAAgC;AAEtC,IAAa,iBAAiB;CAC5B,MAAM,uBAAuB,OAAO,yBAAyB,KAAK;AAClE,KAAI,CAAC,qBACH,OAAM,IAAI,MACR,wGACD;CAGH,MAAM,EAAC,OAAO,gBAAgB,6BAA4B;;;;CAK1D,MAAM,UACJ,MAAoC,EAAE,EACtC,UAAqC,EAAE,KACyB;AAChE,MAAI,CAAC,yBAAyB,MAC5B,OAAM,IAAI,MAAM,iEAAiE;EAGnF,MAAM,gBAAgB,MAAM,IAAI,GAAG,MAAM,WAAW,IAAI;EAExD,MAAM,QAAQ,cAAc,OAAO,MAAM,OAAO,mBAAmB;EACnE,MAAM,UAAU,aAId,OAAO,MAA4C;AAErD,UAAQ,OAAO,MACb,gBACC,cAAc;GACb,MAAM,WAAW,EAAC,GAAG,QAAQ,UAAU,EAAC;GACxC,MAAM,gBAAgB,MAAM,MAAM,WAAW,OAAO,GAAG,UAAU,MAAM;GAKvE,MAAM,IAAiC;IACrC,GAJA,kBAAkB,KACd,EAAC,YAAY,QAAQ,eAAO,EAAC,GAC5B,MAAM,MAAM;IAGjB,MAAM;IACN;IACA;IACA;IACD;AACD,OAAI,SAAS,OAAO;AAClB,WAAO,OAAO,GAAG,SAAS,MAAM;AAChC,aAAS,QAAQ,KAAA;;AAEnB,OAAI,SAAS,KAAK;AAChB,MAAE,WAAW,SAAS;AACtB,aAAS,MAAM,KAAA;;AAEjB,QAAK,MAAM,OAAO,SAChB,KAAI,IAAI,WAAW,KAAK,CACtB,GAAE,OACA,SAAS;YACF,QAAQ,eAAe,SAAS,UACzC,GAAE,aAAa,QAAQ,SAAS,UAAU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