vuestic-ui
Version:
Vue 3 UI Framework
116 lines (115 loc) • 3.12 kB
JavaScript
import { createVNode, render } from "vue";
import { a as getGlobal } from "../../utils/ssr.mjs";
import { _ as _sfc_main } from "./VaToast.vue_vue_type_script_setup_true_lang.mjs";
import { a as withConfigTransport } from "../../services/config-transport/withConfigTransport.mjs";
const VaToast = withConfigTransport(_sfc_main);
let seed = 1;
getGlobal().vaToastInstances = [];
const getNodeProps = (vNode) => {
var _a;
return ((_a = vNode.component) == null ? void 0 : _a.props) || {};
};
const closeNotification = (targetInstance, destroyElementFn) => {
if (!targetInstance) {
return;
}
if (!getGlobal().vaToastInstances.length) {
seed = 1;
return;
}
const targetInstanceIndex = getGlobal().vaToastInstances.findIndex((instance) => instance === targetInstance);
if (targetInstanceIndex < 0) {
return;
}
destroyElementFn();
getGlobal().vaToastInstances = getGlobal().vaToastInstances.reduce((acc, instance, index) => {
if (instance === targetInstance) {
return acc;
}
return [...acc, instance];
}, []);
if (!getGlobal().vaToastInstances.length) {
seed = 1;
}
};
const destroy = (el, node) => {
if (el) {
render(null, el);
el.remove();
}
el = null;
};
const mount = (component, {
props,
children,
element,
appContext
} = {}) => {
let el = element;
let vNode;
const onClose = () => {
closeNotification(vNode, () => destroy(el));
if (props == null ? void 0 : props.onClose) {
props.onClose();
}
};
vNode = createVNode(component, { ...props, onClose }, children);
if (appContext) {
vNode.appContext = appContext;
}
if (el) {
render(vNode, el);
} else if (typeof document !== "undefined") {
render(vNode, el = document.createElement("div"));
}
return { vNode, el };
};
const closeAllNotifications = (appContext) => {
if (!getGlobal().vaToastInstances.length) {
seed = 1;
return;
}
getGlobal().vaToastInstances.forEach((instance) => {
if (appContext && instance.appContext !== appContext) {
return;
}
getNodeProps(instance).onClose();
});
};
const closeById = (id) => {
const targetInstance = getGlobal().vaToastInstances.find((instance) => {
var _a;
return ((_a = instance.el) == null ? void 0 : _a.id) === id;
});
if (targetInstance) {
const nodeProps = getNodeProps(targetInstance);
nodeProps.onClose();
}
};
const getToastOptions = (options) => {
if (typeof options === "string") {
return {
message: options
};
}
return options;
};
const createToastInstance = (customProps, appContext) => {
const { vNode, el } = mount(VaToast, { appContext, props: getToastOptions(customProps) });
const nodeProps = getNodeProps(vNode);
if (el && vNode.el && nodeProps) {
document.body.appendChild(el.childNodes[0]);
vNode.el.id = "notification_" + seed;
seed += 1;
getGlobal().vaToastInstances.push(vNode);
return vNode.el.id;
}
return null;
};
export {
VaToast as V,
closeById as a,
closeAllNotifications as b,
createToastInstance as c
};
//# sourceMappingURL=toast.mjs.map