naive-ui/packages/mixins/detachable.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

import withapp from './withapp'
/**
* Detach $refs.contentContainer to detachTarget
*
* Dependency:
* $refs.contentContainer
*
* @prop {HTMLElement} detachTarget determine where should $refs.contentContainer to be detached
*/
2019-07-11 19:51:59 +08:00
export default {
mixins: [withapp],
2019-07-11 19:51:59 +08:00
props: {
detachTarget: {
validator () {
return true
},
default: () => document.body
},
cleanManually: {
type: Boolean,
default: false
2019-07-11 19:51:59 +08:00
}
},
methods: {
detachContent () {
this.$refs.contentContainer.parentNode.removeChild(this.$refs.contentContainer)
this.detachTarget.append(this.$refs.contentContainer)
2019-07-11 19:51:59 +08:00
}
},
beforeMount () {
if (!this.detachTarget) {
console.warn(this.$options.name, ' will be mounted to', this.detachTarget, ', but it doesn\'t exist! Modal component won\'t work!')
}
},
mounted () {
this.detachTarget = document.body // getScrollParent(this.$refs.self)
this.detachContent()
},
beforeDestroy () {
if (!this.cleanManually) {
this.detachTarget.removeChild(this.$refs.contentContainer)
}
2019-07-11 19:51:59 +08:00
}
}