Skip to content

Commit 4000da6

Browse files
authored
chore: adjust visible area (#401)
* chore: adjust visible area * test: add test case
1 parent 8fc900b commit 4000da6

File tree

2 files changed

+66
-22
lines changed

2 files changed

+66
-22
lines changed

src/hooks/useAlign.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -555,43 +555,43 @@ export default function useAlign(
555555
const numShiftX = shiftX === true ? 0 : shiftX;
556556
if (typeof numShiftX === 'number') {
557557
// Left
558-
if (nextPopupX < visibleArea.left) {
559-
nextOffsetX -= nextPopupX - visibleArea.left;
558+
if (nextPopupX < visibleRegionArea.left) {
559+
nextOffsetX -= nextPopupX - visibleRegionArea.left;
560560

561-
if (targetRect.x + targetWidth < visibleArea.left + numShiftX) {
561+
if (targetRect.x + targetWidth < visibleRegionArea.left + numShiftX) {
562562
nextOffsetX +=
563-
targetRect.x - visibleArea.left + targetWidth - numShiftX;
563+
targetRect.x - visibleRegionArea.left + targetWidth - numShiftX;
564564
}
565565
}
566566

567567
// Right
568-
if (nextPopupRight > visibleArea.right) {
569-
nextOffsetX -= nextPopupRight - visibleArea.right;
568+
if (nextPopupRight > visibleRegionArea.right) {
569+
nextOffsetX -= nextPopupRight - visibleRegionArea.right;
570570

571-
if (targetRect.x > visibleArea.right - numShiftX) {
572-
nextOffsetX += targetRect.x - visibleArea.right + numShiftX;
571+
if (targetRect.x > visibleRegionArea.right - numShiftX) {
572+
nextOffsetX += targetRect.x - visibleRegionArea.right + numShiftX;
573573
}
574574
}
575575
}
576576

577577
const numShiftY = shiftY === true ? 0 : shiftY;
578578
if (typeof numShiftY === 'number') {
579579
// Top
580-
if (nextPopupY < visibleArea.top) {
581-
nextOffsetY -= nextPopupY - visibleArea.top;
580+
if (nextPopupY < visibleRegionArea.top) {
581+
nextOffsetY -= nextPopupY - visibleRegionArea.top;
582582

583-
if (targetRect.y + targetHeight < visibleArea.top + numShiftY) {
583+
if (targetRect.y + targetHeight < visibleRegionArea.top + numShiftY) {
584584
nextOffsetY +=
585-
targetRect.y - visibleArea.top + targetHeight - numShiftY;
585+
targetRect.y - visibleRegionArea.top + targetHeight - numShiftY;
586586
}
587587
}
588588

589589
// Bottom
590-
if (nextPopupBottom > visibleArea.bottom) {
591-
nextOffsetY -= nextPopupBottom - visibleArea.bottom;
590+
if (nextPopupBottom > visibleRegionArea.bottom) {
591+
nextOffsetY -= nextPopupBottom - visibleRegionArea.bottom;
592592

593-
if (targetRect.y > visibleArea.bottom - numShiftY) {
594-
nextOffsetY += targetRect.y - visibleArea.bottom + numShiftY;
593+
if (targetRect.y > visibleRegionArea.bottom - numShiftY) {
594+
nextOffsetY += targetRect.y - visibleRegionArea.bottom + numShiftY;
595595
}
596596
}
597597
}

tests/flipShift.test.tsx

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ const builtinPlacements = {
4848
shiftY: true,
4949
},
5050
},
51+
topShift: {
52+
points: ['bc', 'tc'],
53+
overflow: {
54+
shiftX: true,
55+
},
56+
htmlRegion: 'visibleFirst' as const,
57+
},
5158
bottom: {
5259
points: ['tc', 'bc'],
5360
overflow: {
@@ -72,7 +79,24 @@ const builtinPlacements = {
7279
};
7380

7481
describe('Trigger.Flip+Shift', () => {
82+
let spanRect = { x: 0, y: 0, width: 0, height: 0 };
83+
84+
beforeEach(() => {
85+
spanRect = {
86+
x: 0,
87+
y: 100,
88+
width: 100,
89+
height: 100,
90+
};
91+
92+
document.documentElement.scrollLeft = 0;
93+
});
94+
7595
beforeAll(() => {
96+
jest
97+
.spyOn(document.documentElement, 'scrollWidth', 'get')
98+
.mockReturnValue(1000);
99+
76100
// Viewport size
77101
spyElementPrototypes(HTMLElement, {
78102
clientWidth: {
@@ -96,12 +120,7 @@ describe('Trigger.Flip+Shift', () => {
96120
});
97121
spyElementPrototypes(HTMLSpanElement, {
98122
getBoundingClientRect() {
99-
return {
100-
x: 0,
101-
y: 100,
102-
width: 100,
103-
height: 100,
104-
};
123+
return spanRect;
105124
},
106125
});
107126
spyElementPrototypes(HTMLElement, {
@@ -146,4 +165,29 @@ describe('Trigger.Flip+Shift', () => {
146165
top: '100px',
147166
});
148167
});
168+
169+
it('top with visibleFirst region', async () => {
170+
spanRect.x = -1000;
171+
document.documentElement.scrollLeft = 500;
172+
173+
render(
174+
<Trigger
175+
popupVisible
176+
popupPlacement="topShift"
177+
builtinPlacements={builtinPlacements}
178+
popup={<strong>trigger</strong>}
179+
>
180+
<span className="target" />
181+
</Trigger>,
182+
);
183+
184+
await act(async () => {
185+
await Promise.resolve();
186+
});
187+
188+
// Just need check left < 0
189+
expect(document.querySelector('.rc-trigger-popup')).toHaveStyle({
190+
left: '-900px',
191+
});
192+
});
149193
});

0 commit comments

Comments
 (0)