|
| 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