@rc-component/trigger 1.7.1 → 1.7.3

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.
@@ -5,10 +5,7 @@ import isVisible from "rc-util/es/Dom/isVisible";
5
5
  import useEvent from "rc-util/es/hooks/useEvent";
6
6
  import useLayoutEffect from "rc-util/es/hooks/useLayoutEffect";
7
7
  import * as React from 'react';
8
- import { collectScroller, getWin } from "../util";
9
- function toNum(num) {
10
- return Number.isNaN(num) ? 1 : num;
11
- }
8
+ import { collectScroller, getVisibleArea, getWin, toNum } from "../util";
12
9
  function splitPoints() {
13
10
  var points = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
14
11
  return [points[0], points[1]];
@@ -141,26 +138,7 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
141
138
  right: clientWidth,
142
139
  bottom: clientHeight
143
140
  };
144
- (scrollerList || []).forEach(function (ele) {
145
- if (ele instanceof HTMLBodyElement) {
146
- return;
147
- }
148
- var eleRect = ele.getBoundingClientRect();
149
- var eleOutHeight = ele.offsetHeight,
150
- eleInnerHeight = ele.clientHeight,
151
- eleOutWidth = ele.offsetWidth,
152
- eleInnerWidth = ele.clientWidth;
153
- var scaleX = toNum(Math.round(eleRect.width / eleOutWidth * 1000) / 1000);
154
- var scaleY = toNum(Math.round(eleRect.height / eleOutHeight * 1000) / 1000);
155
- var eleScrollWidth = (eleOutWidth - eleInnerWidth) * scaleX;
156
- var eleScrollHeight = (eleOutHeight - eleInnerHeight) * scaleY;
157
- var eleRight = eleRect.x + eleRect.width - eleScrollWidth;
158
- var eleBottom = eleRect.y + eleRect.height - eleScrollHeight;
159
- visibleArea.left = Math.max(visibleArea.left, eleRect.left);
160
- visibleArea.top = Math.max(visibleArea.top, eleRect.top);
161
- visibleArea.right = Math.min(visibleArea.right, eleRight);
162
- visibleArea.bottom = Math.min(visibleArea.bottom, eleBottom);
163
- });
141
+ visibleArea = getVisibleArea(visibleArea, scrollerList);
164
142
 
165
143
  // Reset back
166
144
  popupElement.style.left = originLeft;
@@ -221,7 +199,7 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
221
199
  var visibleT = Math.max(t, visibleArea.top);
222
200
  var visibleR = Math.min(r, visibleArea.right);
223
201
  var visibleB = Math.min(b, visibleArea.bottom);
224
- return (visibleR - visibleL) * (visibleB - visibleT);
202
+ return Math.max(0, (visibleR - visibleL) * (visibleB - visibleT));
225
203
  }
226
204
  var originIntersectionVisibleArea = getIntersectionVisibleArea(nextOffsetX, nextOffsetY);
227
205
 
@@ -267,7 +245,7 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
267
245
  } else {
268
246
  tmpNextOffsetY = targetAlignPointTL.y - popupAlignPointBR.y - popupOffsetY;
269
247
  }
270
- if (getIntersectionVisibleArea(nextOffsetX, tmpNextOffsetY) > originIntersectionVisibleArea) {
248
+ if (getIntersectionVisibleArea(nextOffsetX, tmpNextOffsetY) >= originIntersectionVisibleArea) {
271
249
  nextOffsetY = tmpNextOffsetY;
272
250
  nextAlignInfo.points = [reversePoints(popupPoints, 0), reversePoints(targetPoints, 0)];
273
251
  }
@@ -281,7 +259,7 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
281
259
  } else {
282
260
  _tmpNextOffsetY = targetAlignPointBR.y - popupAlignPointTL.y - popupOffsetY;
283
261
  }
284
- if (getIntersectionVisibleArea(nextOffsetX, _tmpNextOffsetY) > originIntersectionVisibleArea) {
262
+ if (getIntersectionVisibleArea(nextOffsetX, _tmpNextOffsetY) >= originIntersectionVisibleArea) {
285
263
  nextOffsetY = _tmpNextOffsetY;
286
264
  nextAlignInfo.points = [reversePoints(popupPoints, 0), reversePoints(targetPoints, 0)];
287
265
  }
@@ -301,7 +279,7 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
301
279
  } else {
302
280
  tmpNextOffsetX = targetAlignPointTL.x - popupAlignPointBR.x - popupOffsetX;
303
281
  }
304
- if (getIntersectionVisibleArea(tmpNextOffsetX, nextOffsetY) > originIntersectionVisibleArea) {
282
+ if (getIntersectionVisibleArea(tmpNextOffsetX, nextOffsetY) >= originIntersectionVisibleArea) {
305
283
  nextOffsetX = tmpNextOffsetX;
306
284
  nextAlignInfo.points = [reversePoints(popupPoints, 1), reversePoints(targetPoints, 1)];
307
285
  }
@@ -315,7 +293,7 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
315
293
  } else {
316
294
  _tmpNextOffsetX = targetAlignPointBR.x - popupAlignPointTL.x - popupOffsetX;
317
295
  }
318
- if (getIntersectionVisibleArea(_tmpNextOffsetX, nextOffsetY) > originIntersectionVisibleArea) {
296
+ if (getIntersectionVisibleArea(_tmpNextOffsetX, nextOffsetY) >= originIntersectionVisibleArea) {
319
297
  nextOffsetX = _tmpNextOffsetX;
320
298
  nextAlignInfo.points = [reversePoints(popupPoints, 1), reversePoints(targetPoints, 1)];
321
299
  }
package/es/util.d.ts CHANGED
@@ -4,4 +4,22 @@ export declare function getAlignPopupClassName(builtinPlacements: BuildInPlaceme
4
4
  /** @deprecated We should not use this if we can refactor all deps */
5
5
  export declare function getMotion(prefixCls: string, motion: CSSMotionProps, animation: AnimationType, transitionName: TransitionNameType): CSSMotionProps;
6
6
  export declare function getWin(ele: HTMLElement): Window & typeof globalThis;
7
+ /**
8
+ * Get all the scrollable parent elements of the element
9
+ * @param ele The element to be detected
10
+ * @param areaOnly Only return the parent which will cut visible area
11
+ */
7
12
  export declare function collectScroller(ele: HTMLElement): HTMLElement[];
13
+ export declare function toNum(num: number): number;
14
+ export interface VisibleArea {
15
+ left: number;
16
+ top: number;
17
+ right: number;
18
+ bottom: number;
19
+ }
20
+ export declare function getVisibleArea(initArea: VisibleArea, scrollerList?: HTMLElement[]): {
21
+ left: number;
22
+ top: number;
23
+ right: number;
24
+ bottom: number;
25
+ };
package/es/util.js CHANGED
@@ -1,3 +1,4 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
1
2
  function isPointsEq() {
2
3
  var a1 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
3
4
  var a2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
@@ -40,6 +41,12 @@ export function getMotion(prefixCls, motion, animation, transitionName) {
40
41
  export function getWin(ele) {
41
42
  return ele.ownerDocument.defaultView;
42
43
  }
44
+
45
+ /**
46
+ * Get all the scrollable parent elements of the element
47
+ * @param ele The element to be detected
48
+ * @param areaOnly Only return the parent which will cut visible area
49
+ */
43
50
  export function collectScroller(ele) {
44
51
  var scrollerList = [];
45
52
  var current = ele === null || ele === void 0 ? void 0 : ele.parentElement;
@@ -54,4 +61,38 @@ export function collectScroller(ele) {
54
61
  current = current.parentElement;
55
62
  }
56
63
  return scrollerList;
64
+ }
65
+ export function toNum(num) {
66
+ return Number.isNaN(num) ? 1 : num;
67
+ }
68
+ export function getVisibleArea(initArea, scrollerList) {
69
+ var visibleArea = _objectSpread({}, initArea);
70
+ (scrollerList || []).forEach(function (ele) {
71
+ if (ele instanceof HTMLBodyElement) {
72
+ return;
73
+ }
74
+
75
+ // Skip if static position which will not affect visible area
76
+ var _getWin$getComputedSt2 = getWin(ele).getComputedStyle(ele),
77
+ position = _getWin$getComputedSt2.position;
78
+ if (position === 'static') {
79
+ return;
80
+ }
81
+ var eleRect = ele.getBoundingClientRect();
82
+ var eleOutHeight = ele.offsetHeight,
83
+ eleInnerHeight = ele.clientHeight,
84
+ eleOutWidth = ele.offsetWidth,
85
+ eleInnerWidth = ele.clientWidth;
86
+ var scaleX = toNum(Math.round(eleRect.width / eleOutWidth * 1000) / 1000);
87
+ var scaleY = toNum(Math.round(eleRect.height / eleOutHeight * 1000) / 1000);
88
+ var eleScrollWidth = (eleOutWidth - eleInnerWidth) * scaleX;
89
+ var eleScrollHeight = (eleOutHeight - eleInnerHeight) * scaleY;
90
+ var eleRight = eleRect.x + eleRect.width - eleScrollWidth;
91
+ var eleBottom = eleRect.y + eleRect.height - eleScrollHeight;
92
+ visibleArea.left = Math.max(visibleArea.left, eleRect.x);
93
+ visibleArea.top = Math.max(visibleArea.top, eleRect.y);
94
+ visibleArea.right = Math.min(visibleArea.right, eleRight);
95
+ visibleArea.bottom = Math.min(visibleArea.bottom, eleBottom);
96
+ });
97
+ return visibleArea;
57
98
  }
@@ -14,9 +14,6 @@ var _useEvent = _interopRequireDefault(require("rc-util/lib/hooks/useEvent"));
14
14
  var _useLayoutEffect = _interopRequireDefault(require("rc-util/lib/hooks/useLayoutEffect"));
15
15
  var React = _interopRequireWildcard(require("react"));
16
16
  var _util = require("../util");
17
- function toNum(num) {
18
- return Number.isNaN(num) ? 1 : num;
19
- }
20
17
  function splitPoints() {
21
18
  var points = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
22
19
  return [points[0], points[1]];
@@ -149,34 +146,15 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
149
146
  right: clientWidth,
150
147
  bottom: clientHeight
151
148
  };
152
- (scrollerList || []).forEach(function (ele) {
153
- if (ele instanceof HTMLBodyElement) {
154
- return;
155
- }
156
- var eleRect = ele.getBoundingClientRect();
157
- var eleOutHeight = ele.offsetHeight,
158
- eleInnerHeight = ele.clientHeight,
159
- eleOutWidth = ele.offsetWidth,
160
- eleInnerWidth = ele.clientWidth;
161
- var scaleX = toNum(Math.round(eleRect.width / eleOutWidth * 1000) / 1000);
162
- var scaleY = toNum(Math.round(eleRect.height / eleOutHeight * 1000) / 1000);
163
- var eleScrollWidth = (eleOutWidth - eleInnerWidth) * scaleX;
164
- var eleScrollHeight = (eleOutHeight - eleInnerHeight) * scaleY;
165
- var eleRight = eleRect.x + eleRect.width - eleScrollWidth;
166
- var eleBottom = eleRect.y + eleRect.height - eleScrollHeight;
167
- visibleArea.left = Math.max(visibleArea.left, eleRect.left);
168
- visibleArea.top = Math.max(visibleArea.top, eleRect.top);
169
- visibleArea.right = Math.min(visibleArea.right, eleRight);
170
- visibleArea.bottom = Math.min(visibleArea.bottom, eleBottom);
171
- });
149
+ visibleArea = (0, _util.getVisibleArea)(visibleArea, scrollerList);
172
150
 
173
151
  // Reset back
174
152
  popupElement.style.left = originLeft;
175
153
  popupElement.style.top = originTop;
176
154
 
177
155
  // Calculate scale
178
- var _scaleX = toNum(Math.round(popupWidth / parseFloat(width) * 1000) / 1000);
179
- var _scaleY = toNum(Math.round(popupHeight / parseFloat(height) * 1000) / 1000);
156
+ var _scaleX = (0, _util.toNum)(Math.round(popupWidth / parseFloat(width) * 1000) / 1000);
157
+ var _scaleY = (0, _util.toNum)(Math.round(popupHeight / parseFloat(height) * 1000) / 1000);
180
158
 
181
159
  // No need to align since it's not visible in view
182
160
  if (_scaleX === 0 || _scaleY === 0 || (0, _findDOMNode.isDOM)(target) && !(0, _isVisible.default)(target)) {
@@ -229,7 +207,7 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
229
207
  var visibleT = Math.max(t, visibleArea.top);
230
208
  var visibleR = Math.min(r, visibleArea.right);
231
209
  var visibleB = Math.min(b, visibleArea.bottom);
232
- return (visibleR - visibleL) * (visibleB - visibleT);
210
+ return Math.max(0, (visibleR - visibleL) * (visibleB - visibleT));
233
211
  }
234
212
  var originIntersectionVisibleArea = getIntersectionVisibleArea(nextOffsetX, nextOffsetY);
235
213
 
@@ -275,7 +253,7 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
275
253
  } else {
276
254
  tmpNextOffsetY = targetAlignPointTL.y - popupAlignPointBR.y - popupOffsetY;
277
255
  }
278
- if (getIntersectionVisibleArea(nextOffsetX, tmpNextOffsetY) > originIntersectionVisibleArea) {
256
+ if (getIntersectionVisibleArea(nextOffsetX, tmpNextOffsetY) >= originIntersectionVisibleArea) {
279
257
  nextOffsetY = tmpNextOffsetY;
280
258
  nextAlignInfo.points = [reversePoints(popupPoints, 0), reversePoints(targetPoints, 0)];
281
259
  }
@@ -289,7 +267,7 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
289
267
  } else {
290
268
  _tmpNextOffsetY = targetAlignPointBR.y - popupAlignPointTL.y - popupOffsetY;
291
269
  }
292
- if (getIntersectionVisibleArea(nextOffsetX, _tmpNextOffsetY) > originIntersectionVisibleArea) {
270
+ if (getIntersectionVisibleArea(nextOffsetX, _tmpNextOffsetY) >= originIntersectionVisibleArea) {
293
271
  nextOffsetY = _tmpNextOffsetY;
294
272
  nextAlignInfo.points = [reversePoints(popupPoints, 0), reversePoints(targetPoints, 0)];
295
273
  }
@@ -309,7 +287,7 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
309
287
  } else {
310
288
  tmpNextOffsetX = targetAlignPointTL.x - popupAlignPointBR.x - popupOffsetX;
311
289
  }
312
- if (getIntersectionVisibleArea(tmpNextOffsetX, nextOffsetY) > originIntersectionVisibleArea) {
290
+ if (getIntersectionVisibleArea(tmpNextOffsetX, nextOffsetY) >= originIntersectionVisibleArea) {
313
291
  nextOffsetX = tmpNextOffsetX;
314
292
  nextAlignInfo.points = [reversePoints(popupPoints, 1), reversePoints(targetPoints, 1)];
315
293
  }
@@ -323,7 +301,7 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
323
301
  } else {
324
302
  _tmpNextOffsetX = targetAlignPointBR.x - popupAlignPointTL.x - popupOffsetX;
325
303
  }
326
- if (getIntersectionVisibleArea(_tmpNextOffsetX, nextOffsetY) > originIntersectionVisibleArea) {
304
+ if (getIntersectionVisibleArea(_tmpNextOffsetX, nextOffsetY) >= originIntersectionVisibleArea) {
327
305
  nextOffsetX = _tmpNextOffsetX;
328
306
  nextAlignInfo.points = [reversePoints(popupPoints, 1), reversePoints(targetPoints, 1)];
329
307
  }
package/lib/util.d.ts CHANGED
@@ -4,4 +4,22 @@ export declare function getAlignPopupClassName(builtinPlacements: BuildInPlaceme
4
4
  /** @deprecated We should not use this if we can refactor all deps */
5
5
  export declare function getMotion(prefixCls: string, motion: CSSMotionProps, animation: AnimationType, transitionName: TransitionNameType): CSSMotionProps;
6
6
  export declare function getWin(ele: HTMLElement): Window & typeof globalThis;
7
+ /**
8
+ * Get all the scrollable parent elements of the element
9
+ * @param ele The element to be detected
10
+ * @param areaOnly Only return the parent which will cut visible area
11
+ */
7
12
  export declare function collectScroller(ele: HTMLElement): HTMLElement[];
13
+ export declare function toNum(num: number): number;
14
+ export interface VisibleArea {
15
+ left: number;
16
+ top: number;
17
+ right: number;
18
+ bottom: number;
19
+ }
20
+ export declare function getVisibleArea(initArea: VisibleArea, scrollerList?: HTMLElement[]): {
21
+ left: number;
22
+ top: number;
23
+ right: number;
24
+ bottom: number;
25
+ };
package/lib/util.js CHANGED
@@ -1,12 +1,16 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
3
4
  Object.defineProperty(exports, "__esModule", {
4
5
  value: true
5
6
  });
6
7
  exports.collectScroller = collectScroller;
7
8
  exports.getAlignPopupClassName = getAlignPopupClassName;
8
9
  exports.getMotion = getMotion;
10
+ exports.getVisibleArea = getVisibleArea;
9
11
  exports.getWin = getWin;
12
+ exports.toNum = toNum;
13
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
10
14
  function isPointsEq() {
11
15
  var a1 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
12
16
  var a2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
@@ -49,6 +53,12 @@ function getMotion(prefixCls, motion, animation, transitionName) {
49
53
  function getWin(ele) {
50
54
  return ele.ownerDocument.defaultView;
51
55
  }
56
+
57
+ /**
58
+ * Get all the scrollable parent elements of the element
59
+ * @param ele The element to be detected
60
+ * @param areaOnly Only return the parent which will cut visible area
61
+ */
52
62
  function collectScroller(ele) {
53
63
  var scrollerList = [];
54
64
  var current = ele === null || ele === void 0 ? void 0 : ele.parentElement;
@@ -63,4 +73,38 @@ function collectScroller(ele) {
63
73
  current = current.parentElement;
64
74
  }
65
75
  return scrollerList;
76
+ }
77
+ function toNum(num) {
78
+ return Number.isNaN(num) ? 1 : num;
79
+ }
80
+ function getVisibleArea(initArea, scrollerList) {
81
+ var visibleArea = (0, _objectSpread2.default)({}, initArea);
82
+ (scrollerList || []).forEach(function (ele) {
83
+ if (ele instanceof HTMLBodyElement) {
84
+ return;
85
+ }
86
+
87
+ // Skip if static position which will not affect visible area
88
+ var _getWin$getComputedSt2 = getWin(ele).getComputedStyle(ele),
89
+ position = _getWin$getComputedSt2.position;
90
+ if (position === 'static') {
91
+ return;
92
+ }
93
+ var eleRect = ele.getBoundingClientRect();
94
+ var eleOutHeight = ele.offsetHeight,
95
+ eleInnerHeight = ele.clientHeight,
96
+ eleOutWidth = ele.offsetWidth,
97
+ eleInnerWidth = ele.clientWidth;
98
+ var scaleX = toNum(Math.round(eleRect.width / eleOutWidth * 1000) / 1000);
99
+ var scaleY = toNum(Math.round(eleRect.height / eleOutHeight * 1000) / 1000);
100
+ var eleScrollWidth = (eleOutWidth - eleInnerWidth) * scaleX;
101
+ var eleScrollHeight = (eleOutHeight - eleInnerHeight) * scaleY;
102
+ var eleRight = eleRect.x + eleRect.width - eleScrollWidth;
103
+ var eleBottom = eleRect.y + eleRect.height - eleScrollHeight;
104
+ visibleArea.left = Math.max(visibleArea.left, eleRect.x);
105
+ visibleArea.top = Math.max(visibleArea.top, eleRect.y);
106
+ visibleArea.right = Math.min(visibleArea.right, eleRight);
107
+ visibleArea.bottom = Math.min(visibleArea.bottom, eleBottom);
108
+ });
109
+ return visibleArea;
66
110
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rc-component/trigger",
3
- "version": "1.7.1",
3
+ "version": "1.7.3",
4
4
  "description": "base abstract trigger component for react",
5
5
  "engines": {
6
6
  "node": ">=8.x"