@rc-component/trigger 1.1.2 → 1.3.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.
- package/es/hooks/useAlign.js +95 -40
- package/es/hooks/useWatch.js +1 -16
- package/es/interface.d.ts +1 -0
- package/es/util.d.ts +1 -1
- package/es/util.js +15 -5
- package/lib/hooks/useAlign.js +94 -39
- package/lib/hooks/useWatch.js +2 -17
- package/lib/interface.d.ts +1 -0
- package/lib/util.d.ts +1 -1
- package/lib/util.js +16 -7
- package/package.json +2 -2
package/es/hooks/useAlign.js
CHANGED
|
@@ -3,7 +3,7 @@ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
|
3
3
|
import useEvent from "rc-util/es/hooks/useEvent";
|
|
4
4
|
import useLayoutEffect from "rc-util/es/hooks/useLayoutEffect";
|
|
5
5
|
import * as React from 'react';
|
|
6
|
-
import { getWin } from "../util";
|
|
6
|
+
import { collectScroller, getWin } from "../util";
|
|
7
7
|
function toNum(num) {
|
|
8
8
|
return Number.isNaN(num) ? 1 : num;
|
|
9
9
|
}
|
|
@@ -68,6 +68,14 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
|
|
|
68
68
|
offsetInfo = _React$useState2[0],
|
|
69
69
|
setOffsetInfo = _React$useState2[1];
|
|
70
70
|
var alignCountRef = React.useRef(0);
|
|
71
|
+
var scrollerList = React.useMemo(function () {
|
|
72
|
+
if (!popupEle) {
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
return collectScroller(popupEle);
|
|
76
|
+
}, [popupEle]);
|
|
77
|
+
|
|
78
|
+
// ========================= Align =========================
|
|
71
79
|
var onAlign = useEvent(function () {
|
|
72
80
|
if (popupEle && target && open) {
|
|
73
81
|
var popupElement = popupEle;
|
|
@@ -81,12 +89,23 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
|
|
|
81
89
|
popupElement.style.top = '0';
|
|
82
90
|
|
|
83
91
|
// Calculate align style, we should consider `transform` case
|
|
84
|
-
var targetRect
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
92
|
+
var targetRect;
|
|
93
|
+
if (Array.isArray(target)) {
|
|
94
|
+
targetRect = {
|
|
95
|
+
x: target[0],
|
|
96
|
+
y: target[1],
|
|
97
|
+
width: 0,
|
|
98
|
+
height: 0
|
|
99
|
+
};
|
|
100
|
+
} else {
|
|
101
|
+
var rect = target.getBoundingClientRect();
|
|
102
|
+
targetRect = {
|
|
103
|
+
x: rect.x,
|
|
104
|
+
y: rect.y,
|
|
105
|
+
width: rect.width,
|
|
106
|
+
height: rect.height
|
|
107
|
+
};
|
|
108
|
+
}
|
|
90
109
|
var popupRect = popupElement.getBoundingClientRect();
|
|
91
110
|
var _win$getComputedStyle = win.getComputedStyle(popupElement),
|
|
92
111
|
width = _win$getComputedStyle.width,
|
|
@@ -97,6 +116,31 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
|
|
|
97
116
|
var popupHeight = popupRect.height;
|
|
98
117
|
var popupWidth = popupRect.width;
|
|
99
118
|
|
|
119
|
+
// Get bounding of visible area
|
|
120
|
+
var visibleArea = {
|
|
121
|
+
left: 0,
|
|
122
|
+
top: 0,
|
|
123
|
+
right: clientWidth,
|
|
124
|
+
bottom: clientHeight
|
|
125
|
+
};
|
|
126
|
+
(scrollerList || []).forEach(function (ele) {
|
|
127
|
+
var eleRect = ele.getBoundingClientRect();
|
|
128
|
+
var eleOutHeight = ele.offsetHeight,
|
|
129
|
+
eleInnerHeight = ele.clientHeight,
|
|
130
|
+
eleOutWidth = ele.offsetWidth,
|
|
131
|
+
eleInnerWidth = ele.clientWidth;
|
|
132
|
+
var scaleX = toNum(Math.round(eleRect.width / eleOutWidth * 1000) / 1000);
|
|
133
|
+
var scaleY = toNum(Math.round(eleRect.height / eleOutHeight * 1000) / 1000);
|
|
134
|
+
var scrollWidth = (eleOutWidth - eleInnerWidth) * scaleX;
|
|
135
|
+
var scrollHeight = (eleOutHeight - eleInnerHeight) * scaleY;
|
|
136
|
+
var eleRight = eleRect.x + eleRect.width - scrollWidth;
|
|
137
|
+
var eleBottom = eleRect.y + eleRect.height - scrollHeight;
|
|
138
|
+
visibleArea.left = Math.max(visibleArea.left, eleRect.left);
|
|
139
|
+
visibleArea.top = Math.max(visibleArea.top, eleRect.top);
|
|
140
|
+
visibleArea.right = Math.min(visibleArea.right, eleRight);
|
|
141
|
+
visibleArea.bottom = Math.min(visibleArea.bottom, eleBottom);
|
|
142
|
+
});
|
|
143
|
+
|
|
100
144
|
// Reset back
|
|
101
145
|
popupElement.style.left = originLeft;
|
|
102
146
|
popupElement.style.top = originTop;
|
|
@@ -107,10 +151,30 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
|
|
|
107
151
|
|
|
108
152
|
// Placement
|
|
109
153
|
var placementInfo = builtinPlacements[placement] || popupAlign || {};
|
|
110
|
-
|
|
154
|
+
|
|
155
|
+
// Offset
|
|
156
|
+
var offset = placementInfo.offset,
|
|
157
|
+
targetOffset = placementInfo.targetOffset;
|
|
158
|
+
var _ref = offset || [],
|
|
111
159
|
_ref2 = _slicedToArray(_ref, 2),
|
|
112
|
-
|
|
113
|
-
|
|
160
|
+
_ref2$ = _ref2[0],
|
|
161
|
+
popupOffsetX = _ref2$ === void 0 ? 0 : _ref2$,
|
|
162
|
+
_ref2$2 = _ref2[1],
|
|
163
|
+
popupOffsetY = _ref2$2 === void 0 ? 0 : _ref2$2;
|
|
164
|
+
var _ref3 = targetOffset || [],
|
|
165
|
+
_ref4 = _slicedToArray(_ref3, 2),
|
|
166
|
+
_ref4$ = _ref4[0],
|
|
167
|
+
targetOffsetX = _ref4$ === void 0 ? 0 : _ref4$,
|
|
168
|
+
_ref4$2 = _ref4[1],
|
|
169
|
+
targetOffsetY = _ref4$2 === void 0 ? 0 : _ref4$2;
|
|
170
|
+
targetRect.x += targetOffsetX;
|
|
171
|
+
targetRect.y += targetOffsetY;
|
|
172
|
+
|
|
173
|
+
// Points
|
|
174
|
+
var _ref5 = placementInfo.points || [],
|
|
175
|
+
_ref6 = _slicedToArray(_ref5, 2),
|
|
176
|
+
popupPoint = _ref6[0],
|
|
177
|
+
targetPoint = _ref6[1];
|
|
114
178
|
var targetPoints = splitPoints(targetPoint);
|
|
115
179
|
var popupPoints = splitPoints(popupPoint);
|
|
116
180
|
var targetAlignPoint = getAlignPoint(targetRect, targetPoints);
|
|
@@ -119,16 +183,7 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
|
|
|
119
183
|
// Real align info may not same as origin one
|
|
120
184
|
var nextAlignInfo = _objectSpread({}, placementInfo);
|
|
121
185
|
|
|
122
|
-
// Offset
|
|
123
|
-
var offset = placementInfo.offset;
|
|
124
|
-
var _ref3 = offset || [],
|
|
125
|
-
_ref4 = _slicedToArray(_ref3, 2),
|
|
126
|
-
_ref4$ = _ref4[0],
|
|
127
|
-
popupOffsetX = _ref4$ === void 0 ? 0 : _ref4$,
|
|
128
|
-
_ref4$2 = _ref4[1],
|
|
129
|
-
popupOffsetY = _ref4$2 === void 0 ? 0 : _ref4$2;
|
|
130
|
-
|
|
131
|
-
// Placement
|
|
186
|
+
// Next Offset
|
|
132
187
|
var nextOffsetX = targetAlignPoint.x - popupAlignPoint.x + popupOffsetX;
|
|
133
188
|
var nextOffsetY = targetAlignPoint.y - popupAlignPoint.y + popupOffsetY;
|
|
134
189
|
|
|
@@ -149,13 +204,13 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
|
|
|
149
204
|
var needAdjustY = adjustY === true || adjustY >= 0;
|
|
150
205
|
|
|
151
206
|
// Bottom to Top
|
|
152
|
-
if (needAdjustY && popupPoints[0] === 't' && nextPopupBottom >
|
|
207
|
+
if (needAdjustY && popupPoints[0] === 't' && nextPopupBottom > visibleArea.bottom) {
|
|
153
208
|
nextOffsetY = targetAlignPointTL.y - popupAlignPointBR.y - popupOffsetY;
|
|
154
209
|
nextAlignInfo.points = [reversePoints(popupPoints, 0), reversePoints(targetPoints, 0)];
|
|
155
210
|
}
|
|
156
211
|
|
|
157
212
|
// Top to Bottom
|
|
158
|
-
if (needAdjustY && popupPoints[0] === 'b' && nextPopupY <
|
|
213
|
+
if (needAdjustY && popupPoints[0] === 'b' && nextPopupY < visibleArea.top) {
|
|
159
214
|
nextOffsetY = targetAlignPointBR.y - popupAlignPointTL.y - popupOffsetY;
|
|
160
215
|
nextAlignInfo.points = [reversePoints(popupPoints, 0), reversePoints(targetPoints, 0)];
|
|
161
216
|
}
|
|
@@ -167,13 +222,13 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
|
|
|
167
222
|
|
|
168
223
|
// >>>>> Flip
|
|
169
224
|
// Right to Left
|
|
170
|
-
if (needAdjustX && popupPoints[1] === 'l' && nextPopupRight >
|
|
225
|
+
if (needAdjustX && popupPoints[1] === 'l' && nextPopupRight > visibleArea.right) {
|
|
171
226
|
nextOffsetX = targetAlignPointTL.x - popupAlignPointBR.x - popupOffsetX;
|
|
172
227
|
nextAlignInfo.points = [reversePoints(popupPoints, 1), reversePoints(targetPoints, 1)];
|
|
173
228
|
}
|
|
174
229
|
|
|
175
230
|
// Left to Right
|
|
176
|
-
if (needAdjustX && popupPoints[1] === 'r' && nextPopupX <
|
|
231
|
+
if (needAdjustX && popupPoints[1] === 'r' && nextPopupX < visibleArea.left) {
|
|
177
232
|
nextOffsetX = targetAlignPointBR.x - popupAlignPointTL.x - popupOffsetX;
|
|
178
233
|
nextAlignInfo.points = [reversePoints(popupPoints, 1), reversePoints(targetPoints, 1)];
|
|
179
234
|
}
|
|
@@ -182,36 +237,36 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
|
|
|
182
237
|
var numShiftX = shiftX === true ? 0 : shiftX;
|
|
183
238
|
if (typeof numShiftX === 'number') {
|
|
184
239
|
// Left
|
|
185
|
-
if (nextPopupX <
|
|
186
|
-
nextOffsetX -= nextPopupX;
|
|
187
|
-
if (targetRect.x + targetRect.width < numShiftX) {
|
|
188
|
-
nextOffsetX += targetRect.x + targetRect.width - numShiftX;
|
|
240
|
+
if (nextPopupX < visibleArea.left) {
|
|
241
|
+
nextOffsetX -= nextPopupX - visibleArea.left;
|
|
242
|
+
if (targetRect.x + targetRect.width < visibleArea.left + numShiftX) {
|
|
243
|
+
nextOffsetX += targetRect.x - visibleArea.left + targetRect.width - numShiftX;
|
|
189
244
|
}
|
|
190
245
|
}
|
|
191
246
|
|
|
192
247
|
// Right
|
|
193
|
-
if (nextPopupRight >
|
|
194
|
-
nextOffsetX -= nextPopupRight -
|
|
195
|
-
if (targetRect.x >
|
|
196
|
-
nextOffsetX += targetRect.x -
|
|
248
|
+
if (nextPopupRight > visibleArea.right) {
|
|
249
|
+
nextOffsetX -= nextPopupRight - visibleArea.right;
|
|
250
|
+
if (targetRect.x > visibleArea.right - numShiftX) {
|
|
251
|
+
nextOffsetX += targetRect.x - visibleArea.right + numShiftX;
|
|
197
252
|
}
|
|
198
253
|
}
|
|
199
254
|
}
|
|
200
255
|
var numShiftY = shiftY === true ? 0 : shiftY;
|
|
201
256
|
if (typeof numShiftY === 'number') {
|
|
202
257
|
// Top
|
|
203
|
-
if (nextPopupY <
|
|
204
|
-
nextOffsetY -= nextPopupY;
|
|
205
|
-
if (targetRect.y + targetRect.height < numShiftY) {
|
|
206
|
-
nextOffsetY += targetRect.y + targetRect.height - numShiftY;
|
|
258
|
+
if (nextPopupY < visibleArea.top) {
|
|
259
|
+
nextOffsetY -= nextPopupY - visibleArea.top;
|
|
260
|
+
if (targetRect.y + targetRect.height < visibleArea.top + numShiftY) {
|
|
261
|
+
nextOffsetY += targetRect.y - visibleArea.top + targetRect.height - numShiftY;
|
|
207
262
|
}
|
|
208
263
|
}
|
|
209
264
|
|
|
210
265
|
// Bottom
|
|
211
|
-
if (nextPopupBottom >
|
|
212
|
-
nextOffsetY -= nextPopupBottom -
|
|
213
|
-
if (targetRect.y >
|
|
214
|
-
nextOffsetY += targetRect.y -
|
|
266
|
+
if (nextPopupBottom > visibleArea.bottom) {
|
|
267
|
+
nextOffsetY -= nextPopupBottom - visibleArea.bottom;
|
|
268
|
+
if (targetRect.y > visibleArea.bottom - numShiftY) {
|
|
269
|
+
nextOffsetY += targetRect.y - visibleArea.bottom + numShiftY;
|
|
215
270
|
}
|
|
216
271
|
}
|
|
217
272
|
}
|
package/es/hooks/useWatch.js
CHANGED
|
@@ -1,21 +1,6 @@
|
|
|
1
1
|
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
2
2
|
import useLayoutEffect from "rc-util/es/hooks/useLayoutEffect";
|
|
3
|
-
import { getWin } from "../util";
|
|
4
|
-
function collectScroller(ele) {
|
|
5
|
-
var scrollerList = [];
|
|
6
|
-
var current = ele === null || ele === void 0 ? void 0 : ele.parentElement;
|
|
7
|
-
var scrollStyle = ['hidden', 'scroll', 'auto'];
|
|
8
|
-
while (current) {
|
|
9
|
-
var _getWin$getComputedSt = getWin(current).getComputedStyle(current),
|
|
10
|
-
overflowX = _getWin$getComputedSt.overflowX,
|
|
11
|
-
overflowY = _getWin$getComputedSt.overflowY;
|
|
12
|
-
if (scrollStyle.includes(overflowX) || scrollStyle.includes(overflowY)) {
|
|
13
|
-
scrollerList.push(current);
|
|
14
|
-
}
|
|
15
|
-
current = current.parentElement;
|
|
16
|
-
}
|
|
17
|
-
return scrollerList;
|
|
18
|
-
}
|
|
3
|
+
import { collectScroller, getWin } from "../util";
|
|
19
4
|
export default function useWatch(open, target, popup, onAlign) {
|
|
20
5
|
useLayoutEffect(function () {
|
|
21
6
|
if (open && target && popup) {
|
package/es/interface.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ export interface AlignType {
|
|
|
20
20
|
* offset target node by offset[0] in x and offset[1] in y.
|
|
21
21
|
* If targetOffset contains percentage string value, it is relative to targetNode region.
|
|
22
22
|
*/
|
|
23
|
+
targetOffset?: number[];
|
|
23
24
|
/**
|
|
24
25
|
* If adjustX field is true, will adjust source node in x direction if source node is invisible.
|
|
25
26
|
* If adjustY field is true, will adjust source node in y direction if source node is invisible.
|
package/es/util.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { CSSMotionProps } from 'rc-motion';
|
|
2
2
|
import type { AlignType, AnimationType, BuildInPlacements, TransitionNameType } from './interface';
|
|
3
|
-
export declare function getAlignFromPlacement(builtinPlacements: BuildInPlacements, placementStr: string, align: AlignType): AlignType;
|
|
4
3
|
export declare function getAlignPopupClassName(builtinPlacements: BuildInPlacements, prefixCls: string, align: AlignType, isAlignPoint: boolean): string;
|
|
5
4
|
/** @deprecated We should not use this if we can refactor all deps */
|
|
6
5
|
export declare function getMotion(prefixCls: string, motion: CSSMotionProps, animation: AnimationType, transitionName: TransitionNameType): CSSMotionProps;
|
|
7
6
|
export declare function getWin(ele: HTMLElement): Window & typeof globalThis;
|
|
7
|
+
export declare function collectScroller(ele: HTMLElement): HTMLElement[];
|
package/es/util.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
1
|
function isPointsEq() {
|
|
3
2
|
var a1 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
4
3
|
var a2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
@@ -8,10 +7,6 @@ function isPointsEq() {
|
|
|
8
7
|
}
|
|
9
8
|
return a1[0] === a2[0] && a1[1] === a2[1];
|
|
10
9
|
}
|
|
11
|
-
export function getAlignFromPlacement(builtinPlacements, placementStr, align) {
|
|
12
|
-
var baseAlign = builtinPlacements[placementStr] || {};
|
|
13
|
-
return _objectSpread(_objectSpread({}, baseAlign), align);
|
|
14
|
-
}
|
|
15
10
|
export function getAlignPopupClassName(builtinPlacements, prefixCls, align, isAlignPoint) {
|
|
16
11
|
var points = align.points;
|
|
17
12
|
var placements = Object.keys(builtinPlacements);
|
|
@@ -44,4 +39,19 @@ export function getMotion(prefixCls, motion, animation, transitionName) {
|
|
|
44
39
|
}
|
|
45
40
|
export function getWin(ele) {
|
|
46
41
|
return ele.ownerDocument.defaultView;
|
|
42
|
+
}
|
|
43
|
+
export function collectScroller(ele) {
|
|
44
|
+
var scrollerList = [];
|
|
45
|
+
var current = ele === null || ele === void 0 ? void 0 : ele.parentElement;
|
|
46
|
+
var scrollStyle = ['hidden', 'scroll', 'auto'];
|
|
47
|
+
while (current) {
|
|
48
|
+
var _getWin$getComputedSt = getWin(current).getComputedStyle(current),
|
|
49
|
+
overflowX = _getWin$getComputedSt.overflowX,
|
|
50
|
+
overflowY = _getWin$getComputedSt.overflowY;
|
|
51
|
+
if (scrollStyle.includes(overflowX) || scrollStyle.includes(overflowY)) {
|
|
52
|
+
scrollerList.push(current);
|
|
53
|
+
}
|
|
54
|
+
current = current.parentElement;
|
|
55
|
+
}
|
|
56
|
+
return scrollerList;
|
|
47
57
|
}
|
package/lib/hooks/useAlign.js
CHANGED
|
@@ -76,6 +76,14 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
|
|
|
76
76
|
offsetInfo = _React$useState2[0],
|
|
77
77
|
setOffsetInfo = _React$useState2[1];
|
|
78
78
|
var alignCountRef = React.useRef(0);
|
|
79
|
+
var scrollerList = React.useMemo(function () {
|
|
80
|
+
if (!popupEle) {
|
|
81
|
+
return [];
|
|
82
|
+
}
|
|
83
|
+
return (0, _util.collectScroller)(popupEle);
|
|
84
|
+
}, [popupEle]);
|
|
85
|
+
|
|
86
|
+
// ========================= Align =========================
|
|
79
87
|
var onAlign = (0, _useEvent.default)(function () {
|
|
80
88
|
if (popupEle && target && open) {
|
|
81
89
|
var popupElement = popupEle;
|
|
@@ -89,12 +97,23 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
|
|
|
89
97
|
popupElement.style.top = '0';
|
|
90
98
|
|
|
91
99
|
// Calculate align style, we should consider `transform` case
|
|
92
|
-
var targetRect
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
100
|
+
var targetRect;
|
|
101
|
+
if (Array.isArray(target)) {
|
|
102
|
+
targetRect = {
|
|
103
|
+
x: target[0],
|
|
104
|
+
y: target[1],
|
|
105
|
+
width: 0,
|
|
106
|
+
height: 0
|
|
107
|
+
};
|
|
108
|
+
} else {
|
|
109
|
+
var rect = target.getBoundingClientRect();
|
|
110
|
+
targetRect = {
|
|
111
|
+
x: rect.x,
|
|
112
|
+
y: rect.y,
|
|
113
|
+
width: rect.width,
|
|
114
|
+
height: rect.height
|
|
115
|
+
};
|
|
116
|
+
}
|
|
98
117
|
var popupRect = popupElement.getBoundingClientRect();
|
|
99
118
|
var _win$getComputedStyle = win.getComputedStyle(popupElement),
|
|
100
119
|
width = _win$getComputedStyle.width,
|
|
@@ -105,6 +124,31 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
|
|
|
105
124
|
var popupHeight = popupRect.height;
|
|
106
125
|
var popupWidth = popupRect.width;
|
|
107
126
|
|
|
127
|
+
// Get bounding of visible area
|
|
128
|
+
var visibleArea = {
|
|
129
|
+
left: 0,
|
|
130
|
+
top: 0,
|
|
131
|
+
right: clientWidth,
|
|
132
|
+
bottom: clientHeight
|
|
133
|
+
};
|
|
134
|
+
(scrollerList || []).forEach(function (ele) {
|
|
135
|
+
var eleRect = ele.getBoundingClientRect();
|
|
136
|
+
var eleOutHeight = ele.offsetHeight,
|
|
137
|
+
eleInnerHeight = ele.clientHeight,
|
|
138
|
+
eleOutWidth = ele.offsetWidth,
|
|
139
|
+
eleInnerWidth = ele.clientWidth;
|
|
140
|
+
var scaleX = toNum(Math.round(eleRect.width / eleOutWidth * 1000) / 1000);
|
|
141
|
+
var scaleY = toNum(Math.round(eleRect.height / eleOutHeight * 1000) / 1000);
|
|
142
|
+
var scrollWidth = (eleOutWidth - eleInnerWidth) * scaleX;
|
|
143
|
+
var scrollHeight = (eleOutHeight - eleInnerHeight) * scaleY;
|
|
144
|
+
var eleRight = eleRect.x + eleRect.width - scrollWidth;
|
|
145
|
+
var eleBottom = eleRect.y + eleRect.height - scrollHeight;
|
|
146
|
+
visibleArea.left = Math.max(visibleArea.left, eleRect.left);
|
|
147
|
+
visibleArea.top = Math.max(visibleArea.top, eleRect.top);
|
|
148
|
+
visibleArea.right = Math.min(visibleArea.right, eleRight);
|
|
149
|
+
visibleArea.bottom = Math.min(visibleArea.bottom, eleBottom);
|
|
150
|
+
});
|
|
151
|
+
|
|
108
152
|
// Reset back
|
|
109
153
|
popupElement.style.left = originLeft;
|
|
110
154
|
popupElement.style.top = originTop;
|
|
@@ -115,10 +159,30 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
|
|
|
115
159
|
|
|
116
160
|
// Placement
|
|
117
161
|
var placementInfo = builtinPlacements[placement] || popupAlign || {};
|
|
118
|
-
|
|
162
|
+
|
|
163
|
+
// Offset
|
|
164
|
+
var offset = placementInfo.offset,
|
|
165
|
+
targetOffset = placementInfo.targetOffset;
|
|
166
|
+
var _ref = offset || [],
|
|
119
167
|
_ref2 = (0, _slicedToArray2.default)(_ref, 2),
|
|
120
|
-
|
|
121
|
-
|
|
168
|
+
_ref2$ = _ref2[0],
|
|
169
|
+
popupOffsetX = _ref2$ === void 0 ? 0 : _ref2$,
|
|
170
|
+
_ref2$2 = _ref2[1],
|
|
171
|
+
popupOffsetY = _ref2$2 === void 0 ? 0 : _ref2$2;
|
|
172
|
+
var _ref3 = targetOffset || [],
|
|
173
|
+
_ref4 = (0, _slicedToArray2.default)(_ref3, 2),
|
|
174
|
+
_ref4$ = _ref4[0],
|
|
175
|
+
targetOffsetX = _ref4$ === void 0 ? 0 : _ref4$,
|
|
176
|
+
_ref4$2 = _ref4[1],
|
|
177
|
+
targetOffsetY = _ref4$2 === void 0 ? 0 : _ref4$2;
|
|
178
|
+
targetRect.x += targetOffsetX;
|
|
179
|
+
targetRect.y += targetOffsetY;
|
|
180
|
+
|
|
181
|
+
// Points
|
|
182
|
+
var _ref5 = placementInfo.points || [],
|
|
183
|
+
_ref6 = (0, _slicedToArray2.default)(_ref5, 2),
|
|
184
|
+
popupPoint = _ref6[0],
|
|
185
|
+
targetPoint = _ref6[1];
|
|
122
186
|
var targetPoints = splitPoints(targetPoint);
|
|
123
187
|
var popupPoints = splitPoints(popupPoint);
|
|
124
188
|
var targetAlignPoint = getAlignPoint(targetRect, targetPoints);
|
|
@@ -127,16 +191,7 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
|
|
|
127
191
|
// Real align info may not same as origin one
|
|
128
192
|
var nextAlignInfo = (0, _objectSpread2.default)({}, placementInfo);
|
|
129
193
|
|
|
130
|
-
// Offset
|
|
131
|
-
var offset = placementInfo.offset;
|
|
132
|
-
var _ref3 = offset || [],
|
|
133
|
-
_ref4 = (0, _slicedToArray2.default)(_ref3, 2),
|
|
134
|
-
_ref4$ = _ref4[0],
|
|
135
|
-
popupOffsetX = _ref4$ === void 0 ? 0 : _ref4$,
|
|
136
|
-
_ref4$2 = _ref4[1],
|
|
137
|
-
popupOffsetY = _ref4$2 === void 0 ? 0 : _ref4$2;
|
|
138
|
-
|
|
139
|
-
// Placement
|
|
194
|
+
// Next Offset
|
|
140
195
|
var nextOffsetX = targetAlignPoint.x - popupAlignPoint.x + popupOffsetX;
|
|
141
196
|
var nextOffsetY = targetAlignPoint.y - popupAlignPoint.y + popupOffsetY;
|
|
142
197
|
|
|
@@ -157,13 +212,13 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
|
|
|
157
212
|
var needAdjustY = adjustY === true || adjustY >= 0;
|
|
158
213
|
|
|
159
214
|
// Bottom to Top
|
|
160
|
-
if (needAdjustY && popupPoints[0] === 't' && nextPopupBottom >
|
|
215
|
+
if (needAdjustY && popupPoints[0] === 't' && nextPopupBottom > visibleArea.bottom) {
|
|
161
216
|
nextOffsetY = targetAlignPointTL.y - popupAlignPointBR.y - popupOffsetY;
|
|
162
217
|
nextAlignInfo.points = [reversePoints(popupPoints, 0), reversePoints(targetPoints, 0)];
|
|
163
218
|
}
|
|
164
219
|
|
|
165
220
|
// Top to Bottom
|
|
166
|
-
if (needAdjustY && popupPoints[0] === 'b' && nextPopupY <
|
|
221
|
+
if (needAdjustY && popupPoints[0] === 'b' && nextPopupY < visibleArea.top) {
|
|
167
222
|
nextOffsetY = targetAlignPointBR.y - popupAlignPointTL.y - popupOffsetY;
|
|
168
223
|
nextAlignInfo.points = [reversePoints(popupPoints, 0), reversePoints(targetPoints, 0)];
|
|
169
224
|
}
|
|
@@ -175,13 +230,13 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
|
|
|
175
230
|
|
|
176
231
|
// >>>>> Flip
|
|
177
232
|
// Right to Left
|
|
178
|
-
if (needAdjustX && popupPoints[1] === 'l' && nextPopupRight >
|
|
233
|
+
if (needAdjustX && popupPoints[1] === 'l' && nextPopupRight > visibleArea.right) {
|
|
179
234
|
nextOffsetX = targetAlignPointTL.x - popupAlignPointBR.x - popupOffsetX;
|
|
180
235
|
nextAlignInfo.points = [reversePoints(popupPoints, 1), reversePoints(targetPoints, 1)];
|
|
181
236
|
}
|
|
182
237
|
|
|
183
238
|
// Left to Right
|
|
184
|
-
if (needAdjustX && popupPoints[1] === 'r' && nextPopupX <
|
|
239
|
+
if (needAdjustX && popupPoints[1] === 'r' && nextPopupX < visibleArea.left) {
|
|
185
240
|
nextOffsetX = targetAlignPointBR.x - popupAlignPointTL.x - popupOffsetX;
|
|
186
241
|
nextAlignInfo.points = [reversePoints(popupPoints, 1), reversePoints(targetPoints, 1)];
|
|
187
242
|
}
|
|
@@ -190,36 +245,36 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
|
|
|
190
245
|
var numShiftX = shiftX === true ? 0 : shiftX;
|
|
191
246
|
if (typeof numShiftX === 'number') {
|
|
192
247
|
// Left
|
|
193
|
-
if (nextPopupX <
|
|
194
|
-
nextOffsetX -= nextPopupX;
|
|
195
|
-
if (targetRect.x + targetRect.width < numShiftX) {
|
|
196
|
-
nextOffsetX += targetRect.x + targetRect.width - numShiftX;
|
|
248
|
+
if (nextPopupX < visibleArea.left) {
|
|
249
|
+
nextOffsetX -= nextPopupX - visibleArea.left;
|
|
250
|
+
if (targetRect.x + targetRect.width < visibleArea.left + numShiftX) {
|
|
251
|
+
nextOffsetX += targetRect.x - visibleArea.left + targetRect.width - numShiftX;
|
|
197
252
|
}
|
|
198
253
|
}
|
|
199
254
|
|
|
200
255
|
// Right
|
|
201
|
-
if (nextPopupRight >
|
|
202
|
-
nextOffsetX -= nextPopupRight -
|
|
203
|
-
if (targetRect.x >
|
|
204
|
-
nextOffsetX += targetRect.x -
|
|
256
|
+
if (nextPopupRight > visibleArea.right) {
|
|
257
|
+
nextOffsetX -= nextPopupRight - visibleArea.right;
|
|
258
|
+
if (targetRect.x > visibleArea.right - numShiftX) {
|
|
259
|
+
nextOffsetX += targetRect.x - visibleArea.right + numShiftX;
|
|
205
260
|
}
|
|
206
261
|
}
|
|
207
262
|
}
|
|
208
263
|
var numShiftY = shiftY === true ? 0 : shiftY;
|
|
209
264
|
if (typeof numShiftY === 'number') {
|
|
210
265
|
// Top
|
|
211
|
-
if (nextPopupY <
|
|
212
|
-
nextOffsetY -= nextPopupY;
|
|
213
|
-
if (targetRect.y + targetRect.height < numShiftY) {
|
|
214
|
-
nextOffsetY += targetRect.y + targetRect.height - numShiftY;
|
|
266
|
+
if (nextPopupY < visibleArea.top) {
|
|
267
|
+
nextOffsetY -= nextPopupY - visibleArea.top;
|
|
268
|
+
if (targetRect.y + targetRect.height < visibleArea.top + numShiftY) {
|
|
269
|
+
nextOffsetY += targetRect.y - visibleArea.top + targetRect.height - numShiftY;
|
|
215
270
|
}
|
|
216
271
|
}
|
|
217
272
|
|
|
218
273
|
// Bottom
|
|
219
|
-
if (nextPopupBottom >
|
|
220
|
-
nextOffsetY -= nextPopupBottom -
|
|
221
|
-
if (targetRect.y >
|
|
222
|
-
nextOffsetY += targetRect.y -
|
|
274
|
+
if (nextPopupBottom > visibleArea.bottom) {
|
|
275
|
+
nextOffsetY -= nextPopupBottom - visibleArea.bottom;
|
|
276
|
+
if (targetRect.y > visibleArea.bottom - numShiftY) {
|
|
277
|
+
nextOffsetY += targetRect.y - visibleArea.bottom + numShiftY;
|
|
223
278
|
}
|
|
224
279
|
}
|
|
225
280
|
}
|
package/lib/hooks/useWatch.js
CHANGED
|
@@ -8,28 +8,13 @@ exports.default = useWatch;
|
|
|
8
8
|
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
9
9
|
var _useLayoutEffect = _interopRequireDefault(require("rc-util/lib/hooks/useLayoutEffect"));
|
|
10
10
|
var _util = require("../util");
|
|
11
|
-
function collectScroller(ele) {
|
|
12
|
-
var scrollerList = [];
|
|
13
|
-
var current = ele === null || ele === void 0 ? void 0 : ele.parentElement;
|
|
14
|
-
var scrollStyle = ['hidden', 'scroll', 'auto'];
|
|
15
|
-
while (current) {
|
|
16
|
-
var _getWin$getComputedSt = (0, _util.getWin)(current).getComputedStyle(current),
|
|
17
|
-
overflowX = _getWin$getComputedSt.overflowX,
|
|
18
|
-
overflowY = _getWin$getComputedSt.overflowY;
|
|
19
|
-
if (scrollStyle.includes(overflowX) || scrollStyle.includes(overflowY)) {
|
|
20
|
-
scrollerList.push(current);
|
|
21
|
-
}
|
|
22
|
-
current = current.parentElement;
|
|
23
|
-
}
|
|
24
|
-
return scrollerList;
|
|
25
|
-
}
|
|
26
11
|
function useWatch(open, target, popup, onAlign) {
|
|
27
12
|
(0, _useLayoutEffect.default)(function () {
|
|
28
13
|
if (open && target && popup) {
|
|
29
14
|
var targetElement = target;
|
|
30
15
|
var popupElement = popup;
|
|
31
|
-
var targetScrollList = collectScroller(targetElement);
|
|
32
|
-
var popupScrollList = collectScroller(popupElement);
|
|
16
|
+
var targetScrollList = (0, _util.collectScroller)(targetElement);
|
|
17
|
+
var popupScrollList = (0, _util.collectScroller)(popupElement);
|
|
33
18
|
var win = (0, _util.getWin)(popupElement);
|
|
34
19
|
var mergedList = new Set([win].concat((0, _toConsumableArray2.default)(targetScrollList), (0, _toConsumableArray2.default)(popupScrollList)));
|
|
35
20
|
function notifyScroll() {
|
package/lib/interface.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ export interface AlignType {
|
|
|
20
20
|
* offset target node by offset[0] in x and offset[1] in y.
|
|
21
21
|
* If targetOffset contains percentage string value, it is relative to targetNode region.
|
|
22
22
|
*/
|
|
23
|
+
targetOffset?: number[];
|
|
23
24
|
/**
|
|
24
25
|
* If adjustX field is true, will adjust source node in x direction if source node is invisible.
|
|
25
26
|
* If adjustY field is true, will adjust source node in y direction if source node is invisible.
|
package/lib/util.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { CSSMotionProps } from 'rc-motion';
|
|
2
2
|
import type { AlignType, AnimationType, BuildInPlacements, TransitionNameType } from './interface';
|
|
3
|
-
export declare function getAlignFromPlacement(builtinPlacements: BuildInPlacements, placementStr: string, align: AlignType): AlignType;
|
|
4
3
|
export declare function getAlignPopupClassName(builtinPlacements: BuildInPlacements, prefixCls: string, align: AlignType, isAlignPoint: boolean): string;
|
|
5
4
|
/** @deprecated We should not use this if we can refactor all deps */
|
|
6
5
|
export declare function getMotion(prefixCls: string, motion: CSSMotionProps, animation: AnimationType, transitionName: TransitionNameType): CSSMotionProps;
|
|
7
6
|
export declare function getWin(ele: HTMLElement): Window & typeof globalThis;
|
|
7
|
+
export declare function collectScroller(ele: HTMLElement): HTMLElement[];
|
package/lib/util.js
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
3
|
Object.defineProperty(exports, "__esModule", {
|
|
5
4
|
value: true
|
|
6
5
|
});
|
|
7
|
-
exports.
|
|
6
|
+
exports.collectScroller = collectScroller;
|
|
8
7
|
exports.getAlignPopupClassName = getAlignPopupClassName;
|
|
9
8
|
exports.getMotion = getMotion;
|
|
10
9
|
exports.getWin = getWin;
|
|
11
|
-
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
12
10
|
function isPointsEq() {
|
|
13
11
|
var a1 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
14
12
|
var a2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
@@ -18,10 +16,6 @@ function isPointsEq() {
|
|
|
18
16
|
}
|
|
19
17
|
return a1[0] === a2[0] && a1[1] === a2[1];
|
|
20
18
|
}
|
|
21
|
-
function getAlignFromPlacement(builtinPlacements, placementStr, align) {
|
|
22
|
-
var baseAlign = builtinPlacements[placementStr] || {};
|
|
23
|
-
return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, baseAlign), align);
|
|
24
|
-
}
|
|
25
19
|
function getAlignPopupClassName(builtinPlacements, prefixCls, align, isAlignPoint) {
|
|
26
20
|
var points = align.points;
|
|
27
21
|
var placements = Object.keys(builtinPlacements);
|
|
@@ -54,4 +48,19 @@ function getMotion(prefixCls, motion, animation, transitionName) {
|
|
|
54
48
|
}
|
|
55
49
|
function getWin(ele) {
|
|
56
50
|
return ele.ownerDocument.defaultView;
|
|
51
|
+
}
|
|
52
|
+
function collectScroller(ele) {
|
|
53
|
+
var scrollerList = [];
|
|
54
|
+
var current = ele === null || ele === void 0 ? void 0 : ele.parentElement;
|
|
55
|
+
var scrollStyle = ['hidden', 'scroll', 'auto'];
|
|
56
|
+
while (current) {
|
|
57
|
+
var _getWin$getComputedSt = getWin(current).getComputedStyle(current),
|
|
58
|
+
overflowX = _getWin$getComputedSt.overflowX,
|
|
59
|
+
overflowY = _getWin$getComputedSt.overflowY;
|
|
60
|
+
if (scrollStyle.includes(overflowX) || scrollStyle.includes(overflowY)) {
|
|
61
|
+
scrollerList.push(current);
|
|
62
|
+
}
|
|
63
|
+
current = current.parentElement;
|
|
64
|
+
}
|
|
65
|
+
return scrollerList;
|
|
57
66
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rc-component/trigger",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "base abstract trigger component for react",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=8.x"
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
"classnames": "^2.3.2",
|
|
66
66
|
"rc-align": "^4.0.0",
|
|
67
67
|
"rc-motion": "^2.0.0",
|
|
68
|
-
"rc-resize-observer": "^1.3.
|
|
68
|
+
"rc-resize-observer": "^1.3.1",
|
|
69
69
|
"rc-util": "^5.27.1"
|
|
70
70
|
},
|
|
71
71
|
"peerDependencies": {
|