2017-08-28 23:23:44 +03:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-02-10 21:56:48 +02:00
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2017-12-04 09:26:40 +02:00
|
|
|
import scheduleIdleTask from 'flavours/glitch/util/schedule_idle_task';
|
|
|
|
import getRectFromEntry from 'flavours/glitch/util/get_rect_from_entry';
|
2017-08-28 23:23:44 +03:00
|
|
|
|
2018-02-10 21:56:48 +02:00
|
|
|
export default class IntersectionObserverArticle extends ImmutablePureComponent {
|
2017-08-28 23:23:44 +03:00
|
|
|
|
|
|
|
static propTypes = {
|
2017-09-13 11:24:33 +03:00
|
|
|
intersectionObserverWrapper: PropTypes.object.isRequired,
|
2017-08-28 23:23:44 +03: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 11:24:33 +03:00
|
|
|
saveHeightKey: PropTypes.string,
|
|
|
|
cachedHeight: PropTypes.number,
|
|
|
|
onHeightChange: PropTypes.func,
|
2017-08-28 23:23:44 +03:00
|
|
|
children: PropTypes.node,
|
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
isHidden: false, // set to true in requestIdleCallback to trigger un-render
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate (nextProps, nextState) {
|
2018-02-10 21:56:48 +02:00
|
|
|
if (!nextState.isIntersecting && nextState.isHidden) {
|
|
|
|
// It's only if we're not intersecting (i.e. offscreen) and isHidden is true
|
|
|
|
// that either "isIntersecting" or "isHidden" matter, and then they're
|
|
|
|
// the only things that matter (and updated ARIA attributes).
|
|
|
|
return this.state.isIntersecting || !this.state.isHidden || nextProps.listLength !== this.props.listLength;
|
|
|
|
} else if (nextState.isIntersecting && !this.state.isIntersecting) {
|
|
|
|
// If we're going from a non-intersecting state to an intersecting state,
|
|
|
|
// (i.e. offscreen to onscreen), then we definitely need to re-render
|
2017-08-28 23:23:44 +03:00
|
|
|
return true;
|
|
|
|
}
|
2018-02-10 21:56:48 +02:00
|
|
|
// Otherwise, diff based on "updateOnProps" and "updateOnStates"
|
|
|
|
return super.shouldComponentUpdate(nextProps, nextState);
|
2017-08-28 23:23:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
2017-09-13 11:24:33 +03:00
|
|
|
const { intersectionObserverWrapper, id } = this.props;
|
|
|
|
|
|
|
|
intersectionObserverWrapper.observe(
|
|
|
|
id,
|
2017-08-28 23:23:44 +03:00
|
|
|
this.node,
|
|
|
|
this.handleIntersection
|
|
|
|
);
|
|
|
|
|
|
|
|
this.componentMounted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
2017-09-13 11:24:33 +03:00
|
|
|
const { intersectionObserverWrapper, id } = this.props;
|
|
|
|
intersectionObserverWrapper.unobserve(id, this.node);
|
2017-08-28 23:23:44 +03:00
|
|
|
|
|
|
|
this.componentMounted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleIntersection = (entry) => {
|
2017-09-30 15:28:49 +03:00
|
|
|
this.entry = entry;
|
2017-09-13 11:24:33 +03:00
|
|
|
|
2017-09-30 15:28:49 +03:00
|
|
|
scheduleIdleTask(this.calculateHeight);
|
|
|
|
this.setState(this.updateStateAfterIntersection);
|
|
|
|
}
|
2017-08-28 23:23:44 +03:00
|
|
|
|
2017-09-30 15:28:49 +03:00
|
|
|
updateStateAfterIntersection = (prevState) => {
|
|
|
|
if (prevState.isIntersecting && !this.entry.isIntersecting) {
|
|
|
|
scheduleIdleTask(this.hideIfNotIntersecting);
|
2017-08-28 23:23:44 +03:00
|
|
|
}
|
2017-09-30 15:28:49 +03:00
|
|
|
return {
|
|
|
|
isIntersecting: this.entry.isIntersecting,
|
|
|
|
isHidden: false,
|
|
|
|
};
|
|
|
|
}
|
2017-08-28 23:23:44 +03:00
|
|
|
|
2017-09-30 15:28:49 +03: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 23:23:44 +03: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 11:24:33 +03:00
|
|
|
const { children, id, index, listLength, cachedHeight } = this.props;
|
2017-08-28 23:23:44 +03:00
|
|
|
const { isIntersecting, isHidden } = this.state;
|
|
|
|
|
2017-09-13 11:24:33 +03:00
|
|
|
if (!isIntersecting && (isHidden || cachedHeight)) {
|
2017-08-28 23:23:44 +03:00
|
|
|
return (
|
|
|
|
<article
|
|
|
|
ref={this.handleRef}
|
|
|
|
aria-posinset={index}
|
|
|
|
aria-setsize={listLength}
|
2017-09-13 11:24:33 +03:00
|
|
|
style={{ height: `${this.height || cachedHeight}px`, opacity: 0, overflow: 'hidden' }}
|
2017-08-28 23:23:44 +03:00
|
|
|
data-id={id}
|
|
|
|
tabIndex='0'
|
|
|
|
>
|
|
|
|
{children && React.cloneElement(children, { hidden: true })}
|
|
|
|
</article>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<article ref={this.handleRef} aria-posinset={index} aria-setsize={listLength} data-id={id} tabIndex='0'>
|
|
|
|
{children && React.cloneElement(children, { hidden: false })}
|
|
|
|
</article>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|