2020-03-24 04:39:12 +00:00
|
|
|
import { is } from 'immutable'
|
|
|
|
import { setHeight } from '../actions/height_cache'
|
|
|
|
import scheduleIdleTask from '../utils/schedule_idle_task'
|
|
|
|
import getRectFromEntry from '../utils/get_rect_from_entry'
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
// Diff these props in the "rendered" state
|
2020-03-24 04:39:12 +00:00
|
|
|
const updateOnPropsForRendered = ['id', 'index', 'listLength']
|
2019-08-07 06:02:36 +01:00
|
|
|
// Diff these props in the "unrendered" state
|
2020-03-24 04:39:12 +00:00
|
|
|
const updateOnPropsForUnrendered = ['id', 'index', 'listLength', 'cachedHeight']
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-03-04 04:59:11 +00:00
|
|
|
const makeMapStateToProps = (state, props) => ({
|
|
|
|
cachedHeight: state.getIn(['height_cache', props.saveHeightKey, props.id]),
|
2020-03-24 04:39:12 +00:00
|
|
|
})
|
2020-03-04 04:59:11 +00:00
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
|
|
|
|
2020-03-24 04:39:12 +00:00
|
|
|
onHeightChange(key, id, height) {
|
|
|
|
dispatch(setHeight(key, id, height))
|
2020-03-04 04:59:11 +00:00
|
|
|
},
|
|
|
|
|
2020-03-24 04:39:12 +00:00
|
|
|
})
|
2020-03-04 04:59:11 +00:00
|
|
|
|
|
|
|
export default
|
|
|
|
@connect(makeMapStateToProps, mapDispatchToProps)
|
2020-04-08 02:06:59 +01:00
|
|
|
class IntersectionObserverArticle extends React.Component {
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
intersectionObserverWrapper: PropTypes.object.isRequired,
|
2020-04-23 07:13:29 +01:00
|
|
|
id: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number,
|
|
|
|
]),
|
|
|
|
index: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number,
|
|
|
|
]),
|
|
|
|
listLength: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number,
|
|
|
|
]),
|
2019-08-07 06:02:36 +01:00
|
|
|
saveHeightKey: PropTypes.string,
|
|
|
|
cachedHeight: PropTypes.number,
|
|
|
|
onHeightChange: PropTypes.func,
|
|
|
|
children: PropTypes.node,
|
2020-03-24 04:39:12 +00:00
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
state = {
|
2020-05-04 19:44:37 +01:00
|
|
|
isIntersecting: false,
|
2020-05-06 05:33:54 +01:00
|
|
|
isHidden: false,
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
2020-03-24 04:39:12 +00:00
|
|
|
componentDidMount() {
|
2019-08-07 06:02:36 +01:00
|
|
|
const { intersectionObserverWrapper, id } = this.props;
|
|
|
|
|
|
|
|
intersectionObserverWrapper.observe(
|
|
|
|
id,
|
|
|
|
this.node,
|
|
|
|
this.handleIntersection
|
|
|
|
);
|
|
|
|
|
|
|
|
this.componentMounted = true;
|
|
|
|
}
|
|
|
|
|
2020-03-24 04:39:12 +00:00
|
|
|
componentWillUnmount() {
|
2019-08-07 06:02:36 +01:00
|
|
|
const { intersectionObserverWrapper, id } = this.props;
|
|
|
|
intersectionObserverWrapper.unobserve(id, this.node);
|
|
|
|
|
|
|
|
this.componentMounted = false;
|
|
|
|
}
|
|
|
|
|
2020-05-04 19:44:37 +01:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const isUnrendered = !this.state.isIntersecting && (this.state.isHidden || this.props.cachedHeight);
|
|
|
|
const willBeUnrendered = !nextState.isIntersecting && (nextState.isHidden || nextProps.cachedHeight);
|
|
|
|
|
|
|
|
// If we're going from rendered to unrendered (or vice versa) then update
|
|
|
|
if (!!isUnrendered !== !!willBeUnrendered) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, diff based on props
|
|
|
|
const propsToDiff = isUnrendered ? updateOnPropsForUnrendered : updateOnPropsForRendered;
|
|
|
|
return !propsToDiff.every(prop => is(nextProps[prop], this.props[prop]));
|
|
|
|
}
|
|
|
|
|
2019-08-07 06:02:36 +01:00
|
|
|
handleIntersection = (entry) => {
|
|
|
|
this.entry = entry;
|
|
|
|
|
|
|
|
scheduleIdleTask(this.calculateHeight);
|
|
|
|
this.setState(this.updateStateAfterIntersection);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateStateAfterIntersection = (prevState) => {
|
|
|
|
if (prevState.isIntersecting !== false && !this.entry.isIntersecting) {
|
|
|
|
scheduleIdleTask(this.hideIfNotIntersecting);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
isIntersecting: this.entry.isIntersecting,
|
|
|
|
isHidden: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
calculateHeight = () => {
|
2020-03-24 04:39:12 +00:00
|
|
|
const { onHeightChange, saveHeightKey, id } = this.props
|
2019-08-07 06:02:36 +01:00
|
|
|
// Save the height of the fully-rendered element (this is expensive
|
|
|
|
// on Chrome, where we need to fall back to getBoundingClientRect)
|
2020-03-24 04:39:12 +00:00
|
|
|
this.height = getRectFromEntry(this.entry).height
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
if (onHeightChange && saveHeightKey) {
|
2020-03-24 04:39:12 +00:00
|
|
|
onHeightChange(saveHeightKey, id, this.height)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hideIfNotIntersecting = () => {
|
2020-03-24 04:39:12 +00:00
|
|
|
if (!this.componentMounted) return
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-03-24 04:39:12 +00:00
|
|
|
this.setState((prevState) => ({ isHidden: !prevState.isIntersecting }))
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
handleRef = (node) => {
|
2020-03-24 04:39:12 +00:00
|
|
|
this.node = node
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
2020-03-24 04:39:12 +00:00
|
|
|
render() {
|
2020-03-27 22:57:03 +00:00
|
|
|
const {
|
|
|
|
children,
|
|
|
|
id,
|
|
|
|
index,
|
|
|
|
listLength,
|
|
|
|
cachedHeight
|
|
|
|
} = this.props
|
2020-03-24 04:39:12 +00:00
|
|
|
const { isIntersecting, isHidden } = this.state
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
if (!isIntersecting && (isHidden || cachedHeight)) {
|
|
|
|
return (
|
|
|
|
<article
|
2020-06-12 21:55:59 +01:00
|
|
|
className={_s.outlineNone}
|
2019-08-07 06:02:36 +01:00
|
|
|
ref={this.handleRef}
|
|
|
|
aria-posinset={index + 1}
|
|
|
|
aria-setsize={listLength}
|
|
|
|
style={{ height: `${this.height || cachedHeight}px`, opacity: 0, overflow: 'hidden' }}
|
|
|
|
data-id={id}
|
|
|
|
tabIndex='0'
|
|
|
|
>
|
2020-05-06 05:33:54 +01:00
|
|
|
{React.cloneElement(children, { isHidden: true, cachedHeight })}
|
2019-08-07 06:02:36 +01:00
|
|
|
</article>
|
2020-03-24 04:39:12 +00:00
|
|
|
)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<article
|
2020-06-12 21:55:59 +01:00
|
|
|
className={_s.outlineNone}
|
2019-08-07 06:02:36 +01:00
|
|
|
ref={this.handleRef}
|
|
|
|
aria-posinset={index + 1}
|
|
|
|
aria-setsize={listLength}
|
|
|
|
data-id={id}
|
|
|
|
tabIndex='0'
|
|
|
|
>
|
2020-05-06 05:33:54 +01:00
|
|
|
{React.cloneElement(children, { isHidden: false, isIntersecting, cachedHeight })}
|
2019-08-07 06:02:36 +01:00
|
|
|
</article>
|
2020-03-24 04:39:12 +00:00
|
|
|
)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|