bootstrap-vue-next
Version:
BootstrapVueNext is an early and lovely component library for Vue 3 & Nuxt 3 based on Bootstrap 5 and Typescript.
78 lines (77 loc) • 2.95 kB
JavaScript
import { g as getSSRHandler, f as unrefElement } from "./index-CMqRvrZx.mjs";
import { getCurrentInstance, watch, computed, toValue, inject } from "vue";
import { h as modalManagerPluginKey } from "./keys-DUCx0SNv.mjs";
import { d as tryOnScopeDispose } from "./index-CaguhSuF.mjs";
const modalOpenClassName = "modal-open";
const useSharedModalStack = () => {
const modalManagerPlugin = inject(modalManagerPluginKey);
const dispose = (modal) => {
modalManagerPlugin == null ? void 0 : modalManagerPlugin.removeStack(modal);
modalManagerPlugin == null ? void 0 : modalManagerPlugin.removeRegistry(modal);
};
const updateHTMLAttrs = getSSRHandler("updateHTMLAttrs", (selector, attribute, value) => {
const el = typeof selector === "string" ? window == null ? void 0 : window.document.querySelector(selector) : unrefElement(selector);
if (!el) return;
if (attribute === "class") {
el.classList.toggle(modalOpenClassName, value === modalOpenClassName);
} else {
el.setAttribute(attribute, value);
}
});
tryOnScopeDispose(() => {
updateHTMLAttrs("body", "class", "");
});
watch(
() => modalManagerPlugin == null ? void 0 : modalManagerPlugin.countStack.value,
(newValue) => {
if (newValue === void 0) return;
updateHTMLAttrs("body", "class", newValue > 0 ? modalOpenClassName : "");
}
);
return {
...modalManagerPlugin,
dispose
};
};
const useModalManager = (modalOpen, initialValue) => {
const { pushRegistry, pushStack, removeStack, stack, dispose, countStack } = useSharedModalStack();
const currentModal = getCurrentInstance();
if (!currentModal || currentModal.type.__name !== "BModal") {
throw new Error("useModalManager must only use in BModal component");
}
pushRegistry == null ? void 0 : pushRegistry(currentModal);
tryOnScopeDispose(() => {
dispose(currentModal);
});
const setInStack = (newValue, oldValue) => {
if (newValue) {
pushStack == null ? void 0 : pushStack(currentModal);
} else if (oldValue && !newValue) {
removeStack == null ? void 0 : removeStack(currentModal);
}
};
setInStack(initialValue, initialValue);
watch(modalOpen, setInStack);
return {
activePosition: computed(
() => stack == null ? void 0 : stack.value.findIndex((el) => {
var _a, _b;
return toValue((_a = el.exposed) == null ? void 0 : _a.id) === toValue((_b = currentModal.exposed) == null ? void 0 : _b.id);
})
),
activeModalCount: countStack,
stackWithoutSelf: computed(
() => (stack == null ? void 0 : stack.value.filter(
(el) => {
var _a, _b;
return toValue((_a = el.exposed) == null ? void 0 : _a.id) !== toValue((_b = currentModal.exposed) == null ? void 0 : _b.id);
}
)) ?? []
)
};
};
export {
useModalManager as a,
useSharedModalStack as u
};
//# sourceMappingURL=useModalManager-sahbfe2p.mjs.map