2017-08-28 13:23:44 -07:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2022-10-11 01:51:33 -07:00
|
|
|
import scheduleIdleTask from '../features/ui/util/schedule_idle_task';
|
|
|
|
import getRectFromEntry from '../features/ui/util/get_rect_from_entry';
|
2017-08-28 13:23:44 -07:00
|
|
|
|
2019-06-30 01:24:56 -07:00
|
|
|
// Diff these props in the "unrendered" state
|
|
|
|
const updateOnPropsForUnrendered = ['id', 'index', 'listLength', 'cachedHeight'];
|
|
|
|
|
|
|
|
export default class IntersectionObserverArticle extends React.Component {
|
2017-08-28 13:23:44 -07:00
|
|
|
|
|
|
|
static propTypes = {
|
2017-09-13 01:24:33 -07:00
|
|
|
intersectionObserverWrapper: PropTypes.object.isRequired,
|
2017-08-28 13:23:44 -07:00
|
|
|
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
|
|
|
index: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
|
|
|
listLength: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
2017-09-13 01:24:33 -07:00
|
|
|
saveHeightKey: PropTypes.string,
|
|
|
|
cachedHeight: PropTypes.number,
|
|
|
|
onHeightChange: PropTypes.func,
|
2017-08-28 13:23:44 -07:00
|
|
|
children: PropTypes.node,
|
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
isHidden: false, // set to true in requestIdleCallback to trigger un-render
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate (nextProps, nextState) {
|
2019-06-30 01:24:56 -07:00
|
|
|
const isUnrendered = !this.state.isIntersecting && (this.state.isHidden || this.props.cachedHeight);
|
|
|
|
const willBeUnrendered = !nextState.isIntersecting && (nextState.isHidden || nextProps.cachedHeight);
|
|
|
|
if (!!isUnrendered !== !!willBeUnrendered) {
|
|
|
|
// If we're going from rendered to unrendered (or vice versa) then update
|
2017-08-28 13:23:44 -07:00
|
|
|
return true;
|
|
|
|
}
|
2019-06-30 09:05:37 -07:00
|
|
|
// If we are and remain hidden, diff based on props
|
|
|
|
if (isUnrendered) {
|
2019-07-01 03:45:18 -07:00
|
|
|
return !updateOnPropsForUnrendered.every(prop => nextProps[prop] === this.props[prop]);
|
2019-06-30 09:05:37 -07:00
|
|
|
}
|
|
|
|
// Else, assume the children have changed
|
|
|
|
return true;
|
2017-08-28 13:23:44 -07:00
|
|
|
}
|
|
|
|
|
2019-06-30 01:24:56 -07:00
|
|
|
|
2017-08-28 13:23:44 -07:00
|
|
|
componentDidMount () {
|
2017-09-13 01:24:33 -07:00
|
|
|
const { intersectionObserverWrapper, id } = this.props;
|
|
|
|
|
|
|
|
intersectionObserverWrapper.observe(
|
|
|
|
id,
|
2017-08-28 13:23:44 -07:00
|
|
|
this.node,
|
2020-03-08 08:02:36 -07:00
|
|
|
this.handleIntersection,
|
2017-08-28 13:23:44 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
this.componentMounted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
2017-09-13 01:24:33 -07:00
|
|
|
const { intersectionObserverWrapper, id } = this.props;
|
|
|
|
intersectionObserverWrapper.unobserve(id, this.node);
|
2017-08-28 13:23:44 -07:00
|
|
|
|
|
|
|
this.componentMounted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleIntersection = (entry) => {
|
2017-09-30 05:28:49 -07:00
|
|
|
this.entry = entry;
|
2017-09-13 01:24:33 -07:00
|
|
|
|
2017-09-30 05:28:49 -07:00
|
|
|
scheduleIdleTask(this.calculateHeight);
|
|
|
|
this.setState(this.updateStateAfterIntersection);
|
|
|
|
}
|
2017-08-28 13:23:44 -07:00
|
|
|
|
2017-09-30 05:28:49 -07:00
|
|
|
updateStateAfterIntersection = (prevState) => {
|
2019-02-06 09:59:28 -08:00
|
|
|
if (prevState.isIntersecting !== false && !this.entry.isIntersecting) {
|
2017-09-30 05:28:49 -07:00
|
|
|
scheduleIdleTask(this.hideIfNotIntersecting);
|
2017-08-28 13:23:44 -07:00
|
|
|
}
|
2017-09-30 05:28:49 -07:00
|
|
|
return {
|
|
|
|
isIntersecting: this.entry.isIntersecting,
|
|
|
|
isHidden: false,
|
|
|
|
};
|
|
|
|
}
|
2017-08-28 13:23:44 -07:00
|
|
|
|
2017-09-30 05:28:49 -07:00
|
|
|
calculateHeight = () => {
|
|
|
|
const { onHeightChange, saveHeightKey, id } = this.props;
|
|
|
|
// save the height of the fully-rendered element (this is expensive
|
|
|
|
// on Chrome, where we need to fall back to getBoundingClientRect)
|
|
|
|
this.height = getRectFromEntry(this.entry).height;
|
|
|
|
|
|
|
|
if (onHeightChange && saveHeightKey) {
|
|
|
|
onHeightChange(saveHeightKey, id, this.height);
|
|
|
|
}
|
2017-08-28 13:23:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
hideIfNotIntersecting = () => {
|
|
|
|
if (!this.componentMounted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the browser gets a chance, test if we're still not intersecting,
|
|
|
|
// and if so, set our isHidden to true to trigger an unrender. The point of
|
|
|
|
// this is to save DOM nodes and avoid using up too much memory.
|
|
|
|
// See: https://github.com/tootsuite/mastodon/issues/2900
|
|
|
|
this.setState((prevState) => ({ isHidden: !prevState.isIntersecting }));
|
|
|
|
}
|
|
|
|
|
|
|
|
handleRef = (node) => {
|
|
|
|
this.node = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2017-09-13 01:24:33 -07:00
|
|
|
const { children, id, index, listLength, cachedHeight } = this.props;
|
2017-08-28 13:23:44 -07:00
|
|
|
const { isIntersecting, isHidden } = this.state;
|
|
|
|
|
2019-02-06 11:04:39 -08:00
|
|
|
const style = {};
|
|
|
|
|
2017-09-13 01:24:33 -07:00
|
|
|
if (!isIntersecting && (isHidden || cachedHeight)) {
|
2019-02-06 11:04:39 -08:00
|
|
|
style.height = `${this.height || cachedHeight || 150}px`;
|
|
|
|
style.opacity = 0;
|
|
|
|
style.overflow = 'hidden';
|
2017-08-28 13:23:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2019-02-06 11:04:39 -08:00
|
|
|
<article
|
|
|
|
ref={this.handleRef}
|
|
|
|
aria-posinset={index + 1}
|
|
|
|
aria-setsize={listLength}
|
|
|
|
data-id={id}
|
|
|
|
tabIndex='0'
|
|
|
|
style={style}>
|
2019-06-29 01:43:45 -07:00
|
|
|
{children && React.cloneElement(children, { hidden: !isIntersecting && (isHidden || !!cachedHeight) })}
|
2017-08-28 13:23:44 -07:00
|
|
|
</article>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|