@@ -166,6 +166,9 @@ export default function useAlign(
166
166
const popupHeight = popupRect . height ;
167
167
const popupWidth = popupRect . width ;
168
168
169
+ const targetHeight = targetRect . height ;
170
+ const targetWidth = targetRect . width ;
171
+
169
172
// Get bounding of visible area
170
173
const visibleArea =
171
174
placementInfo . htmlRegion === 'scroll'
@@ -274,13 +277,20 @@ export default function useAlign(
274
277
275
278
const needAdjustY = supportAdjust ( adjustY ) ;
276
279
280
+ const sameTB = popupPoints [ 0 ] === targetPoints [ 0 ] ;
281
+
277
282
// Bottom to Top
278
283
if (
279
284
needAdjustY &&
280
285
popupPoints [ 0 ] === 't' &&
281
286
nextPopupBottom > visibleArea . bottom
282
287
) {
283
- nextOffsetY = targetAlignPointTL . y - popupAlignPointBR . y - popupOffsetY ;
288
+ if ( sameTB ) {
289
+ nextOffsetY -= popupHeight - targetHeight ;
290
+ } else {
291
+ nextOffsetY =
292
+ targetAlignPointTL . y - popupAlignPointBR . y - popupOffsetY ;
293
+ }
284
294
285
295
nextAlignInfo . points = [
286
296
reversePoints ( popupPoints , 0 ) ,
@@ -294,7 +304,12 @@ export default function useAlign(
294
304
popupPoints [ 0 ] === 'b' &&
295
305
nextPopupY < visibleArea . top
296
306
) {
297
- nextOffsetY = targetAlignPointBR . y - popupAlignPointTL . y - popupOffsetY ;
307
+ if ( sameTB ) {
308
+ nextOffsetY += popupHeight - targetHeight ;
309
+ } else {
310
+ nextOffsetY =
311
+ targetAlignPointBR . y - popupAlignPointTL . y - popupOffsetY ;
312
+ }
298
313
299
314
nextAlignInfo . points = [
300
315
reversePoints ( popupPoints , 0 ) ,
@@ -309,13 +324,20 @@ export default function useAlign(
309
324
const needAdjustX = supportAdjust ( adjustX ) ;
310
325
311
326
// >>>>> Flip
327
+ const sameLR = popupPoints [ 1 ] === targetPoints [ 1 ] ;
328
+
312
329
// Right to Left
313
330
if (
314
331
needAdjustX &&
315
332
popupPoints [ 1 ] === 'l' &&
316
333
nextPopupRight > visibleArea . right
317
334
) {
318
- nextOffsetX = targetAlignPointTL . x - popupAlignPointBR . x - popupOffsetX ;
335
+ if ( sameLR ) {
336
+ nextOffsetX -= popupWidth - targetWidth ;
337
+ } else {
338
+ nextOffsetX =
339
+ targetAlignPointTL . x - popupAlignPointBR . x - popupOffsetX ;
340
+ }
319
341
320
342
nextAlignInfo . points = [
321
343
reversePoints ( popupPoints , 1 ) ,
@@ -329,7 +351,12 @@ export default function useAlign(
329
351
popupPoints [ 1 ] === 'r' &&
330
352
nextPopupX < visibleArea . left
331
353
) {
332
- nextOffsetX = targetAlignPointBR . x - popupAlignPointTL . x - popupOffsetX ;
354
+ if ( sameLR ) {
355
+ nextOffsetX += popupWidth - targetWidth ;
356
+ } else {
357
+ nextOffsetX =
358
+ targetAlignPointBR . x - popupAlignPointTL . x - popupOffsetX ;
359
+ }
333
360
334
361
nextAlignInfo . points = [
335
362
reversePoints ( popupPoints , 1 ) ,
@@ -344,9 +371,9 @@ export default function useAlign(
344
371
if ( nextPopupX < visibleArea . left ) {
345
372
nextOffsetX -= nextPopupX - visibleArea . left ;
346
373
347
- if ( targetRect . x + targetRect . width < visibleArea . left + numShiftX ) {
374
+ if ( targetRect . x + targetWidth < visibleArea . left + numShiftX ) {
348
375
nextOffsetX +=
349
- targetRect . x - visibleArea . left + targetRect . width - numShiftX ;
376
+ targetRect . x - visibleArea . left + targetWidth - numShiftX ;
350
377
}
351
378
}
352
379
@@ -366,9 +393,9 @@ export default function useAlign(
366
393
if ( nextPopupY < visibleArea . top ) {
367
394
nextOffsetY -= nextPopupY - visibleArea . top ;
368
395
369
- if ( targetRect . y + targetRect . height < visibleArea . top + numShiftY ) {
396
+ if ( targetRect . y + targetHeight < visibleArea . top + numShiftY ) {
370
397
nextOffsetY +=
371
- targetRect . y - visibleArea . top + targetRect . height - numShiftY ;
398
+ targetRect . y - visibleArea . top + targetHeight - numShiftY ;
372
399
}
373
400
}
374
401
@@ -389,9 +416,9 @@ export default function useAlign(
389
416
const popupBottom = popupTop + popupHeight ;
390
417
391
418
const targetLeft = targetRect . x ;
392
- const targetRight = targetLeft + targetRect . width ;
419
+ const targetRight = targetLeft + targetWidth ;
393
420
const targetTop = targetRect . y ;
394
- const targetBottom = targetTop + targetRect . height ;
421
+ const targetBottom = targetTop + targetHeight ;
395
422
396
423
const maxLeft = Math . max ( popupLeft , targetLeft ) ;
397
424
const minRight = Math . min ( popupRight , targetRight ) ;
0 commit comments