Skip to content

Commit f602777

Browse files
authored
fix: not revert when oos (#353)
1 parent 627d5c7 commit f602777

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

src/hooks/useAlign.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ export default function useAlign(
248248
const visibleR = Math.min(r, visibleArea.right);
249249
const visibleB = Math.min(b, visibleArea.bottom);
250250

251-
return (visibleR - visibleL) * (visibleB - visibleT);
251+
return Math.max(0, (visibleR - visibleL) * (visibleB - visibleT));
252252
}
253253

254254
const originIntersectionVisibleArea = getIntersectionVisibleArea(
@@ -307,7 +307,7 @@ export default function useAlign(
307307
}
308308

309309
if (
310-
getIntersectionVisibleArea(nextOffsetX, tmpNextOffsetY) >
310+
getIntersectionVisibleArea(nextOffsetX, tmpNextOffsetY) >=
311311
originIntersectionVisibleArea
312312
) {
313313
nextOffsetY = tmpNextOffsetY;
@@ -335,7 +335,7 @@ export default function useAlign(
335335
}
336336

337337
if (
338-
getIntersectionVisibleArea(nextOffsetX, tmpNextOffsetY) >
338+
getIntersectionVisibleArea(nextOffsetX, tmpNextOffsetY) >=
339339
originIntersectionVisibleArea
340340
) {
341341
nextOffsetY = tmpNextOffsetY;
@@ -369,7 +369,7 @@ export default function useAlign(
369369
}
370370

371371
if (
372-
getIntersectionVisibleArea(tmpNextOffsetX, nextOffsetY) >
372+
getIntersectionVisibleArea(tmpNextOffsetX, nextOffsetY) >=
373373
originIntersectionVisibleArea
374374
) {
375375
nextOffsetX = tmpNextOffsetX;
@@ -397,7 +397,7 @@ export default function useAlign(
397397
}
398398

399399
if (
400-
getIntersectionVisibleArea(tmpNextOffsetX, nextOffsetY) >
400+
getIntersectionVisibleArea(tmpNextOffsetX, nextOffsetY) >=
401401
originIntersectionVisibleArea
402402
) {
403403
nextOffsetX = tmpNextOffsetX;

tests/flip.test.tsx

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,4 +359,73 @@ describe('Trigger.Align', () => {
359359
bottom: 400,
360360
});
361361
});
362+
363+
// e.g. adjustY + shiftX may make popup out but push back in screen
364+
// which should keep flip
365+
/*
366+
367+
************* Screen
368+
* Popup ********************
369+
************* *
370+
* Target * *
371+
********** *
372+
* *
373+
********************
374+
375+
To:
376+
377+
Screen
378+
********************
379+
********** *
380+
* Target * *
381+
****************** *
382+
* Popup * *
383+
************************
384+
385+
*/
386+
it('out of screen should keep flip', async () => {
387+
spanRect.x = -200;
388+
spanRect.y = 0;
389+
390+
popupRect = {
391+
x: 0,
392+
y: 0,
393+
width: 200,
394+
height: 200,
395+
};
396+
397+
render(
398+
<Trigger
399+
popupVisible
400+
popupPlacement="top"
401+
builtinPlacements={{
402+
top: {
403+
points: ['bc', 'tc'],
404+
overflow: {
405+
shiftX: true,
406+
adjustY: true,
407+
},
408+
},
409+
bottom: {
410+
points: ['tc', 'bc'],
411+
overflow: {
412+
shiftX: true,
413+
adjustY: true,
414+
},
415+
},
416+
}}
417+
popup={<strong>trigger</strong>}
418+
>
419+
<span className="target" />
420+
</Trigger>,
421+
);
422+
423+
await act(async () => {
424+
await Promise.resolve();
425+
});
426+
427+
expect(
428+
document.querySelector('.rc-trigger-popup-placement-bottom'),
429+
).toBeTruthy();
430+
});
362431
});

0 commit comments

Comments
 (0)