@v-c/trigger 0.0.13 → 0.0.14

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.
@@ -1,613 +1,406 @@
1
+ import { collectScroller, getVisibleArea, getWin, toNum } from "../util.js";
2
+ import { computed, nextTick, reactive, ref, shallowRef, toRefs, watch } from "vue";
1
3
  import { isDOM } from "@v-c/util/dist/Dom/findDOMNode";
2
4
  import isVisible from "@v-c/util/dist/Dom/isVisible";
3
5
  import { rafDebounce } from "@v-c/util/dist/raf";
4
- import { reactive, shallowRef, computed, ref, watch, nextTick, toRefs } from "vue";
5
- import { collectScroller, getWin, getVisibleArea, toNum } from "../util.js";
6
6
  function getUnitOffset(size, offset = 0) {
7
- const offsetStr = `${offset}`;
8
- const cells = offsetStr.match(/^(.*)\%$/);
9
- if (cells) {
10
- return size * (parseFloat(cells[1]) / 100);
11
- }
12
- return parseFloat(offsetStr);
7
+ const offsetStr = `${offset}`;
8
+ const cells = offsetStr.match(/^(.*)\%$/);
9
+ if (cells) return size * (parseFloat(cells[1]) / 100);
10
+ return parseFloat(offsetStr);
13
11
  }
14
12
  function getNumberOffset(rect, offset) {
15
- const [offsetX, offsetY] = offset || [];
16
- return [
17
- getUnitOffset(rect.width, offsetX),
18
- getUnitOffset(rect.height, offsetY)
19
- ];
13
+ const [offsetX, offsetY] = offset || [];
14
+ return [getUnitOffset(rect.width, offsetX), getUnitOffset(rect.height, offsetY)];
20
15
  }
21
16
  function splitPoints(points = "") {
22
- return [points[0], points[1]];
17
+ return [points[0], points[1]];
23
18
  }
24
19
  function getAlignPoint(rect, points) {
25
- const topBottom = points[0];
26
- const leftRight = points[1];
27
- let x;
28
- let y;
29
- if (topBottom === "t") {
30
- y = rect.y;
31
- } else if (topBottom === "b") {
32
- y = rect.y + rect.height;
33
- } else {
34
- y = rect.y + rect.height / 2;
35
- }
36
- if (leftRight === "l") {
37
- x = rect.x;
38
- } else if (leftRight === "r") {
39
- x = rect.x + rect.width;
40
- } else {
41
- x = rect.x + rect.width / 2;
42
- }
43
- return { x, y };
20
+ const topBottom = points[0];
21
+ const leftRight = points[1];
22
+ let x;
23
+ let y;
24
+ if (topBottom === "t") y = rect.y;
25
+ else if (topBottom === "b") y = rect.y + rect.height;
26
+ else y = rect.y + rect.height / 2;
27
+ if (leftRight === "l") x = rect.x;
28
+ else if (leftRight === "r") x = rect.x + rect.width;
29
+ else x = rect.x + rect.width / 2;
30
+ return {
31
+ x,
32
+ y
33
+ };
44
34
  }
45
35
  function reversePoints(points, index) {
46
- const reverseMap = {
47
- t: "b",
48
- b: "t",
49
- l: "r",
50
- r: "l"
51
- };
52
- return points.map((point, i) => {
53
- if (i === index) {
54
- return reverseMap[point] || "c";
55
- }
56
- return point;
57
- }).join("");
36
+ const reverseMap = {
37
+ t: "b",
38
+ b: "t",
39
+ l: "r",
40
+ r: "l"
41
+ };
42
+ return points.map((point, i) => {
43
+ if (i === index) return reverseMap[point] || "c";
44
+ return point;
45
+ }).join("");
58
46
  }
59
47
  function parseOriginValue(value, size, axis) {
60
- const fallback = size / 2;
61
- if (!value) {
62
- return fallback;
63
- }
64
- const val = value.trim();
65
- if (!val) {
66
- return fallback;
67
- }
68
- if (val.endsWith("%")) {
69
- return size * (parseFloat(val) / 100);
70
- }
71
- if (val.endsWith("px")) {
72
- return parseFloat(val);
73
- }
74
- if (val === "center") {
75
- return fallback;
76
- }
77
- if (axis === "x") {
78
- if (val === "left") {
79
- return 0;
80
- }
81
- if (val === "right") {
82
- return size;
83
- }
84
- } else if (axis === "y") {
85
- if (val === "top") {
86
- return 0;
87
- }
88
- if (val === "bottom") {
89
- return size;
90
- }
91
- }
92
- const num = parseFloat(val);
93
- return Number.isNaN(num) ? fallback : num;
48
+ const fallback = size / 2;
49
+ if (!value) return fallback;
50
+ const val = value.trim();
51
+ if (!val) return fallback;
52
+ if (val.endsWith("%")) return size * (parseFloat(val) / 100);
53
+ if (val.endsWith("px")) return parseFloat(val);
54
+ if (val === "center") return fallback;
55
+ if (axis === "x") {
56
+ if (val === "left") return 0;
57
+ if (val === "right") return size;
58
+ } else if (axis === "y") {
59
+ if (val === "top") return 0;
60
+ if (val === "bottom") return size;
61
+ }
62
+ const num = parseFloat(val);
63
+ return Number.isNaN(num) ? fallback : num;
94
64
  }
95
65
  function getTransformOriginPoint(origin, width, height) {
96
- const [originX = "50%", originY = "50%"] = origin?.split(/\s+/).filter(Boolean) ?? [];
97
- return [
98
- parseOriginValue(originX, width, "x"),
99
- parseOriginValue(originY, height, "y")
100
- ];
66
+ const [originX = "50%", originY = "50%"] = origin?.split(/\s+/).filter(Boolean) ?? [];
67
+ return [parseOriginValue(originX, width, "x"), parseOriginValue(originY, height, "y")];
101
68
  }
