Skip to content

Commit 0336525

Browse files
acyzazombieJ
andauthored
fix: correct flip (#349)
* fix: correct flip * chore: fix lint * chore: optimize test * chore: optimize test * chore: add test * chore: update * chore: update * test: merge test case * chore: rename * chore: merge naming * chore: merge master * chore: merge master --------- Co-authored-by: 二货机器人 <[email protected]>
1 parent a699a1d commit 0336525

File tree

2 files changed

+98
-12
lines changed

2 files changed

+98
-12
lines changed

src/hooks/useAlign.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,19 @@ export default function useAlign(
269269

270270
// ============== Intersection ===============
271271
// 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;
275275

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);
278281
const visibleR = Math.min(r, visibleArea.right);
279282
const visibleB = Math.min(b, visibleArea.bottom);
280283

281-
return (visibleR - visibleX) * (visibleB - visibleY);
284+
return (visibleR - visibleL) * (visibleB - visibleT);
282285
}
283286

284287
const originIntersectionVisibleArea = getIntersectionVisibleArea(

tests/flip.test.tsx

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ describe('Trigger.Align', () => {
4141
height: 1,
4242
};
4343

44+
let popupRect = {
45+
x: 0,
46+
y: 0,
47+
width: 100,
48+
height: 100,
49+
};
50+
4451
beforeAll(() => {
4552
spyElementPrototypes(HTMLElement, {
4653
clientWidth: {
@@ -53,12 +60,7 @@ describe('Trigger.Align', () => {
5360

5461
spyElementPrototypes(HTMLDivElement, {
5562
getBoundingClientRect() {
56-
return {
57-
x: 0,
58-
y: 0,
59-
width: 100,
60-
height: 100,
61-
};
63+
return popupRect;
6264
},
6365
});
6466

@@ -82,6 +84,12 @@ describe('Trigger.Align', () => {
8284
width: 1,
8385
height: 1,
8486
};
87+
popupRect = {
88+
x: 0,
89+
y: 0,
90+
width: 100,
91+
height: 100,
92+
};
8593
jest.useFakeTimers();
8694
});
8795

@@ -187,4 +195,79 @@ describe('Trigger.Align', () => {
187195
});
188196
});
189197
});
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+
});
190273
});

0 commit comments

Comments
 (0)