Skip to content

Commit f3b2e2c

Browse files
authored
fix: webkit usage (#14)
* fix: webkit usage * test: add test case
1 parent 580c238 commit f3b2e2c

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

src/useScrollLocker.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import * as React from 'react';
22
import { updateCSS, removeCSS } from 'rc-util/lib/Dom/dynamicCSS';
33
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
4-
import getScrollBarSize from 'rc-util/lib/getScrollBarSize';
4+
import { getTargetScrollBarSize } from 'rc-util/lib/getScrollBarSize';
55
import { isBodyOverflowing } from './util';
66

77
const UNIQUE_ID = `rc-util-locker-${Date.now()}`;
88

99
let uuid = 0;
1010

11-
export default function useScrollLocker(
12-
lock?: boolean,
13-
) {
11+
export default function useScrollLocker(lock?: boolean) {
1412
const mergedLock = !!lock;
1513
const [id] = React.useState(() => {
1614
uuid += 1;
@@ -19,7 +17,7 @@ export default function useScrollLocker(
1917

2018
useLayoutEffect(() => {
2119
if (mergedLock) {
22-
const scrollbarSize = getScrollBarSize();
20+
const scrollbarSize = getTargetScrollBarSize(document.body).width;
2321
const isOverflow = isBodyOverflowing();
2422

2523
updateCSS(

tests/webkit.test.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import Portal from '../src';
4+
import { updateCSS } from 'rc-util/lib/Dom/dynamicCSS';
5+
6+
jest.mock('../src/util', () => {
7+
const origin = jest.requireActual('../src/util');
8+
return {
9+
...origin,
10+
isBodyOverflowing: () => true,
11+
};
12+
});
13+
14+
// Revert `useLayoutEffect` back to real one since we should keep order for test
15+
jest.mock('rc-util/lib/hooks/useLayoutEffect', () => {
16+
const origin = jest.requireActual('react');
17+
return origin.useLayoutEffect;
18+
});
19+
20+
// Revert `useLayoutEffect` back to real one since we should keep order for test
21+
jest.mock('rc-util/lib/getScrollBarSize', () => {
22+
const origin = jest.requireActual('rc-util/lib/getScrollBarSize');
23+
return {
24+
...origin,
25+
26+
getTargetScrollBarSize: () => ({ width: 93, height: 1128 }),
27+
};
28+
});
29+
30+
describe('::-webkit-scrollbar', () => {
31+
it('support ::-webkit-scrollbar', () => {
32+
render(
33+
<Portal open autoLock>
34+
<p />
35+
</Portal>,
36+
);
37+
38+
expect(document.body).toHaveStyle({
39+
width: 'calc(100% - 93px)',
40+
});
41+
});
42+
});

0 commit comments

Comments
 (0)