102
69
  function normalizeRect(rect, scaleX, scaleY, originX, originY) {
103
- const rawX = rect.x ?? rect.left;
104
- const rawY = rect.y ?? rect.top;
105
- const width = rect.width / scaleX;
106
- const height = rect.height / scaleY;
107
- const x = rawX - (1 - scaleX) * originX;
108
- const y = rawY - (1 - scaleY) * originY;
109
- const right = x + width;
110
- const bottom = y + height;
111
- return {
112
- x,
113
- y,
114
- width,
115
- height,
116
- left: x,
117
- top: y,
118
- right,
119
- bottom
120
- };
70
+ const rawX = rect.x ?? rect.left;
71
+ const rawY = rect.y ?? rect.top;
72
+ const width = rect.width / scaleX;
73
+ const height = rect.height / scaleY;
74
+ const x = rawX - (1 - scaleX) * originX;
75
+ const y = rawY - (1 - scaleY) * originY;
76
+ return {
77
+ x,
78
+ y,
79
+ width,
80
+ height,
81
+ left: x,
82
+ top: y,
83
+ right: x + width,
84
+ bottom: y + height
85
+ };
121
86
  }
122
87
  function shouldSwitchPlacement(isOverflow, isVisibleFirst, newVisibleArea, originVisibleArea, newRecommendArea, originRecommendArea) {
123
- if (isOverflow) {
124
- return newVisibleArea > originVisibleArea || newVisibleArea === originVisibleArea && (!isVisibleFirst || newRecommendArea >= originRecommendArea);
125
- }
126
- return newVisibleArea > originVisibleArea || isVisibleFirst && newVisibleArea === originVisibleArea && newRecommendArea > originRecommendArea;
88
+ if (isOverflow) return newVisibleArea > originVisibleArea || newVisibleArea === originVisibleArea && (!isVisibleFirst || newRecommendArea >= originRecommendArea);
89
+ return newVisibleArea > originVisibleArea || isVisibleFirst && newVisibleArea === originVisibleArea && newRecommendArea > originRecommendArea;
127
90
  }
