|
|
@ -34,7 +34,7 @@ export default class ScrollableList extends PureComponent {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
state = {
|
|
|
|
state = {
|
|
|
|
lastMouseMove: null,
|
|
|
|
fullscreen: null,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
intersectionObserverWrapper = new IntersectionObserverWrapper();
|
|
|
|
intersectionObserverWrapper = new IntersectionObserverWrapper();
|
|
|
@ -43,7 +43,6 @@ export default class ScrollableList extends PureComponent {
|
|
|
|
if (this.node) {
|
|
|
|
if (this.node) {
|
|
|
|
const { scrollTop, scrollHeight, clientHeight } = this.node;
|
|
|
|
const { scrollTop, scrollHeight, clientHeight } = this.node;
|
|
|
|
const offset = scrollHeight - scrollTop - clientHeight;
|
|
|
|
const offset = scrollHeight - scrollTop - clientHeight;
|
|
|
|
this._oldScrollPosition = scrollHeight - scrollTop;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (400 > offset && this.props.onScrollToBottom && !this.props.isLoading) {
|
|
|
|
if (400 > offset && this.props.onScrollToBottom && !this.props.isLoading) {
|
|
|
|
this.props.onScrollToBottom();
|
|
|
|
this.props.onScrollToBottom();
|
|
|
@ -57,14 +56,6 @@ export default class ScrollableList extends PureComponent {
|
|
|
|
trailing: true,
|
|
|
|
trailing: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
handleMouseMove = throttle(() => {
|
|
|
|
|
|
|
|
this._lastMouseMove = new Date();
|
|
|
|
|
|
|
|
}, 300);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handleMouseLeave = () => {
|
|
|
|
|
|
|
|
this._lastMouseMove = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
componentDidMount () {
|
|
|
|
this.attachScrollListener();
|
|
|
|
this.attachScrollListener();
|
|
|
|
this.attachIntersectionObserver();
|
|
|
|
this.attachIntersectionObserver();
|
|
|
@ -74,24 +65,39 @@ export default class ScrollableList extends PureComponent {
|
|
|
|
this.handleScroll();
|
|
|
|
this.handleScroll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
componentDidUpdate (prevProps) {
|
|
|
|
getScrollPosition = () => {
|
|
|
|
const someItemInserted = React.Children.count(prevProps.children) > 0 &&
|
|
|
|
if (this.node && this.node.scrollTop > 0) {
|
|
|
|
React.Children.count(prevProps.children) < React.Children.count(this.props.children) &&
|
|
|
|
return {height: this.node.scrollHeight, top: this.node.scrollTop};
|
|
|
|
this.getFirstChildKey(prevProps) !== this.getFirstChildKey(this.props);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Reset the scroll position when a new child comes in in order not to
|
|
|
|
updateScrollBottom = (snapshot) => {
|
|
|
|
// jerk the scrollbar around if you're already scrolled down the page.
|
|
|
|
const newScrollTop = this.node.scrollHeight - snapshot;
|
|
|
|
if (someItemInserted && this._oldScrollPosition && this.node.scrollTop > 0) {
|
|
|
|
|
|
|
|
const newScrollTop = this.node.scrollHeight - this._oldScrollPosition;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this.node.scrollTop !== newScrollTop) {
|
|
|
|
if (this.node.scrollTop !== newScrollTop) {
|
|
|
|
this.node.scrollTop = newScrollTop;
|
|
|
|
this.node.scrollTop = newScrollTop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getSnapshotBeforeUpdate (prevProps, prevState) {
|
|
|
|
|
|
|
|
const someItemInserted = React.Children.count(prevProps.children) > 0 &&
|
|
|
|
|
|
|
|
React.Children.count(prevProps.children) < React.Children.count(this.props.children) &&
|
|
|
|
|
|
|
|
this.getFirstChildKey(prevProps) !== this.getFirstChildKey(this.props);
|
|
|
|
|
|
|
|
if (someItemInserted && this.node.scrollTop > 0) {
|
|
|
|
|
|
|
|
return this.node.scrollHeight - this.node.scrollTop;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
this._oldScrollPosition = this.node.scrollHeight - this.node.scrollTop;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
componentDidUpdate (prevProps, prevState, snapshot) {
|
|
|
|
|
|
|
|
// Reset the scroll position when a new child comes in in order not to
|
|
|
|
|
|
|
|
// jerk the scrollbar around if you're already scrolled down the page.
|
|
|
|
|
|
|
|
if (snapshot !== null) this.updateScrollBottom(snapshot);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
|
|
|
componentWillUnmount () {
|
|
|
|
this.detachScrollListener();
|
|
|
|
this.detachScrollListener();
|
|
|
|
this.detachIntersectionObserver();
|
|
|
|
this.detachIntersectionObserver();
|
|
|
@ -141,10 +147,6 @@ export default class ScrollableList extends PureComponent {
|
|
|
|
this.props.onScrollToBottom();
|
|
|
|
this.props.onScrollToBottom();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_recentlyMoved () {
|
|
|
|
|
|
|
|
return this._lastMouseMove !== null && ((new Date()) - this._lastMouseMove < 600);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
render () {
|
|
|
|
render () {
|
|
|
|
const { children, scrollKey, trackScroll, shouldUpdateScroll, isLoading, hasMore, prepend, emptyMessage } = this.props;
|
|
|
|
const { children, scrollKey, trackScroll, shouldUpdateScroll, isLoading, hasMore, prepend, emptyMessage } = this.props;
|
|
|
|
const { fullscreen } = this.state;
|
|
|
|
const { fullscreen } = this.state;
|
|
|
@ -155,7 +157,7 @@ export default class ScrollableList extends PureComponent {
|
|
|
|
|
|
|
|
|
|
|
|
if (isLoading || childrenCount > 0 || !emptyMessage) {
|
|
|
|
if (isLoading || childrenCount > 0 || !emptyMessage) {
|
|
|
|
scrollableArea = (
|
|
|
|
scrollableArea = (
|
|
|
|
<div className={classNames('scrollable', { fullscreen })} ref={this.setRef} onMouseMove={this.handleMouseMove} onMouseLeave={this.handleMouseLeave}>
|
|
|
|
<div className={classNames('scrollable', { fullscreen })} ref={this.setRef}>
|
|
|
|
<div role='feed' className='item-list'>
|
|
|
|
<div role='feed' className='item-list'>
|
|
|
|
{prepend}
|
|
|
|
{prepend}
|
|
|
|
|
|
|
|
|
|
|
@ -168,7 +170,7 @@ export default class ScrollableList extends PureComponent {
|
|
|
|
intersectionObserverWrapper={this.intersectionObserverWrapper}
|
|
|
|
intersectionObserverWrapper={this.intersectionObserverWrapper}
|
|
|
|
saveHeightKey={trackScroll ? `${this.context.router.route.location.key}:${scrollKey}` : null}
|
|
|
|
saveHeightKey={trackScroll ? `${this.context.router.route.location.key}:${scrollKey}` : null}
|
|
|
|
>
|
|
|
|
>
|
|
|
|
{child}
|
|
|
|
{React.cloneElement(child, {getScrollPosition: this.getScrollPosition, updateScrollBottom: this.updateScrollBottom})}
|
|
|
|
</IntersectionObserverArticleContainer>
|
|
|
|
</IntersectionObserverArticleContainer>
|
|
|
|
))}
|
|
|
|
))}
|
|
|
|
|
|
|
|
|
|
|
|