Skip to content

Commit b94c4a9

Browse files
authored
fix: not force fill auto (#508)
1 parent 6253c1b commit b94c4a9

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

docs/examples/getScrollBarSize.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,23 @@ const cssText = `
2222
`;
2323

2424
export default () => {
25+
const defaultRef = React.useRef<HTMLDivElement>();
2526
const webkitRef = React.useRef<HTMLDivElement>();
2627
const scrollRef = React.useRef<HTMLDivElement>();
2728
const [sizeData, setSizeData] = React.useState('');
2829

2930
React.useEffect(() => {
3031
const originSize = getScrollBarSize();
32+
const defaultSize = getTargetScrollBarSize(defaultRef.current);
3133
const webkitSize = getTargetScrollBarSize(webkitRef.current);
3234
const scrollSize = getTargetScrollBarSize(scrollRef.current);
3335

3436
setSizeData(
3537
[
3638
`Origin: ${originSize}`,
39+
`Default: ${defaultSize.width}/${defaultSize.height}`,
3740
`Webkit: ${webkitSize.width}/${webkitSize.height}`,
38-
`Webkit: ${scrollSize.width}/${scrollSize.height}`,
41+
`Scroll: ${scrollSize.width}/${scrollSize.height}`,
3942
].join(', '),
4043
);
4144
}, []);
@@ -55,6 +58,7 @@ export default () => {
5558
overflow: 'scroll',
5659
background: 'yellow',
5760
}}
61+
ref={defaultRef}
5862
>
5963
Origin
6064
</div>

src/getScrollBarSize.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,31 @@ function measureScrollbarSize(ele?: HTMLElement): ScrollBarSize {
3636

3737
// Set Webkit style
3838
const webkitScrollbarStyle = getComputedStyle(ele, '::-webkit-scrollbar');
39+
const width = parseInt(webkitScrollbarStyle.width, 10);
40+
const height = parseInt(webkitScrollbarStyle.height, 10);
3941

4042
// Try wrap to handle CSP case
4143
try {
44+
const widthStyle = width ? `width: ${webkitScrollbarStyle.width};` : '';
45+
const heightStyle = height
46+
? `height: ${webkitScrollbarStyle.height};`
47+
: '';
48+
4249
updateCSS(
4350
`
44-
#${randomId}::-webkit-scrollbar {
45-
width: ${webkitScrollbarStyle.width};
46-
height: ${webkitScrollbarStyle.height};
47-
}
48-
`,
51+
#${randomId}::-webkit-scrollbar {
52+
${widthStyle}
53+
${heightStyle}
54+
}`,
4955
randomId,
5056
);
5157
} catch (e) {
5258
// Can't wrap, just log error
5359
console.error(e);
5460

5561
// Get from style directly
56-
fallbackWidth = parseInt(webkitScrollbarStyle.width, 10);
57-
fallbackHeight = parseInt(webkitScrollbarStyle.height, 10);
62+
fallbackWidth = width;
63+
fallbackHeight = height;
5864
}
5965
}
6066

0 commit comments

Comments
 (0)