/
index.ts
168 lines (143 loc) Β· 4.1 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Mixins
import Bootable from '../bootable'
// Utilities
import { getObjectValueByPath } from '../../util/helpers'
import mixins, { ExtractVue } from '../../util/mixins'
import { consoleWarn } from '../../util/console'
// Types
import { PropOptions } from 'vue'
import { VNode } from 'vue/types'
interface options {
$el: HTMLElement
$refs: {
content?: HTMLElement
}
}
function validateAttachTarget (val: any) {
const type = typeof val
if (type === 'boolean' || type === 'string') return true
return val.nodeType === Node.ELEMENT_NODE
}
function removeActivator (activator: VNode[]) {
activator.forEach(node => {
node.elm &&
node.elm.parentNode &&
node.elm.parentNode.removeChild(node.elm)
})
}
/* @vue/component */
export default mixins<options &
/* eslint-disable indent */
ExtractVue<typeof Bootable>
/* eslint-enable indent */
>(Bootable).extend({
name: 'detachable',
props: {
attach: {
default: false,
validator: validateAttachTarget,
} as PropOptions<boolean | string | Element>,
contentClass: {
type: String,
default: '',
},
},
data: () => ({
activatorNode: null as null | VNode | VNode[],
hasDetached: false,
}),
watch: {
attach () {
this.hasDetached = false
this.initDetach()
},
hasContent () {
this.$nextTick(this.initDetach)
},
},
beforeMount () {
this.$nextTick(() => {
if (this.activatorNode) {
const activator = Array.isArray(this.activatorNode) ? this.activatorNode : [this.activatorNode]
activator.forEach(node => {
if (!node.elm) return
if (!this.$el.parentNode) return
const target = this.$el === this.$el.parentNode.firstChild
? this.$el
: this.$el.nextSibling
this.$el.parentNode.insertBefore(node.elm, target)
})
}
})
},
mounted () {
this.hasContent && this.initDetach()
},
deactivated () {
this.isActive = false
},
beforeDestroy () {
if (
this.$refs.content &&
this.$refs.content.parentNode
) {
this.$refs.content.parentNode.removeChild(this.$refs.content)
}
},
destroyed () {
if (this.activatorNode) {
const activator = Array.isArray(this.activatorNode) ? this.activatorNode : [this.activatorNode]
if (this.$el.isConnected) {
// Component has been destroyed but the element still exists, we must be in a transition
// Wait for the transition to finish before cleaning up the detached activator
const observer = new MutationObserver(list => {
if (
list.some(record => Array.from(record.removedNodes).includes(this.$el))
) {
observer.disconnect()
removeActivator(activator)
}
})
observer.observe(this.$el.parentNode!, { subtree: false, childList: true })
} else {
removeActivator(activator)
}
}
},
methods: {
getScopeIdAttrs () {
const scopeId = getObjectValueByPath(this.$vnode, 'context.$options._scopeId')
return scopeId && {
[scopeId]: '',
}
},
initDetach () {
if (this._isDestroyed ||
!this.$refs.content ||
this.hasDetached ||
// Leave menu in place if attached
// and dev has not changed target
this.attach === '' || // If used as a boolean prop (<v-menu attach>)
this.attach === true || // If bound to a boolean (<v-menu :attach="true">)
this.attach === 'attach' // If bound as boolean prop in pug (v-menu(attach))
) return
let target
if (this.attach === false) {
// Default, detach to app
target = document.querySelector('[data-app]')
} else if (typeof this.attach === 'string') {
// CSS selector
target = document.querySelector(this.attach)
} else {
// DOM Element
target = this.attach
}
if (!target) {
consoleWarn(`Unable to locate target ${this.attach || '[data-app]'}`, this)
return
}
target.appendChild(this.$refs.content)
this.hasDetached = true
},
},
})