128
91
  function useAlign(open, popupEle, target, placement, builtinPlacements, popupAlign, onPopupAlign, mobile) {
129
- const offsetInfo = reactive({
130
- ready: false,
131
- offsetX: 0,
132
- offsetY: 0,
133
- offsetR: 0,
134
- offsetB: 0,
135
- arrowX: 0,
136
- arrowY: 0,
137
- scaleX: 1,
138
- scaleY: 1,
139
- align: builtinPlacements.value[placement.value] || {}
140
- });
141
- const alignCountRef = shallowRef(0);
142
- const scrollerList = computed(() => {
143
- if (!popupEle.value || mobile?.value) {
144
- return [];
145
- }
146
- return collectScroller(popupEle.value);
147
- });
148
- const prevFlipRef = ref({});
149
- const resetFlipCache = () => {
150
- prevFlipRef.value = {};
151
- };
152
- const _onAlign = () => {
153
- if (popupEle.value && target.value && open.value && !mobile?.value) {
154
- let getIntersectionVisibleArea = function(offsetX2, offsetY2, area = visibleArea) {
155
- const l = popupRect.x + offsetX2;
156
- const t = popupRect.y + offsetY2;
157
- const r = l + popupWidth;
158
- const b = t + popupHeight;
159
- const visibleL = Math.max(l, area.left);
160
- const visibleT = Math.max(t, area.top);
161
- const visibleR = Math.min(r, area.right);
162
- const visibleB = Math.min(b, area.bottom);
163
- return Math.max(0, (visibleR - visibleL) * (visibleB - visibleT));
164
- }, syncNextPopupPosition = function() {
165
- nextPopupY = popupRect.y + nextOffsetY;
166
- nextPopupBottom = nextPopupY + popupHeight;
167
- nextPopupX = popupRect.x + nextOffsetX;
168
- nextPopupRight = nextPopupX + popupWidth;
169
- };
170
- const popupElement = popupEle.value;
171
- const doc = popupElement.ownerDocument;
172
- const win = getWin(popupElement);
173
- const popupComputedStyle = win.getComputedStyle(popupElement);
174
- const { position: popupPosition } = popupComputedStyle;
175
- const originLeft = popupElement.style.left;
176
- const originTop = popupElement.style.top;
177
- const originRight = popupElement.style.right;
178
- const originBottom = popupElement.style.bottom;
179
- const originOverflow = popupElement.style.overflow;
180
- const placementInfo = {
181
- ...builtinPlacements.value[placement.value],
182
- ...popupAlign?.value
183
- };
184
- const placeholderElement = doc.createElement("div");
185
- popupElement.parentElement?.appendChild(placeholderElement);
186
- placeholderElement.style.left = `${popupElement.offsetLeft}px`;
187
- placeholderElement.style.top = `${popupElement.offsetTop}px`;
188
- placeholderElement.style.position = popupPosition;
189
- placeholderElement.style.height = `${popupElement.offsetHeight}px`;
190
- placeholderElement.style.width = `${popupElement.offsetWidth}px`;
191
- popupElement.style.left = "0";
192
- popupElement.style.top = "0";
193
- popupElement.style.right = "auto";
194
- popupElement.style.bottom = "auto";
195
- popupElement.style.overflow = "hidden";
196
- let targetRect;
197
- if (Array.isArray(target.value)) {
198
- targetRect = {
199
- x: target.value[0],
200
- y: target.value[1],
201
- width: 0,
202
- height: 0
203
- };
204
- } else {
205
- const rect = target.value.getBoundingClientRect();
206
- rect.x = rect.x ?? rect.left;
207
- rect.y = rect.y ?? rect.top;
208
- targetRect = {
209
- x: rect.x,
210
- y: rect.y,
211
- width: rect.width,
212
- height: rect.height
213
- };
214
- }
215
- const rawPopupRect = popupElement.getBoundingClientRect();
216
- const {
217
- clientWidth,
218
- clientHeight,
219
- scrollWidth,
220
- scrollHeight,
221
- scrollTop,
222
- scrollLeft
223
- } = doc.documentElement;
224
- const targetHeight = targetRect.height;
225
- const targetWidth = targetRect.width;
226
- const visibleRegion = {
227
- left: 0,
228
- top: 0,
229
- right: clientWidth,
230
- bottom: clientHeight
231
- };
232
- const scrollRegion = {
233
- left: -scrollLeft,
234
- top: -scrollTop,
235
- right: scrollWidth - scrollLeft,
236
- bottom: scrollHeight - scrollTop
237
- };
238
- let { htmlRegion } = placementInfo;
239
- const VISIBLE = "visible";
240
- const VISIBLE_FIRST = "visibleFirst";
241
- if (htmlRegion !== "scroll" && htmlRegion !== VISIBLE_FIRST) {
242
- htmlRegion = VISIBLE;
243
- }
244
- const isVisibleFirst = htmlRegion === VISIBLE_FIRST;
245
- const scrollRegionArea = getVisibleArea(scrollRegion, scrollerList.value);
246
- const visibleRegionArea = getVisibleArea(visibleRegion, scrollerList.value);
247
- const visibleArea = htmlRegion === VISIBLE ? visibleRegionArea : scrollRegionArea;
248
- const adjustCheckVisibleArea = isVisibleFirst ? visibleRegionArea : visibleArea;
249
- popupElement.style.left = "auto";
250
- popupElement.style.top = "auto";
251
- popupElement.style.right = "0";
252
- popupElement.style.bottom = "0";
253
- const rawPopupMirrorRect = popupElement.getBoundingClientRect();
254
- popupElement.style.left = originLeft;
255
- popupElement.style.top = originTop;
256
- popupElement.style.right = originRight;
257
- popupElement.style.bottom = originBottom;
258
- popupElement.style.overflow = originOverflow;
259
- popupElement.parentElement?.removeChild(placeholderElement);
260
- const widthValue = parseFloat(popupComputedStyle.width);
261
- const heightValue = parseFloat(popupComputedStyle.height);
262
- const baseWidth = !Number.isNaN(widthValue) && widthValue > 0 ? widthValue : popupElement.offsetWidth;
263
- const baseHeight = !Number.isNaN(heightValue) && heightValue > 0 ? heightValue : popupElement.offsetHeight;
264
- const safeBaseWidth = baseWidth || rawPopupRect.width || 1;
265
- const safeBaseHeight = baseHeight || rawPopupRect.height || 1;
266
- const scaleX2 = toNum(
267
- Math.round(rawPopupRect.width / safeBaseWidth * 1e3) / 1e3
268
- );
269
- const scaleY2 = toNum(
270
- Math.round(rawPopupRect.height / safeBaseHeight * 1e3) / 1e3
271
- );
272
- if (scaleX2 === 0 || scaleY2 === 0 || isDOM(target) && !isVisible(target)) {
273
- return;
274
- }
275
- const [originX, originY] = getTransformOriginPoint(
276
- popupComputedStyle.transformOrigin,
277
- safeBaseWidth,
278
- safeBaseHeight
279
- );
280
- const popupRect = normalizeRect(rawPopupRect, scaleX2, scaleY2, originX, originY);
281
- const popupMirrorRect = normalizeRect(
282
- rawPopupMirrorRect,
283
- scaleX2,
284
- scaleY2,
285
- originX,
286
- originY
287
- );
288
- const popupHeight = popupRect.height;
289
- const popupWidth = popupRect.width;
290
- const { offset, targetOffset } = placementInfo;
291
- let [popupOffsetX, popupOffsetY] = getNumberOffset(popupRect, offset);
292
- const [targetOffsetX, targetOffsetY] = getNumberOffset(
293
- targetRect,
294
- targetOffset
295
- );
296
- targetRect.x -= targetOffsetX;
297
- targetRect.y -= targetOffsetY;
298
- const [popupPoint, targetPoint] = placementInfo.points || [];
299
- const targetPoints = splitPoints(targetPoint);
300
- const popupPoints = splitPoints(popupPoint);
301
- const targetAlignPoint = getAlignPoint(targetRect, targetPoints);
302
- const popupAlignPoint = getAlignPoint(popupRect, popupPoints);
303
- const nextAlignInfo = {
304
- ...placementInfo
305
- };
306
- let nextOffsetX = targetAlignPoint.x - popupAlignPoint.x + popupOffsetX;
307
- let nextOffsetY = targetAlignPoint.y - popupAlignPoint.y + popupOffsetY;
308
- const originIntersectionVisibleArea = getIntersectionVisibleArea(
309
- nextOffsetX,
310
- nextOffsetY
311
- );
312
- const originIntersectionRecommendArea = getIntersectionVisibleArea(
313
- nextOffsetX,
314
- nextOffsetY,
315
- visibleRegionArea
316
- );
317
- const targetAlignPointTL = getAlignPoint(targetRect, ["t", "l"]);
318
- const popupAlignPointTL = getAlignPoint(popupRect, ["t", "l"]);
319
- const targetAlignPointBR = getAlignPoint(targetRect, ["b", "r"]);
320
- const popupAlignPointBR = getAlignPoint(popupRect, ["b", "r"]);
321
- const overflow = placementInfo.overflow || {};
322
- const { adjustX, adjustY, shiftX, shiftY } = overflow;
323
- const supportAdjust = (val) => {
324
- if (typeof val === "boolean") {
325
- return val;
326
- }
327
- return val >= 0;
328
- };
329
- let nextPopupY;
330
- let nextPopupBottom;
331
- let nextPopupX;
332
- let nextPopupRight;
333
- syncNextPopupPosition();
334
- const needAdjustY = supportAdjust(adjustY);
335
- const sameTB = popupPoints[0] === targetPoints[0];
336
- const overflowBottom = nextPopupBottom > adjustCheckVisibleArea.bottom;
337
- if (needAdjustY && popupPoints[0] === "t" && (overflowBottom || prevFlipRef.value.bt)) {
338
- let tmpNextOffsetY = nextOffsetY;
339
- if (sameTB) {
340
- tmpNextOffsetY -= popupHeight - targetHeight;
341
- } else {
342
- tmpNextOffsetY = targetAlignPointTL.y - popupAlignPointBR.y - popupOffsetY;
343
- }
344
- const newVisibleArea = getIntersectionVisibleArea(
345
- nextOffsetX,
346
- tmpNextOffsetY
347
- );
348
- const newVisibleRecommendArea = getIntersectionVisibleArea(
349
- nextOffsetX,
350
- tmpNextOffsetY,
351
- visibleRegionArea
352
- );
353
- const shouldFlip = shouldSwitchPlacement(
354
- overflowBottom,
355
- isVisibleFirst,
356
- newVisibleArea,
357
- originIntersectionVisibleArea,
358
- newVisibleRecommendArea,
359
- originIntersectionRecommendArea
360
- );
361
- if (shouldFlip) {
362
- prevFlipRef.value.bt = true;
363
- nextOffsetY = tmpNextOffsetY;
364
- popupOffsetY = -popupOffsetY;
365
- nextAlignInfo.points = [
366
- reversePoints(popupPoints, 0),
367
- reversePoints(targetPoints, 0)
368
- ];
369
- } else {
370
- prevFlipRef.value.bt = false;
371
- }
372
- }
373
- const overflowTop = nextPopupY < adjustCheckVisibleArea.top;
374
- if (needAdjustY && popupPoints[0] === "b" && (overflowTop || prevFlipRef.value.tb)) {
375
- let tmpNextOffsetY = nextOffsetY;
376
- if (sameTB) {
377
- tmpNextOffsetY += popupHeight - targetHeight;
378
- } else {
379
- tmpNextOffsetY = targetAlignPointBR.y - popupAlignPointTL.y - popupOffsetY;
380
- }
381
- const newVisibleArea = getIntersectionVisibleArea(
382
- nextOffsetX,
383
- tmpNextOffsetY
384
- );
385
- const newVisibleRecommendArea = getIntersectionVisibleArea(
386
- nextOffsetX,
387
- tmpNextOffsetY,
388
- visibleRegionArea
389
- );
390
- const shouldFlip = shouldSwitchPlacement(
391
- overflowTop,
392
- isVisibleFirst,
393
- newVisibleArea,
394
- originIntersectionVisibleArea,
395
- newVisibleRecommendArea,
396
- originIntersectionRecommendArea
397
- );
398
- if (shouldFlip) {
399
- prevFlipRef.value.tb = true;
400
- nextOffsetY = tmpNextOffsetY;
401
- popupOffsetY = -popupOffsetY;
402
- nextAlignInfo.points = [
403
- reversePoints(popupPoints, 0),
404
- reversePoints(targetPoints, 0)
405
- ];
406
- } else {
407
- prevFlipRef.value.tb = false;
408
- }
409
- }
410
- const needAdjustX = supportAdjust(adjustX);
411
- const sameLR = popupPoints[1] === targetPoints[1];
412
- const overflowRight = nextPopupRight > adjustCheckVisibleArea.right;
413
- if (needAdjustX && popupPoints[1] === "l" && (overflowRight || prevFlipRef.value.rl)) {
414
- let tmpNextOffsetX = nextOffsetX;
415
- if (sameLR) {
416
- tmpNextOffsetX -= popupWidth - targetWidth;
417
- } else {
418
- tmpNextOffsetX = targetAlignPointTL.x - popupAlignPointBR.x - popupOffsetX;
419
- }
420
- const newVisibleArea = getIntersectionVisibleArea(
421
- tmpNextOffsetX,
422
- nextOffsetY
423
- );
424
- const newVisibleRecommendArea = getIntersectionVisibleArea(
425
- tmpNextOffsetX,
426
- nextOffsetY,
427
- visibleRegionArea
428
- );
429
- const shouldFlip = shouldSwitchPlacement(
430
- overflowRight,
431
- isVisibleFirst,
432
- newVisibleArea,
433
- originIntersectionVisibleArea,
434
- newVisibleRecommendArea,
435
- originIntersectionRecommendArea
436
- );
437
- if (shouldFlip) {
438
- prevFlipRef.value.rl = true;
439
- nextOffsetX = tmpNextOffsetX;
440
- popupOffsetX = -popupOffsetX;
441
- nextAlignInfo.points = [
442
- reversePoints(popupPoints, 1),
443
- reversePoints(targetPoints, 1)
444
- ];
445
- } else {
446
- prevFlipRef.value.rl = false;
447
- }
448
- }
449
- const overflowLeft = nextPopupX < adjustCheckVisibleArea.left;
450
- if (needAdjustX && popupPoints[1] === "r" && (overflowLeft || prevFlipRef.value.lr)) {
451
- let tmpNextOffsetX = nextOffsetX;
452
- if (sameLR) {
453
- tmpNextOffsetX += popupWidth - targetWidth;
454
- } else {
455
- tmpNextOffsetX = targetAlignPointBR.x - popupAlignPointTL.x - popupOffsetX;
456
- }
457
- const newVisibleArea = getIntersectionVisibleArea(
458
- tmpNextOffsetX,
459
- nextOffsetY
460
- );
461
- const newVisibleRecommendArea = getIntersectionVisibleArea(
462
- tmpNextOffsetX,
463
- nextOffsetY,
464
- visibleRegionArea
465
- );
466
- const shouldFlip = shouldSwitchPlacement(
467
- overflowLeft,
468
- isVisibleFirst,
469
- newVisibleArea,
470
- originIntersectionVisibleArea,
471
- newVisibleRecommendArea,
472
- originIntersectionRecommendArea
473
- );
474
- if (shouldFlip) {
475
- prevFlipRef.value.lr = true;
476
- nextOffsetX = tmpNextOffsetX;
477
- popupOffsetX = -popupOffsetX;
478
- nextAlignInfo.points = [
479
- reversePoints(popupPoints, 1),
480
- reversePoints(targetPoints, 1)
481
- ];
482
- } else {
483
- prevFlipRef.value.lr = false;
484
- }
485
- }
486
- syncNextPopupPosition();
487
- const numShiftX = shiftX === true ? 0 : shiftX;
488
- if (typeof numShiftX === "number") {
489
- if (nextPopupX < visibleRegionArea.left) {
490
- nextOffsetX -= nextPopupX - visibleRegionArea.left - popupOffsetX;
491
- if (targetRect.x + targetWidth < visibleRegionArea.left + numShiftX) {
492
- nextOffsetX += targetRect.x - visibleRegionArea.left + targetWidth - numShiftX;
493
- }
494
- }
495
- if (nextPopupRight > visibleRegionArea.right) {
496
- nextOffsetX -= nextPopupRight - visibleRegionArea.right - popupOffsetX;
497
- if (targetRect.x > visibleRegionArea.right - numShiftX) {
498
- nextOffsetX += targetRect.x - visibleRegionArea.right + numShiftX;
499
- }
500
- }
501
- }
502
- const numShiftY = shiftY === true ? 0 : shiftY;
503
- if (typeof numShiftY === "number") {
504
- if (nextPopupY < visibleRegionArea.top) {
505
- nextOffsetY -= nextPopupY - visibleRegionArea.top - popupOffsetY;
506
- if (targetRect.y + targetHeight < visibleRegionArea.top + numShiftY) {
507
- nextOffsetY += targetRect.y - visibleRegionArea.top + targetHeight - numShiftY;
508
- }
509
- }
510
- if (nextPopupBottom > visibleRegionArea.bottom) {
511
- nextOffsetY -= nextPopupBottom - visibleRegionArea.bottom - popupOffsetY;
512
- if (targetRect.y > visibleRegionArea.bottom - numShiftY) {
513
- nextOffsetY += targetRect.y - visibleRegionArea.bottom + numShiftY;
514
- }
515
- }
516
- }
517
- const popupLeft = popupRect.x + nextOffsetX;
518
- const popupRight = popupLeft + popupWidth;
519
- const popupTop = popupRect.y + nextOffsetY;
520
- const popupBottom = popupTop + popupHeight;
521
- const targetLeft = targetRect.x;
522
- const targetRight = targetLeft + targetWidth;
523
- const targetTop = targetRect.y;
524
- const targetBottom = targetTop + targetHeight;
525
- const maxLeft = Math.max(popupLeft, targetLeft);
526
- const minRight = Math.min(popupRight, targetRight);
527
- const xCenter = (maxLeft + minRight) / 2;
528
- const nextArrowX = xCenter - popupLeft;
529
- const maxTop = Math.max(popupTop, targetTop);
530
- const minBottom = Math.min(popupBottom, targetBottom);
531
- const yCenter = (maxTop + minBottom) / 2;
532
- const nextArrowY = yCenter - popupTop;
533
- onPopupAlign?.(popupEle.value, nextAlignInfo);
534
- let offsetX4Right = popupMirrorRect.right - popupRect.x - (nextOffsetX + popupRect.width);
535
- let offsetY4Bottom = popupMirrorRect.bottom - popupRect.y - (nextOffsetY + popupRect.height);
536
- if (scaleX2 === 1) {
537
- nextOffsetX = Math.round(nextOffsetX);
538
- offsetX4Right = Math.round(offsetX4Right);
539
- }
540
- if (scaleY2 === 1) {
541
- nextOffsetY = Math.round(nextOffsetY);
542
- offsetY4Bottom = Math.round(offsetY4Bottom);
543
- }
544
- const nextOffsetInfo = {
545
- ready: true,
546
- offsetX: nextOffsetX,
547
- offsetY: nextOffsetY,
548
- offsetR: offsetX4Right,
549
- offsetB: offsetY4Bottom,
550
- arrowX: nextArrowX,
551
- arrowY: nextArrowY,
552
- scaleX: scaleX2,
553
- scaleY: scaleY2,
554
- align: nextAlignInfo
555
- };
556
- Object.assign(offsetInfo, nextOffsetInfo);
557
- }
558
- };
559
- const onAlign = rafDebounce(_onAlign);
560
- const triggerAlign = () => {
561
- alignCountRef.value += 1;
562
- const id = alignCountRef.value;
563
- Promise.resolve().then(() => {
564
- if (alignCountRef.value === id) {
565
- onAlign();
566
- }
567
- });
568
- };
569
- watch(
570
- popupEle,
571
- async (ele) => {
572
- if (ele && open.value && !mobile?.value) {
573
- await nextTick();
574
- triggerAlign();
575
- }
576
- }
577
- );
578
- const resetReady = () => {
579
- offsetInfo.ready = false;
580
- };
581
- watch(placement, () => {
582
- resetReady();
583
- });
584
- watch(
585
- open,
586
- () => {
587
- if (!open.value) {
588
- resetFlipCache();
589
- resetReady();
590
- }
591
- },
592
- {
593
- immediate: true
594
- }
595
- );
596
- const { ready, offsetX, offsetR, offsetY, offsetB, align, arrowY, arrowX, scaleY, scaleX } = toRefs(offsetInfo);
597
- return [
598
- ready,
599
- offsetX,
600
- offsetY,
601
- offsetR,
602
- offsetB,
603
- arrowX,
604
- arrowY,
605
- scaleX,
606
- scaleY,
607
- align,
608
- triggerAlign
609
- ];
92
+ const offsetInfo = reactive({
93
+ ready: false,
94
+ offsetX: 0,
95
+ offsetY: 0,
96
+ offsetR: 0,
97
+ offsetB: 0,
98
+ arrowX: 0,
99
+ arrowY: 0,
100
+ scaleX: 1,
101
+ scaleY: 1,
102
+ align: builtinPlacements.value[placement.value] || {}
103
+ });
104
+ const alignCountRef = shallowRef(0);
105
+ const scrollerList = computed(() => {
106
+ if (!popupEle.value || mobile?.value) return [];
107
+ return collectScroller(popupEle.value);
108
+ });
109
+ const prevFlipRef = ref({});
110
+ const resetFlipCache = () => {
111
+ prevFlipRef.value = {};
112
+ };
113
+ const _onAlign = () => {
114
+ if (popupEle.value && target.value && open.value && !mobile?.value) {
115
+ const popupElement = popupEle.value;
116
+ const doc = popupElement.ownerDocument;
117
+ const popupComputedStyle = getWin(popupElement).getComputedStyle(popupElement);
118
+ const { position: popupPosition } = popupComputedStyle;
119
+ const originLeft = popupElement.style.left;
120
+ const originTop = popupElement.style.top;
121
+ const originRight = popupElement.style.right;
122
+ const originBottom = popupElement.style.bottom;
123
+ const originOverflow = popupElement.style.overflow;
124
+ const placementInfo = {
125
+ ...builtinPlacements.value[placement.value],
126
+ ...popupAlign?.value
127
+ };
128
+ const placeholderElement = doc.createElement("div");
129
+ popupElement.parentElement?.appendChild(placeholderElement);
130
+ placeholderElement.style.left = `${popupElement.offsetLeft}px`;
131
+ placeholderElement.style.top = `${popupElement.offsetTop}px`;
132
+ placeholderElement.style.position = popupPosition;
133
+ placeholderElement.style.height = `${popupElement.offsetHeight}px`;
134
+ placeholderElement.style.width = `${popupElement.offsetWidth}px`;
135
+ popupElement.style.left = "0";
136
+ popupElement.style.top = "0";
137
+ popupElement.style.right = "auto";
138
+ popupElement.style.bottom = "auto";
139
+ popupElement.style.overflow = "hidden";
140
+ let targetRect;
141
+ if (Array.isArray(target.value)) targetRect = {
142
+ x: target.value[0],
143
+ y: target.value[1],
144
+ width: 0,
145
+ height: 0
146
+ };
147
+ else {
148
+ const rect = target.value.getBoundingClientRect();
149
+ rect.x = rect.x ?? rect.left;
150
+ rect.y = rect.y ?? rect.top;
151
+ targetRect = {
152
+ x: rect.x,
153
+ y: rect.y,
154
+ width: rect.width,
155
+ height: rect.height
156
+ };
157
+ }
158
+ const rawPopupRect = popupElement.getBoundingClientRect();
159
+ const { clientWidth, clientHeight, scrollWidth, scrollHeight, scrollTop, scrollLeft } = doc.documentElement;
160
+ const targetHeight = targetRect.height;
161
+ const targetWidth = targetRect.width;
162
+ const visibleRegion = {
163
+ left: 0,
164
+ top: 0,
165
+ right: clientWidth,
166
+ bottom: clientHeight
167
+ };
168
+ const scrollRegion = {
169
+ left: -scrollLeft,
170
+ top: -scrollTop,
171
+ right: scrollWidth - scrollLeft,
172
+ bottom: scrollHeight - scrollTop
173
+ };
174
+ let { htmlRegion } = placementInfo;
175
+ const VISIBLE = "visible";
176
+ const VISIBLE_FIRST = "visibleFirst";
177
+ if (htmlRegion !== "scroll" && htmlRegion !== VISIBLE_FIRST) htmlRegion = VISIBLE;
178
+ const isVisibleFirst = htmlRegion === VISIBLE_FIRST;
179
+ const scrollRegionArea = getVisibleArea(scrollRegion, scrollerList.value);
180
+ const visibleRegionArea = getVisibleArea(visibleRegion, scrollerList.value);
181
+ const visibleArea = htmlRegion === VISIBLE ? visibleRegionArea : scrollRegionArea;
182
+ const adjustCheckVisibleArea = isVisibleFirst ? visibleRegionArea : visibleArea;
183
+ popupElement.style.left = "auto";
184
+ popupElement.style.top = "auto";
185
+ popupElement.style.right = "0";
186
+ popupElement.style.bottom = "0";
187
+ const rawPopupMirrorRect = popupElement.getBoundingClientRect();
188
+ popupElement.style.left = originLeft;
189
+ popupElement.style.top = originTop;
190
+ popupElement.style.right = originRight;
191
+ popupElement.style.bottom = originBottom;
192
+ popupElement.style.overflow = originOverflow;
193
+ popupElement.parentElement?.removeChild(placeholderElement);
194
+ const widthValue = parseFloat(popupComputedStyle.width);
195
+ const heightValue = parseFloat(popupComputedStyle.height);
196
+ const baseWidth = !Number.isNaN(widthValue) && widthValue > 0 ? widthValue : popupElement.offsetWidth;
197
+ const baseHeight = !Number.isNaN(heightValue) && heightValue > 0 ? heightValue : popupElement.offsetHeight;
198
+ const safeBaseWidth = baseWidth || rawPopupRect.width || 1;
199
+ const safeBaseHeight = baseHeight || rawPopupRect.height || 1;
200
+ const scaleX$1 = toNum(Math.round(rawPopupRect.width / safeBaseWidth * 1e3) / 1e3);
201
+ const scaleY$1 = toNum(Math.round(rawPopupRect.height / safeBaseHeight * 1e3) / 1e3);
202
+ if (scaleX$1 === 0 || scaleY$1 === 0 || isDOM(target) && !isVisible(target)) return;
203
+ const [originX, originY] = getTransformOriginPoint(popupComputedStyle.transformOrigin, safeBaseWidth, safeBaseHeight);
204
+ const popupRect = normalizeRect(rawPopupRect, scaleX$1, scaleY$1, originX, originY);
205
+ const popupMirrorRect = normalizeRect(rawPopupMirrorRect, scaleX$1, scaleY$1, originX, originY);
206
+ const popupHeight = popupRect.height;
207
+ const popupWidth = popupRect.width;
208
+ const { offset, targetOffset } = placementInfo;
209
+ let [popupOffsetX, popupOffsetY] = getNumberOffset(popupRect, offset);
210
+ const [targetOffsetX, targetOffsetY] = getNumberOffset(targetRect, targetOffset);
211
+ targetRect.x -= targetOffsetX;
212
+ targetRect.y -= targetOffsetY;
213
+ const [popupPoint, targetPoint] = placementInfo.points || [];
214
+ const targetPoints = splitPoints(targetPoint);
215
+ const popupPoints = splitPoints(popupPoint);
216
+ const targetAlignPoint = getAlignPoint(targetRect, targetPoints);
217
+ const popupAlignPoint = getAlignPoint(popupRect, popupPoints);
218
+ const nextAlignInfo = { ...placementInfo };
219
+ let nextOffsetX = targetAlignPoint.x - popupAlignPoint.x + popupOffsetX;
220
+ let nextOffsetY = targetAlignPoint.y - popupAlignPoint.y + popupOffsetY;
221
+ function getIntersectionVisibleArea(offsetX$1, offsetY$1, area = visibleArea) {
222
+ const l = popupRect.x + offsetX$1;
223
+ const t = popupRect.y + offsetY$1;
224
+ const r = l + popupWidth;
225
+ const b = t + popupHeight;
226
+ const visibleL = Math.max(l, area.left);
227
+ const visibleT = Math.max(t, area.top);
228
+ const visibleR = Math.min(r, area.right);
229
+ const visibleB = Math.min(b, area.bottom);
230
+ return Math.max(0, (visibleR - visibleL) * (visibleB - visibleT));
231
+ }
232
+ const originIntersectionVisibleArea = getIntersectionVisibleArea(nextOffsetX, nextOffsetY);
233
+ const originIntersectionRecommendArea = getIntersectionVisibleArea(nextOffsetX, nextOffsetY, visibleRegionArea);
234
+ const targetAlignPointTL = getAlignPoint(targetRect, ["t", "l"]);
235
+ const popupAlignPointTL = getAlignPoint(popupRect, ["t", "l"]);
236
+ const targetAlignPointBR = getAlignPoint(targetRect, ["b", "r"]);
237
+ const popupAlignPointBR = getAlignPoint(popupRect, ["b", "r"]);
238
+ const { adjustX, adjustY, shiftX, shiftY } = placementInfo.overflow || {};
239
+ const supportAdjust = (val) => {
240
+ if (typeof val === "boolean") return val;
241
+ return val >= 0;
242
+ };
243
+ let nextPopupY;
244
+ let nextPopupBottom;
245
+ let nextPopupX;
246
+ let nextPopupRight;
247
+ function syncNextPopupPosition() {
248
+ nextPopupY = popupRect.y + nextOffsetY;
249
+ nextPopupBottom = nextPopupY + popupHeight;
250
+ nextPopupX = popupRect.x + nextOffsetX;
251
+ nextPopupRight = nextPopupX + popupWidth;
252
+ }
253
+ syncNextPopupPosition();
254
+ const needAdjustY = supportAdjust(adjustY);
255
+ const sameTB = popupPoints[0] === targetPoints[0];
256
+ const overflowBottom = nextPopupBottom > adjustCheckVisibleArea.bottom;
257
+ if (needAdjustY && popupPoints[0] === "t" && (overflowBottom || prevFlipRef.value.bt)) {
258
+ let tmpNextOffsetY = nextOffsetY;
259
+ if (sameTB) tmpNextOffsetY -= popupHeight - targetHeight;
260
+ else tmpNextOffsetY = targetAlignPointTL.y - popupAlignPointBR.y - popupOffsetY;
261
+ if (shouldSwitchPlacement(overflowBottom, isVisibleFirst, getIntersectionVisibleArea(nextOffsetX, tmpNextOffsetY), originIntersectionVisibleArea, getIntersectionVisibleArea(nextOffsetX, tmpNextOffsetY, visibleRegionArea), originIntersectionRecommendArea)) {
262
+ prevFlipRef.value.bt = true;
263
+ nextOffsetY = tmpNextOffsetY;
264
+ popupOffsetY = -popupOffsetY;
265
+ nextAlignInfo.points = [reversePoints(popupPoints, 0), reversePoints(targetPoints, 0)];
266
+ } else prevFlipRef.value.bt = false;
267
+ }
268
+ const overflowTop = nextPopupY < adjustCheckVisibleArea.top;
269
+ if (needAdjustY && popupPoints[0] === "b" && (overflowTop || prevFlipRef.value.tb)) {
270
+ let tmpNextOffsetY = nextOffsetY;
271
+ if (sameTB) tmpNextOffsetY += popupHeight - targetHeight;
272
+ else tmpNextOffsetY = targetAlignPointBR.y - popupAlignPointTL.y - popupOffsetY;
273
+ if (shouldSwitchPlacement(overflowTop, isVisibleFirst, getIntersectionVisibleArea(nextOffsetX, tmpNextOffsetY), originIntersectionVisibleArea, getIntersectionVisibleArea(nextOffsetX, tmpNextOffsetY, visibleRegionArea), originIntersectionRecommendArea)) {
274
+ prevFlipRef.value.tb = true;
275
+ nextOffsetY = tmpNextOffsetY;
276
+ popupOffsetY = -popupOffsetY;
277
+ nextAlignInfo.points = [reversePoints(popupPoints, 0), reversePoints(targetPoints, 0)];
278
+ } else prevFlipRef.value.tb = false;
279
+ }
280
+ const needAdjustX = supportAdjust(adjustX);
281
+ const sameLR = popupPoints[1] === targetPoints[1];
282
+ const overflowRight = nextPopupRight > adjustCheckVisibleArea.right;
283
+ if (needAdjustX && popupPoints[1] === "l" && (overflowRight || prevFlipRef.value.rl)) {
284
+ let tmpNextOffsetX = nextOffsetX;
285
+ if (sameLR) tmpNextOffsetX -= popupWidth - targetWidth;
286
+ else tmpNextOffsetX = targetAlignPointTL.x - popupAlignPointBR.x - popupOffsetX;
287
+ if (shouldSwitchPlacement(overflowRight, isVisibleFirst, getIntersectionVisibleArea(tmpNextOffsetX, nextOffsetY), originIntersectionVisibleArea, getIntersectionVisibleArea(tmpNextOffsetX, nextOffsetY, visibleRegionArea), originIntersectionRecommendArea)) {
288
+ prevFlipRef.value.rl = true;
289
+ nextOffsetX = tmpNextOffsetX;
290
+ popupOffsetX = -popupOffsetX;
291
+ nextAlignInfo.points = [reversePoints(popupPoints, 1), reversePoints(targetPoints, 1)];
292
+ } else prevFlipRef.value.rl = false;
293
+ }
294
+ const overflowLeft = nextPopupX < adjustCheckVisibleArea.left;
295
+ if (needAdjustX && popupPoints[1] === "r" && (overflowLeft || prevFlipRef.value.lr)) {
296
+ let tmpNextOffsetX = nextOffsetX;
297
+ if (sameLR) tmpNextOffsetX += popupWidth - targetWidth;
298
+ else tmpNextOffsetX = targetAlignPointBR.x - popupAlignPointTL.x - popupOffsetX;
299
+ if (shouldSwitchPlacement(overflowLeft, isVisibleFirst, getIntersectionVisibleArea(tmpNextOffsetX, nextOffsetY), originIntersectionVisibleArea, getIntersectionVisibleArea(tmpNextOffsetX, nextOffsetY, visibleRegionArea), originIntersectionRecommendArea)) {
300
+ prevFlipRef.value.lr = true;
301
+ nextOffsetX = tmpNextOffsetX;
302
+ popupOffsetX = -popupOffsetX;
303
+ nextAlignInfo.points = [reversePoints(popupPoints, 1), reversePoints(targetPoints, 1)];
304
+ } else prevFlipRef.value.lr = false;
305
+ }
306
+ syncNextPopupPosition();
307
+ const numShiftX = shiftX === true ? 0 : shiftX;
308
+ if (typeof numShiftX === "number") {
309
+ if (nextPopupX < visibleRegionArea.left) {
310
+ nextOffsetX -= nextPopupX - visibleRegionArea.left - popupOffsetX;
311
+ if (targetRect.x + targetWidth < visibleRegionArea.left + numShiftX) nextOffsetX += targetRect.x - visibleRegionArea.left + targetWidth - numShiftX;
312
+ }
313
+ if (nextPopupRight > visibleRegionArea.right) {
314
+ nextOffsetX -= nextPopupRight - visibleRegionArea.right - popupOffsetX;
315
+ if (targetRect.x > visibleRegionArea.right - numShiftX) nextOffsetX += targetRect.x - visibleRegionArea.right + numShiftX;
316
+ }
317
+ }
318
+ const numShiftY = shiftY === true ? 0 : shiftY;
319
+ if (typeof numShiftY === "number") {
320
+ if (nextPopupY < visibleRegionArea.top) {
321
+ nextOffsetY -= nextPopupY - visibleRegionArea.top - popupOffsetY;
322
+ if (targetRect.y + targetHeight < visibleRegionArea.top + numShiftY) nextOffsetY += targetRect.y - visibleRegionArea.top + targetHeight - numShiftY;
323
+ }
324
+ if (nextPopupBottom > visibleRegionArea.bottom) {
325
+ nextOffsetY -= nextPopupBottom - visibleRegionArea.bottom - popupOffsetY;
326
+ if (targetRect.y > visibleRegionArea.bottom - numShiftY) nextOffsetY += targetRect.y - visibleRegionArea.bottom + numShiftY;
327
+ }
328
+ }
329
+ const popupLeft = popupRect.x + nextOffsetX;
330
+ const popupRight = popupLeft + popupWidth;
331
+ const popupTop = popupRect.y + nextOffsetY;
332
+ const popupBottom = popupTop + popupHeight;
333
+ const targetLeft = targetRect.x;
334
+ const targetRight = targetLeft + targetWidth;
335
+ const targetTop = targetRect.y;
336
+ const targetBottom = targetTop + targetHeight;
337
+ const nextArrowX = (Math.max(popupLeft, targetLeft) + Math.min(popupRight, targetRight)) / 2 - popupLeft;
338
+ const nextArrowY = (Math.max(popupTop, targetTop) + Math.min(popupBottom, targetBottom)) / 2 - popupTop;
339
+ onPopupAlign?.(popupEle.value, nextAlignInfo);
340
+ let offsetX4Right = popupMirrorRect.right - popupRect.x - (nextOffsetX + popupRect.width);
341
+ let offsetY4Bottom = popupMirrorRect.bottom - popupRect.y - (nextOffsetY + popupRect.height);
342
+ if (scaleX$1 === 1) {
343
+ nextOffsetX = Math.round(nextOffsetX);
344
+ offsetX4Right = Math.round(offsetX4Right);
345
+ }
346
+ if (scaleY$1 === 1) {
347
+ nextOffsetY = Math.round(nextOffsetY);
348
+ offsetY4Bottom = Math.round(offsetY4Bottom);
349
+ }
350
+ const nextOffsetInfo = {
351
+ ready: true,
352
+ offsetX: nextOffsetX,
353
+ offsetY: nextOffsetY,
354
+ offsetR: offsetX4Right,
355
+ offsetB: offsetY4Bottom,
356
+ arrowX: nextArrowX,
357
+ arrowY: nextArrowY,
358
+ scaleX: scaleX$1,
359
+ scaleY: scaleY$1,
360
+ align: nextAlignInfo
361
+ };
362
+ Object.assign(offsetInfo, nextOffsetInfo);
363
+ }
364
+ };
365
+ const onAlign = rafDebounce(_onAlign);
366
+ const triggerAlign = () => {
367
+ alignCountRef.value += 1;
368
+ const id = alignCountRef.value;
369
+ Promise.resolve().then(() => {
370
+ if (alignCountRef.value === id) onAlign();
371
+ });
372
+ };
373
+ watch(popupEle, async (ele) => {
374
+ if (ele && open.value && !mobile?.value) {
375
+ await nextTick();
376
+ triggerAlign();
377
+ }
378
+ });
379
+ const resetReady = () => {
380
+ offsetInfo.ready = false;
381
+ };
382
+ watch(placement, () => {
383
+ resetReady();
384
+ });
385
+ watch(open, () => {
386
+ if (!open.value) {
387
+ resetFlipCache();
388
+ resetReady();
389
+ }
390
+ }, { immediate: true });
391
+ const { ready, offsetX, offsetR, offsetY, offsetB, align, arrowY, arrowX, scaleY, scaleX } = toRefs(offsetInfo);
392
+ return [
393
+ ready,
394
+ offsetX,
395
+ offsetY,
396
+ offsetR,
397
+ offsetB,
398
+ arrowX,
399
+ arrowY,
400
+ scaleX,
401
+ scaleY,
402
+ align,
403
+ triggerAlign
404
+ ];
610
405
  }
611
- export {
612
- useAlign as default
613
- };
406
+ export { useAlign as default };