Last active
January 22, 2021 02:30
-
-
Save curist/deb7dea8ee534ad2a5d3a989c663709f to your computer and use it in GitHub Desktop.
Vue showModal util
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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