Skip to content

Commit

Permalink
[Tabs] use inlined ScrollbarSize implementation (#18)
Browse files Browse the repository at this point in the history
* [Tabs] use inlined ScrollbarSize implementation

* [Tabs] fix linter errs

* [docs] apply babel-preset-env to SVG imports

* [Tabs] fix unit test
  • Loading branch information
petermikitsh authored Oct 3, 2018
1 parent 546748a commit a21b430
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 107 deletions.
130 changes: 27 additions & 103 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
"dependencies": {
"@babel/runtime": "7.0.0",
"classnames": "2.2.6",
"debounce": "1.2.0",
"material-design-icons": "3.0.1",
"react-scrollbar-size": "2.1.0",
"react-event-listener": "0.6.4",
"react-transition-group": "1.2.1",
"tinycolor2": "1.4.1"
},
Expand Down
77 changes: 77 additions & 0 deletions src/Tabs/ScrollbarSize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import React from 'react';
import PropTypes from 'prop-types';
import EventListener from 'react-event-listener';
import debounce from 'debounce'; // < 1kb payload overhead when lodash/debounce is > 3kb.

const styles = {
width: 100,
height: 100,
position: 'absolute',
top: -10000,
overflow: 'scroll',
msOverflowStyle: 'scrollbar'
};

/**
* @ignore - internal component.
* The component is originates from https://github.com/STORIS/react-scrollbar-size.
* It has been moved into the core in order to minimize the bundle size.
*/
class ScrollbarSize extends React.Component {
handleResize = debounce(() => {
const {onChange} = this.props;

const prevHeight = this.scrollbarHeight;
const prevWidth = this.scrollbarWidth;
this.setMeasurements();
if (prevHeight !== this.scrollbarHeight || prevWidth !== this.scrollbarWidth) {
onChange({scrollbarHeight: this.scrollbarHeight, scrollbarWidth: this.scrollbarWidth});
}
}, 166); // Corresponds to 10 frames at 60 Hz.

componentDidMount() {
this.setMeasurements();
this.props.onLoad({
scrollbarHeight: this.scrollbarHeight,
scrollbarWidth: this.scrollbarWidth
});
}

componentWillUnmount() {
this.handleResize.clear();
}

setMeasurements = () => {
const {nodeRef} = this;

if (!nodeRef) {
return;
}

this.scrollbarHeight = nodeRef.offsetHeight - nodeRef.clientHeight;
this.scrollbarWidth = nodeRef.offsetWidth - nodeRef.clientWidth;
};

render() {
const {onChange} = this.props;

return (
<div>
{onChange ? <EventListener target="window" onResize={this.handleResize}/> : null}
<div
style={styles}
ref={ref => {
this.nodeRef = ref;
}}
/>
</div>
);
}
}

ScrollbarSize.propTypes = {
onChange: PropTypes.func.isRequired,
onLoad: PropTypes.func.isRequired
};

export default ScrollbarSize;
Loading

0 comments on commit a21b430

Please sign in to comment.