2017-08-28 20:23:44 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2023-05-23 15:15:17 +00:00
|
|
|
import { cloneElement, Component } from 'react';
|
|
|
|
|
2017-08-28 20:23:44 +00:00
|
|
|
import getRectFromEntry from '../features/ui/util/get_rect_from_entry';
|
2023-05-23 15:15:17 +00:00
|
|
|
import scheduleIdleTask from '../features/ui/util/schedule_idle_task';
|
2017-08-28 20:23:44 +00:00
|
|
|
|
2017-09-22 02:58:33 +00:00
|
|
|
// Diff these props in the "unrendered" state
|
|
|
|
const updateOnPropsForUnrendered = ['id', 'index', 'listLength', 'cachedHeight'];
|
|
|
|
|
2023-05-23 08:52:27 +00:00
|
|
|
export default class IntersectionObserverArticle extends Component {
|
2017-08-28 20:23:44 +00:00
|
|
|
|
|
|
|
static propTypes = {
|
2017-09-13 08:24:33 +00:00
|
|
|
intersectionObserverWrapper: PropTypes.object.isRequired,
|
2017-08-28 20:23:44 +00: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 08:24:33 +00:00
|
|
|
saveHeightKey: PropTypes.string,
|
|
|
|
cachedHeight: PropTypes.number,
|
|
|
|
onHeightChange: PropTypes.func,
|
2017-08-28 20:23:44 +00:00
|
|
|
children: PropTypes.node,
|
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
isHidden: false, // set to true in requestIdleCallback to trigger un-render
|
2023-01-30 00:45:35 +00:00
|
|
|
};
|
2017-08-28 20:23:44 +00:00
|
|
|
|
|
|
|
shouldComponentUpdate (nextProps, nextState) {
|
2017-09-22 02:58:33 +00: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 20:23:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-09-26 18:57:07 +00:00
|
|
|
// If we are and remain hidden, diff based on props
|
|
|
|
if (isUnrendered) {
|
|
|
|
return !updateOnPropsForUnrendered.every(prop => nextProps[prop] === this.props[prop]);
|
|
|
|
}
|
|
|
|
// Else, assume the children have changed
|
|
|
|
return true;
|
2017-08-28 20:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
2017-09-13 08:24:33 +00:00
|
|
|
const { intersectionObserverWrapper, id } = this.props;
|
|
|
|
|
|
|
|
intersectionObserverWrapper.observe(
|
|
|
|
id,
|
2017-08-28 20:23:44 +00:00
|
|
|
this.node,
|
2020-03-08 15:02:36 +00:00
|
|
|
this.handleIntersection,
|
2017-08-28 20:23:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
this.componentMounted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
2017-09-13 08:24:33 +00:00
|
|
|
const { intersectionObserverWrapper, id } = this.props;
|
|
|
|
intersectionObserverWrapper.unobserve(id, this.node);
|
2017-08-28 20:23:44 +00:00
|
|
|
|
|
|
|
this.componentMounted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleIntersection = (entry) => {
|
2017-09-30 12:28:49 +00:00
|
|
|
this.entry = entry;
|
2017-09-13 08:24:33 +00:00
|
|
|
|
2017-09-30 12:28:49 +00:00
|
|
|
scheduleIdleTask(this.calculateHeight);
|
|
|
|
this.setState(this.updateStateAfterIntersection);
|
2023-01-30 00:45:35 +00:00
|
|
|
};
|
2017-08-28 20:23:44 +00:00
|
|
|
|
2017-09-30 12:28:49 +00:00
|
|
|
updateStateAfterIntersection = (prevState) => {
|
2019-02-06 22:36:43 +00:00
|
|
|
if (prevState.isIntersecting !== false && !this.entry.isIntersecting) {
|
2017-09-30 12:28:49 +00:00
|
|
|
scheduleIdleTask(this.hideIfNotIntersecting);
|
2017-08-28 20:23:44 +00:00
|
|
|
}
|
2017-09-30 12:28:49 +00:00
|
|
|
return {
|
|
|
|
isIntersecting: this.entry.isIntersecting,
|
|
|
|
isHidden: false,
|
|
|
|
};
|
2023-01-30 00:45:35 +00:00
|
|
|
};
|
2017-08-28 20:23:44 +00:00
|
|
|
|
2017-09-30 12:28:49 +00: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);
|
|
|
|
}
|
2023-01-30 00:45:35 +00:00
|
|
|
};
|
2017-08-28 20:23:44 +00: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.
|
2021-07-13 13:46:20 +00:00
|
|
|
// See: https://github.com/mastodon/mastodon/issues/2900
|
2017-08-28 20:23:44 +00:00
|
|
|
this.setState((prevState) => ({ isHidden: !prevState.isIntersecting }));
|
2023-01-30 00:45:35 +00:00
|
|
|
};
|
2017-08-28 20:23:44 +00:00
|
|
|
|
|
|
|
handleRef = (node) => {
|
|
|
|
this.node = node;
|
2023-01-30 00:45:35 +00:00
|
|
|
};
|
2017-08-28 20:23:44 +00:00
|
|
|
|
|
|
|
render () {
|
2017-09-13 08:24:33 +00:00
|
|
|
const { children, id, index, listLength, cachedHeight } = this.props;
|
2017-08-28 20:23:44 +00:00
|
|
|
const { isIntersecting, isHidden } = this.state;
|
|
|
|
|
2017-09-13 08:24:33 +00:00
|
|
|
if (!isIntersecting && (isHidden || cachedHeight)) {
|
2017-08-28 20:23:44 +00:00
|
|
|
return (
|
|
|
|
<article
|
|
|
|
ref={this.handleRef}
|
2018-08-23 16:28:59 +00:00
|
|
|
aria-posinset={index + 1}
|
2017-08-28 20:23:44 +00:00
|
|
|
aria-setsize={listLength}
|
2017-09-13 08:24:33 +00:00
|
|
|
style={{ height: `${this.height || cachedHeight}px`, opacity: 0, overflow: 'hidden' }}
|
2017-08-28 20:23:44 +00:00
|
|
|
data-id={id}
|
2023-07-23 15:55:13 +00:00
|
|
|
tabIndex={-1}
|
2017-08-28 20:23:44 +00:00
|
|
|
>
|
2023-05-23 08:52:27 +00:00
|
|
|
{children && cloneElement(children, { hidden: true })}
|
2017-08-28 20:23:44 +00:00
|
|
|
</article>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-07-23 15:55:13 +00:00
|
|
|
<article ref={this.handleRef} aria-posinset={index + 1} aria-setsize={listLength} data-id={id} tabIndex={-1}>
|
2023-05-23 08:52:27 +00:00
|
|
|
{children && cloneElement(children, { hidden: false })}
|
2017-08-28 20:23:44 +00:00
|
|
|
</article>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|