Skip to content

Commit 08df278

Browse files
committed
fix: Portal not lock screen when container is not body
1 parent 0747c5b commit 08df278

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/Portal.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ export default function Portal(props: PortalProps) {
5959

6060
const [mergedRender, setMergedRender] = React.useState(open);
6161

62-
useScrollLocker(autoLock && open);
63-
6462
// ====================== Should Render ======================
6563
React.useEffect(() => {
6664
if (autoDestroy || open) {
@@ -86,6 +84,15 @@ export default function Portal(props: PortalProps) {
8684
);
8785
const mergedContainer = innerContainer ?? defaultContainer;
8886

87+
// ========================= Locker ==========================
88+
useScrollLocker(
89+
autoLock &&
90+
open &&
91+
canUseDom() &&
92+
(mergedContainer === defaultContainer ||
93+
mergedContainer === document.body),
94+
);
95+
8996
// ========================= Render ==========================
9097
// Do not render when nothing need render
9198
// When innerContainer is `undefined`, it may not ready since user use ref in the same render

src/useScrollLocker.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const UNIQUE_ID = `rc-util-locker-${Date.now()}`;
88

99
let uuid = 0;
1010

11-
export default function useScrollLocker(lock?: boolean) {
11+
export default function useScrollLocker(
12+
lock?: boolean,
13+
) {
1214
const mergedLock = !!lock;
1315
const [id] = React.useState(() => {
1416
uuid += 1;

tests/index.test.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,32 @@ describe('Portal', () => {
126126
overflowY: 'hidden',
127127
});
128128
});
129+
130+
it('not lock screen when getContainer is not body', () => {
131+
const div = document.createElement('div');
132+
document.body.appendChild(div);
133+
render(
134+
<Portal open autoLock getContainer={() => div}>
135+
Bamboo
136+
</Portal>,
137+
);
138+
139+
expect(document.body).not.toHaveStyle({
140+
overflowY: 'hidden',
141+
});
142+
});
143+
144+
it('lock when getContainer give document.body', () => {
145+
render(
146+
<Portal open autoLock getContainer={() => document.body}>
147+
Bamboo
148+
</Portal>,
149+
);
150+
151+
expect(document.body).toHaveStyle({
152+
overflowY: 'hidden',
153+
});
154+
});
129155
});
130156

131157
it('autoDestroy', () => {

0 commit comments

Comments
 (0)