Skip to content

Commit 33ff3ec

Browse files
authored
Merge pull request #17 from sakhnyuk/dev
Minor updates
2 parents b3ef55a + 9f8369b commit 33ff3ec

File tree

3 files changed

+49
-21
lines changed

3 files changed

+49
-21
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rc-scrollbars",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "React scrollbars component",
55
"main": "lib/index.js",
66
"scripts": {

src/Scrollbars/index.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { createStyles } from './styles';
1515

1616
interface State {
1717
didMountUniversal: boolean;
18+
scrollbarWidth: number;
1819
}
1920

2021
export class Scrollbars extends Component<ScrollbarsProps, State> {
@@ -94,6 +95,7 @@ export class Scrollbars extends Component<ScrollbarsProps, State> {
9495

9596
this.state = {
9697
didMountUniversal: false,
98+
scrollbarWidth: getScrollbarWidth(),
9799
};
98100
}
99101

@@ -260,7 +262,8 @@ export class Scrollbars extends Component<ScrollbarsProps, State> {
260262

261263
const { view, trackHorizontal, trackVertical, thumbHorizontal, thumbVertical } = this;
262264
view.addEventListener('scroll', this.handleScroll);
263-
if (!getScrollbarWidth()) return;
265+
266+
if (!this.state.scrollbarWidth) return;
264267

265268
trackHorizontal.addEventListener('mouseenter', this.handleTrackMouseEnter);
266269
trackHorizontal.addEventListener('mouseleave', this.handleTrackMouseLeave);
@@ -286,7 +289,9 @@ export class Scrollbars extends Component<ScrollbarsProps, State> {
286289
return;
287290
const { view, trackHorizontal, trackVertical, thumbHorizontal, thumbVertical } = this;
288291
view.removeEventListener('scroll', this.handleScroll);
289-
if (!getScrollbarWidth()) return;
292+
293+
if (!this.state.scrollbarWidth) return;
294+
290295
trackHorizontal.removeEventListener('mouseenter', this.handleTrackMouseEnter);
291296
trackHorizontal.removeEventListener('mouseleave', this.handleTrackMouseLeave);
292297
trackHorizontal.removeEventListener('mousedown', this.handleHorizontalTrackMouseDown);
@@ -504,7 +509,14 @@ export class Scrollbars extends Component<ScrollbarsProps, State> {
504509
_update(callback) {
505510
const { onUpdate, hideTracksWhenNotNeeded } = this.props;
506511
const values = this.getValues();
507-
if (getScrollbarWidth()) {
512+
513+
const freshScrollbarWidth = getScrollbarWidth();
514+
515+
if (this.state.scrollbarWidth !== freshScrollbarWidth) {
516+
this.setState({ scrollbarWidth: freshScrollbarWidth });
517+
}
518+
519+
if (this.state.scrollbarWidth) {
508520
const { scrollLeft, clientWidth, scrollWidth } = values;
509521
const trackHorizontalWidth = getInnerWidth(this.trackHorizontal);
510522

@@ -543,7 +555,8 @@ export class Scrollbars extends Component<ScrollbarsProps, State> {
543555
}
544556

545557
render() {
546-
const scrollbarWidth = getScrollbarWidth();
558+
const { scrollbarWidth, didMountUniversal } = this.state;
559+
547560
/* eslint-disable no-unused-vars */
548561
const {
549562
autoHeight,
@@ -570,12 +583,11 @@ export class Scrollbars extends Component<ScrollbarsProps, State> {
570583
thumbMinSize,
571584
thumbSize,
572585
universal,
586+
disableDefaultStyles,
573587
...props
574588
} = this.props;
575589
/* eslint-enable no-unused-vars */
576590

577-
const { didMountUniversal } = this.state;
578-
579591
const {
580592
containerStyleAutoHeight,
581593
containerStyleDefault,

src/utils/getScrollbarWidth.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,44 @@
11
import css from 'dom-css';
22

33
let scrollbarWidth: number | undefined = undefined;
4+
let pxRatio: number = window.screen.availWidth / document.documentElement.clientWidth;
45

56
export default function getScrollbarWidth() {
7+
/**
8+
* Check zoom ratio. If it was changed, then it would update scrollbatWidth
9+
*/
10+
const newPxRatio = window.screen.availWidth / document.documentElement.clientWidth;
11+
if (pxRatio !== newPxRatio) {
12+
scrollbarWidth = getScrollbarWidthFromDom();
13+
}
14+
615
if (typeof scrollbarWidth === 'number') return scrollbarWidth;
716

817
/* istanbul ignore else */
918
if (typeof document !== 'undefined') {
10-
const div = document.createElement('div');
11-
12-
css(div, {
13-
width: 100,
14-
height: 100,
15-
position: 'absolute',
16-
top: -9999,
17-
overflow: 'scroll',
18-
MsOverflowStyle: 'scrollbar',
19-
});
20-
21-
document.body.appendChild(div);
22-
scrollbarWidth = div.offsetWidth - div.clientWidth;
23-
document.body.removeChild(div);
19+
scrollbarWidth = getScrollbarWidthFromDom();
2420
} else {
2521
scrollbarWidth = 0;
2622
}
23+
2724
return scrollbarWidth || 0;
2825
}
26+
27+
function getScrollbarWidthFromDom() {
28+
const div = document.createElement('div');
29+
30+
css(div, {
31+
width: 100,
32+
height: 100,
33+
position: 'absolute',
34+
top: -9999,
35+
overflow: 'scroll',
36+
MsOverflowStyle: 'scrollbar',
37+
});
38+
39+
document.body.appendChild(div);
40+
const result = div.offsetWidth - div.clientWidth;
41+
document.body.removeChild(div);
42+
43+
return result;
44+
}

0 commit comments

Comments
 (0)