Skip to content

Commit d5ad9fd

Browse files
committed
fix: preserve numeric shift inset after popup shift
1 parent 59b659d commit d5ad9fd

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

src/hooks/useAlign.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,30 @@ export default function useAlign(
661661
}
662662
}
663663

664+
syncNextPopupPosition();
665+
666+
if (typeof numShiftX === 'number' && numShiftX !== 0) {
667+
const minLeft = visibleRegion.left + numShiftX;
668+
const maxRight = visibleRegion.right - numShiftX;
669+
670+
if (nextPopupX < minLeft) {
671+
nextOffsetX += minLeft - nextPopupX;
672+
} else if (nextPopupRight > maxRight) {
673+
nextOffsetX += maxRight - nextPopupRight;
674+
}
675+
}
676+
677+
if (typeof numShiftY === 'number' && numShiftY !== 0) {
678+
const minTop = visibleRegion.top + numShiftY;
679+
const maxBottom = visibleRegion.bottom - numShiftY;
680+
681+
if (nextPopupY < minTop) {
682+
nextOffsetY += minTop - nextPopupY;
683+
} else if (nextPopupBottom > maxBottom) {
684+
nextOffsetY += maxBottom - nextPopupBottom;
685+
}
686+
}
687+
664688
// ============================ Arrow ============================
665689
// Arrow center align
666690
const popupLeft = popupRect.x + nextOffsetX;

tests/flipShift.test.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ const builtinPlacements = {
5555
},
5656
htmlRegion: 'visibleFirst' as const,
5757
},
58+
topShiftInset: {
59+
points: ['bc', 'tc'],
60+
overflow: {
61+
shiftX: 20,
62+
},
63+
},
5864
bottom: {
5965
points: ['tc', 'bc'],
6066
overflow: {
@@ -195,6 +201,30 @@ describe('Trigger.Flip+Shift', () => {
195201
});
196202
});
197203

204+
it('keeps numeric shiftX as viewport inset after shift', async () => {
205+
spanRect.x = -50;
206+
spanRect.left = -50;
207+
208+
render(
209+
<Trigger
210+
popupVisible
211+
popupPlacement="topShiftInset"
212+
builtinPlacements={builtinPlacements}
213+
popup={<strong>trigger</strong>}
214+
>
215+
<span className="target" />
216+
</Trigger>,
217+
);
218+
219+
await act(async () => {
220+
await Promise.resolve();
221+
});
222+
223+
expect(document.querySelector('.rc-trigger-popup')).toHaveStyle({
224+
left: '20px',
225+
});
226+
});
227+
198228
// https://github.com/ant-design/ant-design/issues/44096
199229
// Note: Safe to modify `top` style compare if refactor
200230
it('flip not shake by offset with shift', async () => {

0 commit comments

Comments
 (0)