gab-social/app/javascript/gabsocial/components/intersection_observer_article.js

164 lines
4.3 KiB
JavaScript
Raw Normal View History

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'
// Diff these props in the "rendered" state
2020-03-24 04:39:12 +00:00
const updateOnPropsForRendered = ['id', 'index', 'listLength']
// Diff these props in the "unrendered" state
2020-03-24 04:39:12 +00:00
const updateOnPropsForUnrendered = ['id', 'index', 'listLength', 'cachedHeight']
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 {
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,
]),
saveHeightKey: PropTypes.string,
cachedHeight: PropTypes.number,
onHeightChange: PropTypes.func,
children: PropTypes.node,
2020-03-24 04:39:12 +00:00
}
state = {
2020-05-04 19:44:37 +01:00
isIntersecting: false,
2020-05-06 05:33:54 +01:00
isHidden: false,
}
2020-03-24 04:39:12 +00:00
componentDidMount() {
const { intersectionObserverWrapper, id } = this.props;
intersectionObserverWrapper.observe(
id,
this.node,
this.handleIntersection
);
this.componentMounted = true;
}
2020-03-24 04:39:12 +00:00
componentWillUnmount() {
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]));
}
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
// 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
if (onHeightChange && saveHeightKey) {
2020-03-24 04:39:12 +00:00
onHeightChange(saveHeightKey, id, this.height)
}
}
hideIfNotIntersecting = () => {
2020-03-24 04:39:12 +00:00
if (!this.componentMounted) return
2020-03-24 04:39:12 +00:00
this.setState((prevState) => ({ isHidden: !prevState.isIntersecting }))
}
handleRef = (node) => {
2020-03-24 04:39:12 +00:00
this.node = node
}
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
if (!isIntersecting && (isHidden || cachedHeight)) {
return (
<article
2020-03-27 22:57:03 +00:00
className={[_s.outlineNone].join(' ')}
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 })}
</article>
2020-03-24 04:39:12 +00:00
)
}
return (
<article
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 })}
</article>
2020-03-24 04:39:12 +00:00
)
}
}