UNPKG

vue3-promise-dialog

Version:

Dialogs meet promises in Vue 3 !

67 lines (66 loc) 1.99 kB
import { shallowRef, defineComponent, ref, watch, openBlock, createBlock, Transition, normalizeProps, guardReactiveProps, withCtx, resolveDynamicComponent, mergeProps, createCommentVNode } from "vue"; const dialogRef = shallowRef(); function closeDialog(data) { if (data === void 0) { data = dialogRef.value.comp.returnValue(); } dialogRef.value.resolve(data); dialogRef.value = null; } function openDialog(dialog, props, wrapper = "default") { return new Promise((resolve) => { dialogRef.value = { dialog, props, wrapper, resolve }; }); } const PromiseDialog = { install: (app, options) => { app.config.globalProperties.$close = (comp, alternateValue) => { closeDialog(alternateValue); }; } }; var _export_sfc = (sfc, props) => { const target = sfc.__vccOpts || sfc; for (const [key, val] of props) { target[key] = val; } return target; }; const _sfc_main = defineComponent({ name: "DialogWrapper", components: {}, props: { name: { type: String, default: "default" }, transitionAttrs: Object }, setup() { const dialogInstance = ref(); watch(dialogInstance, () => { if (dialogRef.value) { dialogRef.value.comp = dialogInstance.value; } }); return { dialogRef, dialogInstance }; } }); function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) { return openBlock(), createBlock(Transition, normalizeProps(guardReactiveProps(_ctx.transitionAttrs)), { default: withCtx(() => [ _ctx.dialogRef && _ctx.dialogRef.wrapper === _ctx.name ? (openBlock(), createBlock(resolveDynamicComponent(_ctx.dialogRef.dialog), mergeProps({ key: 0 }, _ctx.dialogRef.props, { ref: "dialogInstance" }), null, 16)) : createCommentVNode("", true) ]), _: 1 }, 16); } var DialogWrapper = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]); export { DialogWrapper, PromiseDialog, closeDialog, openDialog };