2017-07-22 21:27:38 +05:30
|
|
|
// @flow
|
|
|
|
import React, { PureComponent } from 'react';
|
2016-05-02 22:55:14 +05:30
|
|
|
|
2017-07-22 21:27:38 +05:30
|
|
|
import { omit, rAF, debounce } from 'functions';
|
2016-07-29 22:44:52 +05:30
|
|
|
|
2016-05-02 22:55:14 +05:30
|
|
|
/**
|
|
|
|
* MeasureHeight is a component that allows you to measure the height of elements wrapped.
|
|
|
|
*
|
|
|
|
* Each time the height changed, the `onMeasure` prop will be called.
|
|
|
|
* On each component update the `shouldMeasure` prop is being called and depending of
|
|
|
|
* the value returned will be decided whether to call `onMeasure`.
|
|
|
|
* By default `shouldMeasure` will compare the old and new values of the `state` prop.
|
|
|
|
* Both `shouldMeasure` and `state` can be used to reduce the amount of meausres, which
|
|
|
|
* will recude the count of forced reflows in browser.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* <MeasureHeight
|
|
|
|
* state={theValueToInvalidateCurrentMeasure}
|
|
|
|
* onMeasure={this.onUpdateContextHeight}
|
|
|
|
* >
|
|
|
|
* <div>some content here</div>
|
|
|
|
* <div>which may be multiple children</div>
|
|
|
|
* </MeasureHeight>
|
|
|
|
*/
|
|
|
|
|
2017-08-23 02:01:41 +05:30
|
|
|
export default class MeasureHeight extends PureComponent<{
|
|
|
|
shouldMeasure: (prevState: any, newState: any) => bool,
|
|
|
|
onMeasure: (height: number) => void,
|
|
|
|
state: any
|
|
|
|
}> {
|
2016-05-02 22:55:14 +05:30
|
|
|
static defaultProps = {
|
2017-07-22 21:27:38 +05:30
|
|
|
shouldMeasure: (prevState: any, newState: any) => prevState !== newState,
|
|
|
|
onMeasure: (height) => {} // eslint-disable-line
|
2016-05-02 22:55:14 +05:30
|
|
|
};
|
|
|
|
|
2017-08-04 10:21:41 +05:30
|
|
|
el: ?HTMLDivElement;
|
2016-05-02 22:55:14 +05:30
|
|
|
|
2017-07-22 21:27:38 +05:30
|
|
|
componentDidMount() {
|
2016-05-02 22:55:14 +05:30
|
|
|
this.measure();
|
2017-10-24 01:11:11 +05:30
|
|
|
window.addEventListener('resize', this.measure);
|
2016-05-02 22:55:14 +05:30
|
|
|
}
|
|
|
|
|
2017-07-22 21:27:38 +05:30
|
|
|
componentDidUpdate(prevProps: typeof MeasureHeight.prototype.props) {
|
2016-05-02 22:55:14 +05:30
|
|
|
if (this.props.shouldMeasure(prevProps.state, this.props.state)) {
|
|
|
|
this.measure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 01:11:11 +05:30
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener('resize', this.measure);
|
|
|
|
}
|
|
|
|
|
2016-05-02 22:55:14 +05:30
|
|
|
render() {
|
2017-07-22 21:27:38 +05:30
|
|
|
const props: Object = omit(this.props, [
|
|
|
|
'shouldMeasure',
|
|
|
|
'onMeasure',
|
|
|
|
'state'
|
|
|
|
]);
|
2016-07-29 22:44:52 +05:30
|
|
|
|
2017-07-22 21:27:38 +05:30
|
|
|
return <div {...props} ref={(el: HTMLDivElement) => this.el = el} />;
|
2016-05-02 22:55:14 +05:30
|
|
|
}
|
|
|
|
|
2017-07-22 21:27:38 +05:30
|
|
|
measure = debounce(() => {
|
2017-08-04 10:21:41 +05:30
|
|
|
rAF(() => this.el && this.props.onMeasure(this.el.offsetHeight));
|
2017-07-22 21:27:38 +05:30
|
|
|
});
|
2016-05-02 22:55:14 +05:30
|
|
|
}
|