wave-ui 2.32.2 → 2.34.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,7 +22,8 @@ export default {
22
22
  alignRight: { type: Boolean },
23
23
  noPosition: { type: Boolean },
24
24
  zIndex: { type: [Number, String, Boolean] },
25
- activator: { type: String } // Optionally designate an external activator.
25
+ // Optionally designate an external activator.
26
+ activator: { type: [String, Object, HTMLElement] } // The activator can be a DOM string selector, a ref or a DOM node.
26
27
  },
27
28
 
28
29
  data: () => ({
@@ -31,7 +32,7 @@ export default {
31
32
  // as is in an array so we can delete them on destroy.
32
33
  // This only applies to the activatorEventHandlers, the other events listeners can be removed
33
34
  // normally.
34
- docAEventListenersHandlers: []
35
+ docEventListenersHandlers: []
35
36
  }),
36
37
 
37
38
  computed: {
@@ -70,17 +71,22 @@ export default {
70
71
  },
71
72
 
72
73
  hasSeparateActivator () {
73
- return !this.$slots.activator && typeof this.activator === 'string'
74
+ if (this.$slots.activator) return false
75
+ const activatorIsString = typeof this.activator === 'string'
76
+ const activatorIsDomEl = (this.activator?.$el || this.activator) instanceof HTMLElement
77
+ return activatorIsString || activatorIsDomEl
74
78
  },
75
79
 
76
80
  activatorEl: {
77
81
  get () {
78
- if (this.hasSeparateActivator) return document.querySelector(this.activator)
82
+ if (this.hasSeparateActivator) {
83
+ const activator = this.activator?.$el || this.activator
84
+ if (activator instanceof HTMLElement) return activator
85
+ return document.querySelector(this.activator)
86
+ }
79
87
  return this.$el.firstElementChild
80
88
  },
81
- set () {
82
-
83
- }
89
+ set () {}
84
90
  },
85
91
 
86
92
  position () {
@@ -106,9 +112,40 @@ export default {
106
112
 
107
113
  methods: {
108
114
  // ! \ This function uses the DOM - NO SSR (only trigger from beforeMount and later).
109
- getActivatorCoordinates (e) {
115
+ async open (e) {
116
+ // A tiny delay may help positioning the detachable correctly in case of multiple activators
117
+ // with different menu contents.
118
+ if (this.delay) await new Promise(resolve => setTimeout(resolve, this.delay))
119
+
120
+ this.detachableVisible = true
121
+
122
+ // If the activator is external, there might be multiple,
123
+ // so on open, the activator will be set to the event target.
124
+ if (this.activator) this.activatorEl = e.target
125
+
126
+ await this.insertInDOM()
127
+
128
+ if (this.minWidth === 'activator') this.activatorWidth = this.activatorEl.offsetWidth
129
+
130
+ if (!this.noPosition) this.computeDetachableCoords()
131
+
132
+ // In `getActivatorCoordinates` accessing the menu computed styles takes a few ms (less than 10ms),
133
+ // if we don't postpone the Menu apparition it will start transition from a visible menu and
134
+ // thus will not transition.
135
+ this.timeoutId = setTimeout(() => {
136
+ this.$emit('update:modelValue', true)
137
+ this.$emit('input', true)
138
+ this.$emit('open')
139
+ }, 0)
140
+
141
+ if (!this.persistent) document.addEventListener('mousedown', this.onOutsideMousedown)
142
+ if (!this.noPosition) window.addEventListener('resize', this.onResize)
143
+ },
144
+
145
+ // ! \ This function uses the DOM - NO SSR (only trigger from beforeMount and later).
146
+ getActivatorCoordinates () {
110
147
  // Get the activator coordinates relative to window.
111
- const { top, left, width, height } = (e ? e.target : this.activatorEl).getBoundingClientRect()
148
+ const { top, left, width, height } = (this.activatorEl).getBoundingClientRect()
112
149
  let coords = { top, left, width, height }
113
150
 
114
151
  // If absolute position, adjust top & left.
@@ -126,9 +163,9 @@ export default {
126
163
  },
127
164
 
128
165
  // ! \ This function uses the DOM - NO SSR (only trigger from beforeMount and later).
129
- computeDetachableCoords (e) {
166
+ computeDetachableCoords () {
130
167
  // Get the activator coordinates.
131
- let { top, left, width, height } = this.getActivatorCoordinates(e)
168
+ let { top, left, width, height } = this.getActivatorCoordinates()
132
169
 
133
170
  // 1. First display the menu but hide it (So we can get its dimension).
134
171
  // --------------------------------------------------
@@ -251,31 +288,47 @@ export default {
251
288
  this.detachableEl.remove()
252
289
  this.detachableEl = null
253
290
  }
254
- }
255
- },
256
-
257
- mounted () {
258
- const wrapper = this.$el
259
-
260
- // Unwrap the activator element if the activator is in the activator slot.
261
- if (this.$slots.activator) wrapper.parentNode.insertBefore(this.activatorEl, wrapper)
291
+ },
262
292
 
263
293
  // If the activator is external, add event listeners to the document and check the target is
264
294
  // the activator when toggling.
265
295
  // This way, the activator can be a future DOM element, that is not yet in the DOM.
266
- else if (this.activator) {
296
+ bindActivatorEvents () {
297
+ const activatorIsString = typeof this.activator === 'string'
298
+
267
299
  Object.entries(this.activatorEventHandlers).forEach(([eventName, handler]) => {
268
300
  // Convert mouseenter to mouseover & mouseleave to mouseout because we are attaching
269
- // event to the document, so it can accept future nodes.
301
+ // event to the document, so it can accept future DOM nodes.
270
302
  eventName = eventName.replace('mouseenter', 'mouseover').replace('mouseleave', 'mouseout')
271
303
  const handlerWrap = e => {
272
- if (e.target?.matches && e.target.matches(this.activator)) handler(e)
304
+ // The activator can be a DOM string selector a ref or a DOM node.
305
+ if (activatorIsString && e.target?.matches && e.target.matches(this.activator)) handler(e)
306
+ else if (e.target === this.activatorEl || this.activatorEl.contains(e.target)) handler(e)
273
307
  }
274
308
  document.addEventListener(eventName, handlerWrap)
275
309
  // The event listeners handlers have to be removed the exact same way they have been attached.
276
310
  // Since the handler functions have variables that change after hot-reload, keep them exactly
277
311
  // as is in an array so we can delete them on destroy.
278
- this.docAEventListenersHandlers.push({ eventName, handler: handlerWrap })
312
+ this.docEventListenersHandlers.push({ eventName, handler: handlerWrap })
313
+ })
314
+ }
315
+ },
316
+
317
+ mounted () {
318
+ const wrapper = this.$el
319
+
320
+ // Unwrap the activator element if the activator is in the activator slot.
321
+ if (this.$slots.activator) wrapper.parentNode.insertBefore(this.activatorEl, wrapper)
322
+
323
+ // If the activator is external.
324
+ else if (this.activator) this.bindActivatorEvents()
325
+
326
+ // If the activator seems to be undefined, it is probably a DOM node or Vue ref,
327
+ // so check it on nextTick.
328
+ else {
329
+ this.$nextTick(() => {
330
+ this.activator && this.bindActivatorEvents()
331
+ if (this.modelValue) this.toggle({ type: 'click', target: this.activatorEl })
279
332
  })
280
333
  }
281
334
 
@@ -285,7 +338,7 @@ export default {
285
338
  wrapper.parentNode.insertBefore(this.overlayEl, wrapper)
286
339
  }
287
340
 
288
- if (this.modelValue) this.toggleMenu({ type: 'click', target: this.activatorEl })
341
+ if (this.modelValue && this.activator) this.toggle({ type: 'click', target: this.activatorEl })
289
342
  },
290
343
 
291
344
  beforeUnmount () {
@@ -295,8 +348,8 @@ export default {
295
348
 
296
349
  // Remove the event listeners the exact same way they have been defined.
297
350
  // Fixes issues on hot-reloading.
298
- if (this.docAEventListenersHandlers.length) {
299
- this.docAEventListenersHandlers.forEach(({ eventName, handler }) => {
351
+ if (this.docEventListenersHandlers.length) {
352
+ this.docEventListenersHandlers.forEach(({ eventName, handler }) => {
300
353
  document.removeEventListener(eventName, handler)
301
354
  })
302
355
  }