Skip to content

Commit 726c878

Browse files
committed
fix: Not lock when window is not scrollable
1 parent a36fbfd commit 726c878

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/useScrollLocker.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import useLayoutEffect, {
44
useLayoutUpdateEffect,
55
} from 'rc-util/lib/hooks/useLayoutEffect';
66
import getScrollBarSize from 'rc-util/lib/getScrollBarSize';
7+
import { isBodyOverflowing } from './util';
78

89
let lockCount = 0;
910
let locked = false;
@@ -18,11 +19,13 @@ function syncLocker() {
1819

1920
if (locked) {
2021
const scrollbarSize = getScrollBarSize();
22+
const isOverflow = isBodyOverflowing();
23+
2124
updateCSS(
2225
`
2326
html body {
2427
overflow-y: hidden;
25-
width: calc(100% - ${scrollbarSize}px);
28+
${isOverflow ? `width: calc(100% - ${scrollbarSize}px);` : ''}
2629
}`,
2730
UNIQUE_ID,
2831
);

src/util.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Test usage export. Do not use in your production
3+
*/
4+
export function isBodyOverflowing() {
5+
return (
6+
document.body.scrollHeight >
7+
(window.innerHeight || document.documentElement.clientHeight) &&
8+
window.innerWidth > document.body.offsetWidth
9+
);
10+
}

tests/index.test.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,21 @@ import React from 'react';
22
import { render } from '@testing-library/react';
33
import Portal from '../src';
44

5+
global.isOverflow = true;
6+
7+
jest.mock('../src/util', () => {
8+
const origin = jest.requireActual('../src/util');
9+
return {
10+
...origin,
11+
isBodyOverflowing: () => global.isOverflow,
12+
};
13+
});
14+
515
describe('Portal', () => {
16+
beforeEach(() => {
17+
global.isOverflow = true;
18+
});
19+
620
it('Order', () => {
721
render(
822
<Portal open debug="root">
@@ -103,5 +117,14 @@ describe('Portal', () => {
103117
test('StrictMode', {
104118
wrapper: React.StrictMode,
105119
});
120+
121+
it('window not scrollable', () => {
122+
global.isOverflow = false;
123+
render(<Portal open />);
124+
125+
expect(document.body).not.toHaveStyle({
126+
overflowY: 'hidden',
127+
});
128+
});
106129
});
107130
});

0 commit comments

Comments
 (0)