Skip to content

Commit a21b430

Browse files
authored
[Tabs] use inlined ScrollbarSize implementation (#18)
* [Tabs] use inlined ScrollbarSize implementation * [Tabs] fix linter errs * [docs] apply babel-preset-env to SVG imports * [Tabs] fix unit test
1 parent 546748a commit a21b430

File tree

6 files changed

+202
-107
lines changed

6 files changed

+202
-107
lines changed

package-lock.json

Lines changed: 27 additions & 103 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@
3838
"dependencies": {
3939
"@babel/runtime": "7.0.0",
4040
"classnames": "2.2.6",
41+
"debounce": "1.2.0",
4142
"material-design-icons": "3.0.1",
42-
"react-scrollbar-size": "2.1.0",
43+
"react-event-listener": "0.6.4",
4344
"react-transition-group": "1.2.1",
4445
"tinycolor2": "1.4.1"
4546
},

src/Tabs/ScrollbarSize.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import EventListener from 'react-event-listener';
4+
import debounce from 'debounce'; // < 1kb payload overhead when lodash/debounce is > 3kb.
5+
6+
const styles = {
7+
width: 100,
8+
height: 100,
9+
position: 'absolute',
10+
top: -10000,
11+
overflow: 'scroll',
12+
msOverflowStyle: 'scrollbar'
13+
};
14+
15+
/**
16+
* @ignore - internal component.
17+
* The component is originates from https://github.com/STORIS/react-scrollbar-size.
18+
* It has been moved into the core in order to minimize the bundle size.
19+
*/
20+
class ScrollbarSize extends React.Component {
21+
handleResize = debounce(() => {
22+
const {onChange} = this.props;
23+
24+
const prevHeight = this.scrollbarHeight;
25+
const prevWidth = this.scrollbarWidth;
26+
this.setMeasurements();
27+
if (prevHeight !== this.scrollbarHeight || prevWidth !== this.scrollbarWidth) {
28+
onChange({scrollbarHeight: this.scrollbarHeight, scrollbarWidth: this.scrollbarWidth});
29+
}
30+
}, 166); // Corresponds to 10 frames at 60 Hz.
31+
32+
componentDidMount() {
33+
this.setMeasurements();
34+
this.props.onLoad({
35+
scrollbarHeight: this.scrollbarHeight,
36+
scrollbarWidth: this.scrollbarWidth
37+
});
38+
}
39+
40+
componentWillUnmount() {
41+
this.handleResize.clear();
42+
}
43+
44+
setMeasurements = () => {
45+
const {nodeRef} = this;
46+
47+
if (!nodeRef) {
48+
return;
49+
}
50+
51+
this.scrollbarHeight = nodeRef.offsetHeight - nodeRef.clientHeight;
52+
this.scrollbarWidth = nodeRef.offsetWidth - nodeRef.clientWidth;
53+
};
54+
55+
render() {
56+
const {onChange} = this.props;
57+
58+
return (
59+
<div>
60+
{onChange ? <EventListener target="window" onResize={this.handleResize}/> : null}
61+
<div
62+
style={styles}
63+
ref={ref => {
64+
this.nodeRef = ref;
65+
}}
66+
/>
67+
</div>
68+
);
69+
}
70+
}
71+
72+
ScrollbarSize.propTypes = {
73+
onChange: PropTypes.func.isRequired,
74+
onLoad: PropTypes.func.isRequired
75+
};
76+
77+
export default ScrollbarSize;

0 commit comments

Comments
 (0)