naive-ui/packages/mixins/detachable.js

58 lines
1.4 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
2019-09-26 22:35:37 +08:00
}
},
watch: {
active (value) {
if (value && !this.contentContainerMounted) {
this.contentContainerMounted = true
this.appendContent()
}
}
},
data () {
return {
contentContainer: null,
contentContainerMounted: false
2019-07-11 19:51:59 +08:00
}
},
methods: {
detachContent () {
2019-09-26 22:35:37 +08:00
this.contentContainer = this.$refs.contentContainer
this.$refs.contentContainer.parentNode.removeChild(this.$refs.contentContainer)
2019-09-26 22:35:37 +08:00
},
appendContent () {
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()
2019-09-26 22:35:37 +08:00
// this.appendContent()
2019-07-11 19:51:59 +08:00
},
beforeDestroy () {
2019-09-26 22:35:37 +08:00
this.detachTarget.removeChild(this.$refs.contentContainer)
2019-07-11 19:51:59 +08:00
}
}