@v-c/trigger 0.0.10 → 0.0.11
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.
- package/dist/Popup/Arrow.cjs +91 -1
- package/dist/Popup/Arrow.js +60 -38
- package/dist/Popup/Mask.cjs +66 -1
- package/dist/Popup/Mask.js +35 -32
- package/dist/Popup/PopupContent.cjs +29 -1
- package/dist/Popup/PopupContent.js +15 -8
- package/dist/Popup/index.cjs +365 -1
- package/dist/Popup/index.js +194 -160
- package/dist/UniqueProvider/UniqueContainer.cjs +159 -1
- package/dist/UniqueProvider/UniqueContainer.js +87 -67
- package/dist/UniqueProvider/index.cjs +213 -1
- package/dist/UniqueProvider/index.js +177 -131
- package/dist/UniqueProvider/useTargetState.cjs +42 -1
- package/dist/UniqueProvider/useTargetState.js +39 -10
- package/dist/context.cjs +38 -1
- package/dist/context.js +27 -16
- package/dist/hooks/useAction.cjs +32 -1
- package/dist/hooks/useAction.js +27 -12
- package/dist/hooks/useAlign.cjs +499 -1
- package/dist/hooks/useAlign.d.ts +1 -1
- package/dist/hooks/useAlign.js +445 -208
- package/dist/hooks/useDelay.cjs +27 -1
- package/dist/hooks/useDelay.js +23 -12
- package/dist/hooks/useOffsetStyle.cjs +36 -1
- package/dist/hooks/useOffsetStyle.js +28 -12
- package/dist/hooks/useWatch.cjs +37 -1
- package/dist/hooks/useWatch.js +28 -17
- package/dist/hooks/useWinClick.cjs +67 -1
- package/dist/hooks/useWinClick.js +56 -39
- package/dist/index.cjs +700 -1
- package/dist/index.js +501 -312
- package/dist/util.cjs +101 -1
- package/dist/util.js +86 -54
- package/package.json +2 -2
package/dist/hooks/useAlign.cjs
CHANGED
|
@@ -1 +1,499 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const findDOMNode = require("@v-c/util/dist/Dom/findDOMNode");
|
|
4
|
+
const isVisible = require("@v-c/util/dist/Dom/isVisible");
|
|
5
|
+
const raf = require("@v-c/util/dist/raf");
|
|
6
|
+
const vue = require("vue");
|
|
7
|
+
const util = require("../util.cjs");
|
|
8
|
+
function getUnitOffset(size, offset = 0) {
|
|
9
|
+
const offsetStr = `${offset}`;
|
|
10
|
+
const cells = offsetStr.match(/^(.*)\%$/);
|
|
11
|
+
if (cells) {
|
|
12
|
+
return size * (parseFloat(cells[1]) / 100);
|
|
13
|
+
}
|
|
14
|
+
return parseFloat(offsetStr);
|
|
15
|
+
}
|
|
16
|
+
function getNumberOffset(rect, offset) {
|
|
17
|
+
const [offsetX, offsetY] = offset || [];
|
|
18
|
+
return [
|
|
19
|
+
getUnitOffset(rect.width, offsetX),
|
|
20
|
+
getUnitOffset(rect.height, offsetY)
|
|
21
|
+
];
|
|
22
|
+
}
|
|
23
|
+
function splitPoints(points = "") {
|
|
24
|
+
return [points[0], points[1]];
|
|
25
|
+
}
|
|
26
|
+
function getAlignPoint(rect, points) {
|
|
27
|
+
const topBottom = points[0];
|
|
28
|
+
const leftRight = points[1];
|
|
29
|
+
let x;
|
|
30
|
+
let y;
|
|
31
|
+
if (topBottom === "t") {
|
|
32
|
+
y = rect.y;
|
|
33
|
+
} else if (topBottom === "b") {
|
|
34
|
+
y = rect.y + rect.height;
|
|
35
|
+
} else {
|
|
36
|
+
y = rect.y + rect.height / 2;
|
|
37
|
+
}
|
|
38
|
+
if (leftRight === "l") {
|
|
39
|
+
x = rect.x;
|
|
40
|
+
} else if (leftRight === "r") {
|
|
41
|
+
x = rect.x + rect.width;
|
|
42
|
+
} else {
|
|
43
|
+
x = rect.x + rect.width / 2;
|
|
44
|
+
}
|
|
45
|
+
return { x, y };
|
|
46
|
+
}
|
|
47
|
+
function reversePoints(points, index) {
|
|
48
|
+
const reverseMap = {
|
|
49
|
+
t: "b",
|
|
50
|
+
b: "t",
|
|
51
|
+
l: "r",
|
|
52
|
+
r: "l"
|
|
53
|
+
};
|
|
54
|
+
return points.map((point, i) => {
|
|
55
|
+
if (i === index) {
|
|
56
|
+
return reverseMap[point] || "c";
|
|
57
|
+
}
|
|
58
|
+
return point;
|
|
59
|
+
}).join("");
|
|
60
|
+
}
|
|
61
|
+
function useAlign(open, popupEle, target, placement, builtinPlacements, popupAlign, onPopupAlign, mobile) {
|
|
62
|
+
const offsetInfo = vue.reactive({
|
|
63
|
+
ready: false,
|
|
64
|
+
offsetX: 0,
|
|
65
|
+
offsetY: 0,
|
|
66
|
+
offsetR: 0,
|
|
67
|
+
offsetB: 0,
|
|
68
|
+
arrowX: 0,
|
|
69
|
+
arrowY: 0,
|
|
70
|
+
scaleX: 1,
|
|
71
|
+
scaleY: 1,
|
|
72
|
+
align: builtinPlacements.value[placement.value] || {}
|
|
73
|
+
});
|
|
74
|
+
const alignCountRef = vue.shallowRef(0);
|
|
75
|
+
const scrollerList = vue.computed(() => {
|
|
76
|
+
if (!popupEle.value || mobile?.value) {
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
return util.collectScroller(popupEle.value);
|
|
80
|
+
});
|
|
81
|
+
const prevFlipRef = vue.ref({});
|
|
82
|
+
const resetFlipCache = () => {
|
|
83
|
+
prevFlipRef.value = {};
|
|
84
|
+
};
|
|
85
|
+
const _onAlign = () => {
|
|
86
|
+
if (popupEle.value && target.value && open.value && !mobile?.value) {
|
|
87
|
+
let getIntersectionVisibleArea = function(offsetX2, offsetY2, area = visibleArea) {
|
|
88
|
+
const l = popupRect.x + offsetX2;
|
|
89
|
+
const t = popupRect.y + offsetY2;
|
|
90
|
+
const r = l + popupWidth;
|
|
91
|
+
const b = t + popupHeight;
|
|
92
|
+
const visibleL = Math.max(l, area.left);
|
|
93
|
+
const visibleT = Math.max(t, area.top);
|
|
94
|
+
const visibleR = Math.min(r, area.right);
|
|
95
|
+
const visibleB = Math.min(b, area.bottom);
|
|
96
|
+
return Math.max(0, (visibleR - visibleL) * (visibleB - visibleT));
|
|
97
|
+
}, syncNextPopupPosition = function() {
|
|
98
|
+
nextPopupY = popupRect.y + nextOffsetY;
|
|
99
|
+
nextPopupBottom = nextPopupY + popupHeight;
|
|
100
|
+
nextPopupX = popupRect.x + nextOffsetX;
|
|
101
|
+
nextPopupRight = nextPopupX + popupWidth;
|
|
102
|
+
};
|
|
103
|
+
const popupElement = popupEle.value;
|
|
104
|
+
const doc = popupElement.ownerDocument;
|
|
105
|
+
const win = util.getWin(popupElement);
|
|
106
|
+
const { position: popupPosition } = win.getComputedStyle(popupElement);
|
|
107
|
+
const originLeft = popupElement.style.left;
|
|
108
|
+
const originTop = popupElement.style.top;
|
|
109
|
+
const originRight = popupElement.style.right;
|
|
110
|
+
const originBottom = popupElement.style.bottom;
|
|
111
|
+
const originOverflow = popupElement.style.overflow;
|
|
112
|
+
const placementInfo = {
|
|
113
|
+
...builtinPlacements.value[placement.value],
|
|
114
|
+
...popupAlign?.value
|
|
115
|
+
};
|
|
116
|
+
const placeholderElement = doc.createElement("div");
|
|
117
|
+
popupElement.parentElement?.appendChild(placeholderElement);
|
|
118
|
+
placeholderElement.style.left = `${popupElement.offsetLeft}px`;
|
|
119
|
+
placeholderElement.style.top = `${popupElement.offsetTop}px`;
|
|
120
|
+
placeholderElement.style.position = popupPosition;
|
|
121
|
+
placeholderElement.style.height = `${popupElement.offsetHeight}px`;
|
|
122
|
+
placeholderElement.style.width = `${popupElement.offsetWidth}px`;
|
|
123
|
+
popupElement.style.left = "0";
|
|
124
|
+
popupElement.style.top = "0";
|
|
125
|
+
popupElement.style.right = "auto";
|
|
126
|
+
popupElement.style.bottom = "auto";
|
|
127
|
+
popupElement.style.overflow = "hidden";
|
|
128
|
+
let targetRect;
|
|
129
|
+
if (Array.isArray(target.value)) {
|
|
130
|
+
targetRect = {
|
|
131
|
+
x: target.value[0],
|
|
132
|
+
y: target.value[1],
|
|
133
|
+
width: 0,
|
|
134
|
+
height: 0
|
|
135
|
+
};
|
|
136
|
+
} else {
|
|
137
|
+
const rect = target.value.getBoundingClientRect();
|
|
138
|
+
rect.x = rect.x ?? rect.left;
|
|
139
|
+
rect.y = rect.y ?? rect.top;
|
|
140
|
+
targetRect = {
|
|
141
|
+
x: rect.x,
|
|
142
|
+
y: rect.y,
|
|
143
|
+
width: rect.width,
|
|
144
|
+
height: rect.height
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
const popupRect = popupElement.getBoundingClientRect();
|
|
148
|
+
const { height, width } = win.getComputedStyle(popupElement);
|
|
149
|
+
popupRect.x = popupRect.x ?? popupRect.left;
|
|
150
|
+
popupRect.y = popupRect.y ?? popupRect.top;
|
|
151
|
+
const {
|
|
152
|
+
clientWidth,
|
|
153
|
+
clientHeight,
|
|
154
|
+
scrollWidth,
|
|
155
|
+
scrollHeight,
|
|
156
|
+
scrollTop,
|
|
157
|
+
scrollLeft
|
|
158
|
+
} = doc.documentElement;
|
|
159
|
+
const popupHeight = popupRect.height;
|
|
160
|
+
const popupWidth = popupRect.width;
|
|
161
|
+
const targetHeight = targetRect.height;
|
|
162
|
+
const targetWidth = targetRect.width;
|
|
163
|
+
const visibleRegion = {
|
|
164
|
+
left: 0,
|
|
165
|
+
top: 0,
|
|
166
|
+
right: clientWidth,
|
|
167
|
+
bottom: clientHeight
|
|
168
|
+
};
|
|
169
|
+
const scrollRegion = {
|
|
170
|
+
left: -scrollLeft,
|
|
171
|
+
top: -scrollTop,
|
|
172
|
+
right: scrollWidth - scrollLeft,
|
|
173
|
+
bottom: scrollHeight - scrollTop
|
|
174
|
+
};
|
|
175
|
+
let { htmlRegion } = placementInfo;
|
|
176
|
+
const VISIBLE = "visible";
|
|
177
|
+
const VISIBLE_FIRST = "visibleFirst";
|
|
178
|
+
if (htmlRegion !== "scroll" && htmlRegion !== VISIBLE_FIRST) {
|
|
179
|
+
htmlRegion = VISIBLE;
|
|
180
|
+
}
|
|
181
|
+
const isVisibleFirst = htmlRegion === VISIBLE_FIRST;
|
|
182
|
+
const scrollRegionArea = util.getVisibleArea(scrollRegion, scrollerList.value);
|
|
183
|
+
const visibleRegionArea = util.getVisibleArea(visibleRegion, scrollerList.value);
|
|
184
|
+
const visibleArea = htmlRegion === VISIBLE ? visibleRegionArea : scrollRegionArea;
|
|
185
|
+
const adjustCheckVisibleArea = isVisibleFirst ? visibleRegionArea : visibleArea;
|
|
186
|
+
popupElement.style.left = "auto";
|
|
187
|
+
popupElement.style.top = "auto";
|
|
188
|
+
popupElement.style.right = "0";
|
|
189
|
+
popupElement.style.bottom = "0";
|
|
190
|
+
const popupMirrorRect = popupElement.getBoundingClientRect();
|
|
191
|
+
popupElement.style.left = originLeft;
|
|
192
|
+
popupElement.style.top = originTop;
|
|
193
|
+
popupElement.style.right = originRight;
|
|
194
|
+
popupElement.style.bottom = originBottom;
|
|
195
|
+
popupElement.style.overflow = originOverflow;
|
|
196
|
+
popupElement.parentElement?.removeChild(placeholderElement);
|
|
197
|
+
const scaleX2 = util.toNum(
|
|
198
|
+
Math.round(popupWidth / parseFloat(width) * 1e3) / 1e3
|
|
199
|
+
);
|
|
200
|
+
const scaleY2 = util.toNum(
|
|
201
|
+
Math.round(popupHeight / parseFloat(height) * 1e3) / 1e3
|
|
202
|
+
);
|
|
203
|
+
if (scaleX2 === 0 || scaleY2 === 0 || findDOMNode.isDOM(target) && !isVisible(target)) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const { offset, targetOffset } = placementInfo;
|
|
207
|
+
let [popupOffsetX, popupOffsetY] = getNumberOffset(popupRect, offset);
|
|
208
|
+
const [targetOffsetX, targetOffsetY] = getNumberOffset(
|
|
209
|
+
targetRect,
|
|
210
|
+
targetOffset
|
|
211
|
+
);
|
|
212
|
+
targetRect.x -= targetOffsetX;
|
|
213
|
+
targetRect.y -= targetOffsetY;
|
|
214
|
+
const [popupPoint, targetPoint] = placementInfo.points || [];
|
|
215
|
+
const targetPoints = splitPoints(targetPoint);
|
|
216
|
+
const popupPoints = splitPoints(popupPoint);
|
|
217
|
+
const targetAlignPoint = getAlignPoint(targetRect, targetPoints);
|
|
218
|
+
const popupAlignPoint = getAlignPoint(popupRect, popupPoints);
|
|
219
|
+
const nextAlignInfo = {
|
|
220
|
+
...placementInfo
|
|
221
|
+
};
|
|
222
|
+
let nextOffsetX = targetAlignPoint.x - popupAlignPoint.x + popupOffsetX;
|
|
223
|
+
let nextOffsetY = targetAlignPoint.y - popupAlignPoint.y + popupOffsetY;
|
|
224
|
+
const originIntersectionVisibleArea = getIntersectionVisibleArea(
|
|
225
|
+
nextOffsetX,
|
|
226
|
+
nextOffsetY
|
|
227
|
+
);
|
|
228
|
+
const originIntersectionRecommendArea = getIntersectionVisibleArea(
|
|
229
|
+
nextOffsetX,
|
|
230
|
+
nextOffsetY,
|
|
231
|
+
visibleRegionArea
|
|
232
|
+
);
|
|
233
|
+
const targetAlignPointTL = getAlignPoint(targetRect, ["t", "l"]);
|
|
234
|
+
const popupAlignPointTL = getAlignPoint(popupRect, ["t", "l"]);
|
|
235
|
+
const targetAlignPointBR = getAlignPoint(targetRect, ["b", "r"]);
|
|
236
|
+
const popupAlignPointBR = getAlignPoint(popupRect, ["b", "r"]);
|
|
237
|
+
const overflow = placementInfo.overflow || {};
|
|
238
|
+
const { adjustX, adjustY, shiftX, shiftY } = overflow;
|
|
239
|
+
const supportAdjust = (val) => {
|
|
240
|
+
if (typeof val === "boolean") {
|
|
241
|
+
return val;
|
|
242
|
+
}
|
|
243
|
+
return val >= 0;
|
|
244
|
+
};
|
|
245
|
+
let nextPopupY;
|
|
246
|
+
let nextPopupBottom;
|
|
247
|
+
let nextPopupX;
|
|
248
|
+
let nextPopupRight;
|
|
249
|
+
syncNextPopupPosition();
|
|
250
|
+
const needAdjustY = supportAdjust(adjustY);
|
|
251
|
+
const sameTB = popupPoints[0] === targetPoints[0];
|
|
252
|
+
if (needAdjustY && popupPoints[0] === "t" && (nextPopupBottom > adjustCheckVisibleArea.bottom || prevFlipRef.value.bt)) {
|
|
253
|
+
let tmpNextOffsetY = nextOffsetY;
|
|
254
|
+
if (sameTB) {
|
|
255
|
+
tmpNextOffsetY -= popupHeight - targetHeight;
|
|
256
|
+
} else {
|
|
257
|
+
tmpNextOffsetY = targetAlignPointTL.y - popupAlignPointBR.y - popupOffsetY;
|
|
258
|
+
}
|
|
259
|
+
const newVisibleArea = getIntersectionVisibleArea(
|
|
260
|
+
nextOffsetX,
|
|
261
|
+
tmpNextOffsetY
|
|
262
|
+
);
|
|
263
|
+
const newVisibleRecommendArea = getIntersectionVisibleArea(
|
|
264
|
+
nextOffsetX,
|
|
265
|
+
tmpNextOffsetY,
|
|
266
|
+
visibleRegionArea
|
|
267
|
+
);
|
|
268
|
+
if (
|
|
269
|
+
// Of course use larger one
|
|
270
|
+
newVisibleArea > originIntersectionVisibleArea || newVisibleArea === originIntersectionVisibleArea && (!isVisibleFirst || newVisibleRecommendArea >= originIntersectionRecommendArea)
|
|
271
|
+
) {
|
|
272
|
+
prevFlipRef.value.bt = true;
|
|
273
|
+
nextOffsetY = tmpNextOffsetY;
|
|
274
|
+
popupOffsetY = -popupOffsetY;
|
|
275
|
+
nextAlignInfo.points = [
|
|
276
|
+
reversePoints(popupPoints, 0),
|
|
277
|
+
reversePoints(targetPoints, 0)
|
|
278
|
+
];
|
|
279
|
+
} else {
|
|
280
|
+
prevFlipRef.value.bt = false;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
if (needAdjustY && popupPoints[0] === "b" && (nextPopupY < adjustCheckVisibleArea.top || prevFlipRef.value.tb)) {
|
|
284
|
+
let tmpNextOffsetY = nextOffsetY;
|
|
285
|
+
if (sameTB) {
|
|
286
|
+
tmpNextOffsetY += popupHeight - targetHeight;
|
|
287
|
+
} else {
|
|
288
|
+
tmpNextOffsetY = targetAlignPointBR.y - popupAlignPointTL.y - popupOffsetY;
|
|
289
|
+
}
|
|
290
|
+
const newVisibleArea = getIntersectionVisibleArea(
|
|
291
|
+
nextOffsetX,
|
|
292
|
+
tmpNextOffsetY
|
|
293
|
+
);
|
|
294
|
+
const newVisibleRecommendArea = getIntersectionVisibleArea(
|
|
295
|
+
nextOffsetX,
|
|
296
|
+
tmpNextOffsetY,
|
|
297
|
+
visibleRegionArea
|
|
298
|
+
);
|
|
299
|
+
if (
|
|
300
|
+
// Of course use larger one
|
|
301
|
+
newVisibleArea > originIntersectionVisibleArea || newVisibleArea === originIntersectionVisibleArea && (!isVisibleFirst || newVisibleRecommendArea >= originIntersectionRecommendArea)
|
|
302
|
+
) {
|
|
303
|
+
prevFlipRef.value.tb = true;
|
|
304
|
+
nextOffsetY = tmpNextOffsetY;
|
|
305
|
+
popupOffsetY = -popupOffsetY;
|
|
306
|
+
nextAlignInfo.points = [
|
|
307
|
+
reversePoints(popupPoints, 0),
|
|
308
|
+
reversePoints(targetPoints, 0)
|
|
309
|
+
];
|
|
310
|
+
} else {
|
|
311
|
+
prevFlipRef.value.tb = false;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
const needAdjustX = supportAdjust(adjustX);
|
|
315
|
+
const sameLR = popupPoints[1] === targetPoints[1];
|
|
316
|
+
if (needAdjustX && popupPoints[1] === "l" && (nextPopupRight > adjustCheckVisibleArea.right || prevFlipRef.value.rl)) {
|
|
317
|
+
let tmpNextOffsetX = nextOffsetX;
|
|
318
|
+
if (sameLR) {
|
|
319
|
+
tmpNextOffsetX -= popupWidth - targetWidth;
|
|
320
|
+
} else {
|
|
321
|
+
tmpNextOffsetX = targetAlignPointTL.x - popupAlignPointBR.x - popupOffsetX;
|
|
322
|
+
}
|
|
323
|
+
const newVisibleArea = getIntersectionVisibleArea(
|
|
324
|
+
tmpNextOffsetX,
|
|
325
|
+
nextOffsetY
|
|
326
|
+
);
|
|
327
|
+
const newVisibleRecommendArea = getIntersectionVisibleArea(
|
|
328
|
+
tmpNextOffsetX,
|
|
329
|
+
nextOffsetY,
|
|
330
|
+
visibleRegionArea
|
|
331
|
+
);
|
|
332
|
+
if (
|
|
333
|
+
// Of course use larger one
|
|
334
|
+
newVisibleArea > originIntersectionVisibleArea || newVisibleArea === originIntersectionVisibleArea && (!isVisibleFirst || newVisibleRecommendArea >= originIntersectionRecommendArea)
|
|
335
|
+
) {
|
|
336
|
+
prevFlipRef.value.rl = true;
|
|
337
|
+
nextOffsetX = tmpNextOffsetX;
|
|
338
|
+
popupOffsetX = -popupOffsetX;
|
|
339
|
+
nextAlignInfo.points = [
|
|
340
|
+
reversePoints(popupPoints, 1),
|
|
341
|
+
reversePoints(targetPoints, 1)
|
|
342
|
+
];
|
|
343
|
+
} else {
|
|
344
|
+
prevFlipRef.value.rl = false;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
if (needAdjustX && popupPoints[1] === "r" && (nextPopupX < adjustCheckVisibleArea.left || prevFlipRef.value.lr)) {
|
|
348
|
+
let tmpNextOffsetX = nextOffsetX;
|
|
349
|
+
if (sameLR) {
|
|
350
|
+
tmpNextOffsetX += popupWidth - targetWidth;
|
|
351
|
+
} else {
|
|
352
|
+
tmpNextOffsetX = targetAlignPointBR.x - popupAlignPointTL.x - popupOffsetX;
|
|
353
|
+
}
|
|
354
|
+
const newVisibleArea = getIntersectionVisibleArea(
|
|
355
|
+
tmpNextOffsetX,
|
|
356
|
+
nextOffsetY
|
|
357
|
+
);
|
|
358
|
+
const newVisibleRecommendArea = getIntersectionVisibleArea(
|
|
359
|
+
tmpNextOffsetX,
|
|
360
|
+
nextOffsetY,
|
|
361
|
+
visibleRegionArea
|
|
362
|
+
);
|
|
363
|
+
if (
|
|
364
|
+
// Of course use larger one
|
|
365
|
+
newVisibleArea > originIntersectionVisibleArea || newVisibleArea === originIntersectionVisibleArea && (!isVisibleFirst || newVisibleRecommendArea >= originIntersectionRecommendArea)
|
|
366
|
+
) {
|
|
367
|
+
prevFlipRef.value.lr = true;
|
|
368
|
+
nextOffsetX = tmpNextOffsetX;
|
|
369
|
+
popupOffsetX = -popupOffsetX;
|
|
370
|
+
nextAlignInfo.points = [
|
|
371
|
+
reversePoints(popupPoints, 1),
|
|
372
|
+
reversePoints(targetPoints, 1)
|
|
373
|
+
];
|
|
374
|
+
} else {
|
|
375
|
+
prevFlipRef.value.lr = false;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
syncNextPopupPosition();
|
|
379
|
+
const numShiftX = shiftX === true ? 0 : shiftX;
|
|
380
|
+
if (typeof numShiftX === "number") {
|
|
381
|
+
if (nextPopupX < visibleRegionArea.left) {
|
|
382
|
+
nextOffsetX -= nextPopupX - visibleRegionArea.left - popupOffsetX;
|
|
383
|
+
if (targetRect.x + targetWidth < visibleRegionArea.left + numShiftX) {
|
|
384
|
+
nextOffsetX += targetRect.x - visibleRegionArea.left + targetWidth - numShiftX;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
if (nextPopupRight > visibleRegionArea.right) {
|
|
388
|
+
nextOffsetX -= nextPopupRight - visibleRegionArea.right - popupOffsetX;
|
|
389
|
+
if (targetRect.x > visibleRegionArea.right - numShiftX) {
|
|
390
|
+
nextOffsetX += targetRect.x - visibleRegionArea.right + numShiftX;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
const numShiftY = shiftY === true ? 0 : shiftY;
|
|
395
|
+
if (typeof numShiftY === "number") {
|
|
396
|
+
if (nextPopupY < visibleRegionArea.top) {
|
|
397
|
+
nextOffsetY -= nextPopupY - visibleRegionArea.top - popupOffsetY;
|
|
398
|
+
if (targetRect.y + targetHeight < visibleRegionArea.top + numShiftY) {
|
|
399
|
+
nextOffsetY += targetRect.y - visibleRegionArea.top + targetHeight - numShiftY;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
if (nextPopupBottom > visibleRegionArea.bottom) {
|
|
403
|
+
nextOffsetY -= nextPopupBottom - visibleRegionArea.bottom - popupOffsetY;
|
|
404
|
+
if (targetRect.y > visibleRegionArea.bottom - numShiftY) {
|
|
405
|
+
nextOffsetY += targetRect.y - visibleRegionArea.bottom + numShiftY;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
const popupLeft = popupRect.x + nextOffsetX;
|
|
410
|
+
const popupRight = popupLeft + popupWidth;
|
|
411
|
+
const popupTop = popupRect.y + nextOffsetY;
|
|
412
|
+
const popupBottom = popupTop + popupHeight;
|
|
413
|
+
const targetLeft = targetRect.x;
|
|
414
|
+
const targetRight = targetLeft + targetWidth;
|
|
415
|
+
const targetTop = targetRect.y;
|
|
416
|
+
const targetBottom = targetTop + targetHeight;
|
|
417
|
+
const maxLeft = Math.max(popupLeft, targetLeft);
|
|
418
|
+
const minRight = Math.min(popupRight, targetRight);
|
|
419
|
+
const xCenter = (maxLeft + minRight) / 2;
|
|
420
|
+
const nextArrowX = xCenter - popupLeft;
|
|
421
|
+
const maxTop = Math.max(popupTop, targetTop);
|
|
422
|
+
const minBottom = Math.min(popupBottom, targetBottom);
|
|
423
|
+
const yCenter = (maxTop + minBottom) / 2;
|
|
424
|
+
const nextArrowY = yCenter - popupTop;
|
|
425
|
+
onPopupAlign?.(popupEle.value, nextAlignInfo);
|
|
426
|
+
let offsetX4Right = popupMirrorRect.right - popupRect.x - (nextOffsetX + popupRect.width);
|
|
427
|
+
let offsetY4Bottom = popupMirrorRect.bottom - popupRect.y - (nextOffsetY + popupRect.height);
|
|
428
|
+
if (scaleX2 === 1) {
|
|
429
|
+
nextOffsetX = Math.round(nextOffsetX);
|
|
430
|
+
offsetX4Right = Math.round(offsetX4Right);
|
|
431
|
+
}
|
|
432
|
+
if (scaleY2 === 1) {
|
|
433
|
+
nextOffsetY = Math.round(nextOffsetY);
|
|
434
|
+
offsetY4Bottom = Math.round(offsetY4Bottom);
|
|
435
|
+
}
|
|
436
|
+
const nextOffsetInfo = {
|
|
437
|
+
ready: true,
|
|
438
|
+
offsetX: nextOffsetX / scaleX2,
|
|
439
|
+
offsetY: nextOffsetY / scaleY2,
|
|
440
|
+
offsetR: offsetX4Right / scaleX2,
|
|
441
|
+
offsetB: offsetY4Bottom / scaleY2,
|
|
442
|
+
arrowX: nextArrowX / scaleX2,
|
|
443
|
+
arrowY: nextArrowY / scaleY2,
|
|
444
|
+
scaleX: scaleX2,
|
|
445
|
+
scaleY: scaleY2,
|
|
446
|
+
align: nextAlignInfo
|
|
447
|
+
};
|
|
448
|
+
Object.assign(offsetInfo, nextOffsetInfo);
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
const onAlign = raf.rafDebounce(_onAlign);
|
|
452
|
+
const triggerAlign = () => {
|
|
453
|
+
alignCountRef.value += 1;
|
|
454
|
+
const id = alignCountRef.value;
|
|
455
|
+
Promise.resolve().then(() => {
|
|
456
|
+
if (alignCountRef.value === id) {
|
|
457
|
+
onAlign();
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
};
|
|
461
|
+
vue.watch(
|
|
462
|
+
popupEle,
|
|
463
|
+
async (ele) => {
|
|
464
|
+
if (ele && open.value && !mobile?.value) {
|
|
465
|
+
await vue.nextTick();
|
|
466
|
+
triggerAlign();
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
);
|
|
470
|
+
const resetReady = () => {
|
|
471
|
+
offsetInfo.ready = false;
|
|
472
|
+
};
|
|
473
|
+
vue.watch(placement, async () => {
|
|
474
|
+
await vue.nextTick();
|
|
475
|
+
resetReady();
|
|
476
|
+
});
|
|
477
|
+
vue.watchEffect(async () => {
|
|
478
|
+
if (!open.value) {
|
|
479
|
+
resetFlipCache();
|
|
480
|
+
await vue.nextTick();
|
|
481
|
+
resetReady();
|
|
482
|
+
}
|
|
483
|
+
});
|
|
484
|
+
const { ready, offsetX, offsetR, offsetY, offsetB, align, arrowY, arrowX, scaleY, scaleX } = vue.toRefs(offsetInfo);
|
|
485
|
+
return [
|
|
486
|
+
ready,
|
|
487
|
+
offsetX,
|
|
488
|
+
offsetY,
|
|
489
|
+
offsetR,
|
|
490
|
+
offsetB,
|
|
491
|
+
arrowX,
|
|
492
|
+
arrowY,
|
|
493
|
+
scaleX,
|
|
494
|
+
scaleY,
|
|
495
|
+
align,
|
|
496
|
+
triggerAlign
|
|
497
|
+
];
|
|
498
|
+
}
|
|
499
|
+
exports.default = useAlign;
|
package/dist/hooks/useAlign.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Ref } from 'vue';
|
|
2
2
|
import { TriggerProps } from '../index.tsx';
|
|
3
3
|
import { AlignType } from '../interface';
|
|
4
|
-
export default function useAlign(open: Ref<boolean>, popupEle: Ref<HTMLElement>, target: Ref<HTMLElement | [x: number, y: number]>, placement: Ref<string>, builtinPlacements: Ref<any>, popupAlign?: Ref<AlignType | undefined>, onPopupAlign?: TriggerProps['onPopupAlign'], mobile?: Ref<boolean | undefined>): readonly [
|
|
4
|
+
export default function useAlign(open: Ref<boolean>, popupEle: Ref<HTMLElement>, target: Ref<HTMLElement | [x: number, y: number]>, placement: Ref<string>, builtinPlacements: Ref<any>, popupAlign?: Ref<AlignType | undefined>, onPopupAlign?: TriggerProps['onPopupAlign'], mobile?: Ref<boolean | undefined>): readonly [Ref<boolean, boolean>, Ref<number, number>, Ref<number, number>, Ref<number, number>, Ref<number, number>, Ref<number, number>, Ref<number, number>, Ref<number, number>, Ref<number, number>, Ref<any, any>, () => void];
|