Skip to content

Instantly share code, notes, and snippets.

@curist
Last active January 22, 2021 02:30
Show Gist options
  • Save curist/deb7dea8ee534ad2a5d3a989c663709f to your computer and use it in GitHub Desktop.
Save curist/deb7dea8ee534ad2a5d3a989c663709f to your computer and use it in GitHub Desktop.
Vue showModal util
import Vue from 'vue';
export const showModal = function(comp, props = {}) {
let resolve, $el;
const modalContainer = new Vue({
render(h) {
return h(comp, {
props: {
...props,
handleFinish,
handleClose,
},
});
},
});
function removeElement() {
if(!$el) return;
document.body.removeChild($el);
modalContainer.$destroy();
$el = null;
}
function handleFinish(data = null) {
removeElement();
resolve({ ok: true, data });
}
function handleClose() {
removeElement();
resolve({ ok: false });
}
modalContainer.$mount();
$el = modalContainer.$el;
document.body.appendChild($el);
const promise = new Promise(r => resolve = r);
promise.dismiss = handleClose;
return promise;
};
export default showModal;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment