File tree Expand file tree Collapse file tree 2 files changed +98
-12
lines changed Expand file tree Collapse file tree 2 files changed +98
-12
lines changed Original file line number Diff line number Diff line change @@ -269,16 +269,19 @@ export default function useAlign(
269
269
270
270
// ============== Intersection ===============
271
271
// Get area by position. Used for check if flip area is better
272
- function getIntersectionVisibleArea ( x : number , y : number ) {
273
- const r = x + popupWidth ;
274
- const b = y + popupHeight ;
272
+ function getIntersectionVisibleArea ( offsetX : number , offsetY : number ) {
273
+ const l = popupRect . x + offsetX ;
274
+ const t = popupRect . y + offsetY ;
275
275
276
- const visibleX = Math . max ( x , visibleArea . left ) ;
277
- const visibleY = Math . max ( y , visibleArea . top ) ;
276
+ const r = l + popupWidth ;
277
+ const b = t + popupHeight ;
278
+
279
+ const visibleL = Math . max ( l , visibleArea . left ) ;
280
+ const visibleT = Math . max ( t , visibleArea . top ) ;
278
281
const visibleR = Math . min ( r , visibleArea . right ) ;
279
282
const visibleB = Math . min ( b , visibleArea . bottom ) ;
280
283
281
- return ( visibleR - visibleX ) * ( visibleB - visibleY ) ;
284
+ return ( visibleR - visibleL ) * ( visibleB - visibleT ) ;
282
285
}
283
286
284
287
const originIntersectionVisibleArea = getIntersectionVisibleArea (
Original file line number Diff line number Diff line change @@ -41,6 +41,13 @@ describe('Trigger.Align', () => {
41
41
height : 1 ,
42
42
} ;
43
43
44
+ let popupRect = {
45
+ x : 0 ,
46
+ y : 0 ,
47
+ width : 100 ,
48
+ height : 100 ,
49
+ } ;
50
+
44
51
beforeAll ( ( ) => {
45
52
spyElementPrototypes ( HTMLElement , {
46
53
clientWidth : {
@@ -53,12 +60,7 @@ describe('Trigger.Align', () => {
53
60
54
61
spyElementPrototypes ( HTMLDivElement , {
55
62
getBoundingClientRect ( ) {
56
- return {
57
- x : 0 ,
58
- y : 0 ,
59
- width : 100 ,
60
- height : 100 ,
61
- } ;
63
+ return popupRect ;
62
64
} ,
63
65
} ) ;
64
66
@@ -82,6 +84,12 @@ describe('Trigger.Align', () => {
82
84
width : 1 ,
83
85
height : 1 ,
84
86
} ;
87
+ popupRect = {
88
+ x : 0 ,
89
+ y : 0 ,
90
+ width : 100 ,
91
+ height : 100 ,
92
+ } ;
85
93
jest . useFakeTimers ( ) ;
86
94
} ) ;
87
95
@@ -187,4 +195,79 @@ describe('Trigger.Align', () => {
187
195
} ) ;
188
196
} ) ;
189
197
} ) ;
198
+
199
+ // `getPopupContainer` sometime makes the popup 0/0 not start at left top.
200
+ // We need cal the real visible position
201
+ /*
202
+
203
+ *******************
204
+ * Target *
205
+ * *************
206
+ * * Popup *
207
+ * *************
208
+ * *
209
+ *******************
210
+
211
+ To:
212
+
213
+ *******************
214
+ * Target *
215
+ * ************* *
216
+ * * Popup * *
217
+ * ************* *
218
+ * *
219
+ *******************
220
+
221
+ */
222
+ it ( 'popup start position not at left top' , async ( ) => {
223
+ spanRect . x = 99 ;
224
+ spanRect . y = 0 ;
225
+
226
+ popupRect = {
227
+ x : 100 ,
228
+ y : 1 ,
229
+ width : 100 ,
230
+ height : 100 ,
231
+ } ;
232
+
233
+ render (
234
+ < Trigger
235
+ popupVisible
236
+ popupPlacement = "topLeft"
237
+ builtinPlacements = { {
238
+ topLeft : {
239
+ points : [ 'tl' , 'bl' ] ,
240
+ overflow : {
241
+ adjustX : true ,
242
+ adjustY : true ,
243
+ } ,
244
+ } ,
245
+ topRight : {
246
+ points : [ 'tr' , 'br' ] ,
247
+ overflow : {
248
+ adjustX : true ,
249
+ adjustY : true ,
250
+ } ,
251
+ } ,
252
+ } }
253
+ popup = { < strong > trigger</ strong > }
254
+ >
255
+ < span className = "target" />
256
+ </ Trigger > ,
257
+ ) ;
258
+
259
+ await act ( async ( ) => {
260
+ await Promise . resolve ( ) ;
261
+ } ) ;
262
+
263
+ // Flip
264
+ expect (
265
+ document . querySelector ( '.rc-trigger-popup-placement-topRight' ) ,
266
+ ) . toBeTruthy ( ) ;
267
+
268
+ expect ( document . querySelector ( '.rc-trigger-popup' ) ) . toHaveStyle ( {
269
+ left : `-100px` , // (left: 100) - (offset: 100) = 0
270
+ top : `0px` ,
271
+ } ) ;
272
+ } ) ;
190
273
} ) ;
You can’t perform that action at this time.
0 commit comments