-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
index.ts
217 lines (184 loc) Β· 5.92 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Mixins
import Delayable from '../delayable'
import Toggleable from '../toggleable'
// Utilities
import mixins from '../../util/mixins'
import { getSlot, getSlotType } from '../../util/helpers'
import { consoleError } from '../../util/console'
// Types
import { VNode, PropType } from 'vue'
type Listeners = Dictionary<(e: MouseEvent & KeyboardEvent & FocusEvent) => void>
const baseMixins = mixins(
Delayable,
Toggleable
)
/* @vue/component */
export default baseMixins.extend({
name: 'activatable',
props: {
activator: {
default: null as unknown as PropType<string | HTMLElement | VNode | Element | null>,
validator: (val: string | object) => {
return ['string', 'object'].includes(typeof val)
},
},
disabled: Boolean,
internalActivator: Boolean,
openOnClick: {
type: Boolean,
default: true,
},
openOnHover: Boolean,
openOnFocus: Boolean,
},
data: () => ({
// Do not use this directly, call getActivator() instead
activatorElement: null as HTMLElement | null,
activatorNode: [] as VNode[],
events: ['click', 'mouseenter', 'mouseleave', 'focus'],
listeners: {} as Listeners,
}),
watch: {
activator: 'resetActivator',
openOnFocus: 'resetActivator',
openOnHover: 'resetActivator',
},
mounted () {
const slotType = getSlotType(this, 'activator', true)
if (slotType && ['v-slot', 'normal'].includes(slotType)) {
consoleError(`The activator slot must be bound, try '<template v-slot:activator="{ on }"><v-btn v-on="on">'`, this)
}
this.addActivatorEvents()
},
beforeDestroy () {
this.removeActivatorEvents()
},
methods: {
addActivatorEvents () {
if (
!this.activator ||
this.disabled ||
!this.getActivator()
) return
this.listeners = this.genActivatorListeners()
const keys = Object.keys(this.listeners)
for (const key of keys) {
this.getActivator()!.addEventListener(key, this.listeners[key] as any)
}
},
genActivator () {
const node = getSlot(this, 'activator', Object.assign(this.getValueProxy(), {
on: this.genActivatorListeners(),
attrs: this.genActivatorAttributes(),
})) || []
this.activatorNode = node
return node
},
genActivatorAttributes () {
return {
role: (this.openOnClick && !this.openOnHover) ? 'button' : undefined,
'aria-haspopup': true,
'aria-expanded': String(this.isActive),
}
},
genActivatorListeners () {
if (this.disabled) return {}
const listeners: Listeners = {}
if (this.openOnHover) {
listeners.mouseenter = (e: MouseEvent) => {
this.getActivator(e)
this.runDelay('open')
}
listeners.mouseleave = (e: MouseEvent) => {
this.getActivator(e)
this.runDelay('close')
}
} else if (this.openOnClick) {
listeners.click = (e: MouseEvent) => {
const activator = this.getActivator(e)
if (activator) activator.focus()
e.stopPropagation()
this.isActive = !this.isActive
}
}
if (this.openOnFocus) {
listeners.focus = (e: FocusEvent) => {
this.getActivator(e)
e.stopPropagation()
this.isActive = !this.isActive
}
}
return listeners
},
getActivator (e?: Event): HTMLElement | null {
// If we've already fetched the activator, re-use
if (this.activatorElement) return this.activatorElement
let activator = null
if (this.activator) {
const target = this.internalActivator ? this.$el : document
if (typeof this.activator === 'string') {
// Selector
activator = target.querySelector(this.activator)
} else if ((this.activator as any).$el) {
// Component (ref)
activator = (this.activator as any).$el
} else {
// HTMLElement | Element
activator = this.activator
}
} else if (this.activatorNode.length === 1 || (this.activatorNode.length && !e)) {
// Use the contents of the activator slot
// There's either only one element in it or we
// don't have a click event to use as a last resort
const vm = this.activatorNode[0].componentInstance
if (
vm &&
vm.$options.mixins && // Activatable is indirectly used via Menuable
vm.$options.mixins.some((m: any) => m.options && ['activatable', 'menuable'].includes(m.options.name))
) {
// Activator is actually another activatible component, use its activator (#8846)
activator = (vm as any).getActivator()
} else {
activator = this.activatorNode[0].elm as HTMLElement
}
} else if (e) {
// Activated by a click or focus event
activator = (e.currentTarget || e.target) as HTMLElement
}
// The activator should only be a valid element (Ignore comments and text nodes)
this.activatorElement = activator?.nodeType === Node.ELEMENT_NODE ? activator : null
return this.activatorElement
},
getContentSlot () {
return getSlot(this, 'default', this.getValueProxy(), true)
},
getValueProxy (): object {
const self = this
return {
get value () {
return self.isActive
},
set value (isActive: boolean) {
self.isActive = isActive
},
}
},
removeActivatorEvents () {
if (
!this.activator ||
!this.activatorElement
) return
const keys = Object.keys(this.listeners)
for (const key of keys) {
(this.activatorElement as any).removeEventListener(key, this.listeners[key])
}
this.listeners = {}
},
resetActivator () {
this.removeActivatorEvents()
this.activatorElement = null
this.getActivator()
this.addActivatorEvents()
},
},
})