Skip to content

Instantly share code, notes, and snippets.

@NateScarlet
Last active January 9, 2020 09:28
Show Gist options
  • Save NateScarlet/1c7471fec66c45bb72419f8b613d230e to your computer and use it in GitHub Desktop.
Save NateScarlet/1c7471fec66c45bb72419f8b613d230e to your computer and use it in GitHub Desktop.
element-ui modal show
import Vue, { VNode, VNodeData, VueConstructor } from 'vue';
import { app } from '@/main';
/**
* Show a modal vue component
* @param constructor Component constructor
* @param data Component data
*/
export function show<V extends Vue>(
constructor: VueConstructor<V>,
data?: VNodeData
): V {
const wrapper = new Vue({
name: 'ModalWrapper',
parent: app,
data: {
visible: false,
},
watch: {
visible(v): void {
if (v === false) {
setTimeout((): void => {
this.$destroy();
this.$el.remove();
}, 5e3);
}
},
},
mounted(): void {
this.visible = true;
},
render(h): VNode {
const d: VNodeData = {
...data,
props: {
...(data && data.props),
visible: this.$data.visible,
},
on: {
...(data && data.on),
'update:visible': (v: boolean): void => {
this.$data.visible = v;
},
},
};
return h(constructor, d);
},
});
wrapper.$mount(document.createElement('div'));
wrapper.$parent.$el.append(wrapper.$el);
const ret = wrapper.$children[0] as V;
return ret;
}
import { Component, Prop, Vue } from 'vue-property-decorator';
/**
* Mixin for support `dialog.show`
*/
@Component
export class ModalMixin extends Vue {
@Prop({ default: false, type: Boolean })
public visible!: boolean;
public get $_visible(): boolean {
return this.visible;
}
public set $_visible(v: boolean) {
this.$emit('update:visible', v);
}
}
@NateScarlet
Copy link
Author

NateScarlet commented Jan 9, 2020

Example usage:

Add :visible.sync='$_visible' to element drawer or dialog.

  async showEditingDrawer() {
    modal.show(
      (
        await import(
          /* webpackChunkName: "project-admin" */ '@/components/project/ProjectEditingDrawer.vue'
        )
      ).default,
      {
        props: { id: this.id },
      }
    );
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment