-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
1 parent
546748a
commit a21b430
Showing
6 changed files
with
202 additions
and
107 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.