UNPKG

bootstrap-vue-next

Version:

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

87 lines (86 loc) 3.56 kB
import { _ as orchestratorRegistryKey } from "../../../keys-CQKrwmvN.mjs"; import { buildPromise } from "../orchestratorShared/index.mjs"; import { t as BToast_default } from "../../../BToast-B2rFKhLD.mjs"; import "../../components/index.mjs"; import { inject, isReadonly, isRef, markRaw, onScopeDispose, shallowRef, toValue, watch } from "vue"; //#region src/composables/useToast/index.ts var posDefault = "top-end"; var useToast = () => { const orchestratorRegistry = inject(orchestratorRegistryKey, null); if (!orchestratorRegistry) throw new Error("useToast() must be called within setup(), and BApp, useRegistry or plugin must be installed/provided."); const { store, _isToastAppend, _isOrchestratorInstalled } = orchestratorRegistry; /** * @returns {PromiseWithComponent<typeof BToast, ToastOrchestratorParam>} Returns a promise object with methods to control the toast (show, hide, toggle, get, set, destroy) */ const create = (obj = {}, options = {}) => { if (!_isOrchestratorInstalled.value) throw new Error("The BApp component must be mounted to use the toast composable"); const resolvedProps = isRef(obj) ? obj : shallowRef(obj); const _self = resolvedProps.value?.id || Symbol("Toast controller"); const promise = buildPromise(_self, store); promise.stop = watch(resolvedProps, (_newValue) => { const newValue = { ...toValue(_newValue) }; const previousIndex = store.value.findIndex((el) => el._self === _self); const v = { ...previousIndex === -1 ? { _component: markRaw(BToast_default) } : store.value[previousIndex], type: "toast", _self, promise, options }; if (newValue.props) { Object.assign(v, newValue.props); newValue.props = void 0; } if (newValue.pos) { v.position = newValue.pos; newValue.pos = void 0; } for (const key in newValue) if (key.startsWith("on")) v[key] = newValue[key]; else if (key === "component" && newValue.component) v._component = markRaw(newValue.component); else if (key === "slots" && newValue.slots) v.slots = markRaw(newValue.slots); else v[key] = toValue(newValue[key]); v.position = v.position || posDefault; v.modelValue = v.modelValue ?? 5e3; v["onUpdate:modelValue"] = (val) => { newValue["onUpdate:modelValue"]?.(val); const { modelValue } = toValue(obj); if (isRef(obj) && !isRef(modelValue)) obj.value.modelValue = val; if (isRef(modelValue) && !isReadonly(modelValue)) modelValue.value = val; if (v.modelValue !== val) { const toast = store.value.find((el) => el._self === _self); if (toast) toast.modelValue = val; } }; if (previousIndex === -1) if (resolvedProps.value?.appendToast !== void 0 ? resolvedProps.value.appendToast : _isToastAppend.value) store.value.push(v); else store.value.unshift(v); else store.value.splice(previousIndex, 1, v); }, { deep: true, immediate: true }); onScopeDispose(() => { const toast = store.value.find((el) => el._self === _self); if (toast) toast.promise.value.destroy?.(); }, true); return promise.value; }; const show = (obj = {}) => { console.warn("[BootstrapVueNext] useToast: The `show` method is deprecated. Use the `create` method instead."); return create(obj); }; return { _isToastAppend, _isOrchestratorInstalled, store, create, show }; }; /** * @deprecated use useToast() instead. * @returns {ReturnType<typeof useToast>} The toast controller */ var useToastController = useToast; //#endregion export { useToast, useToastController }; //# sourceMappingURL=index.mjs.map