TimelineQueueButtonHeader: automatically load new items when scrolled to the top of the page

This commit is contained in:
Alex Gleason 2021-10-08 15:20:08 -05:00
parent 75a496c806
commit 81921578e0
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 20 additions and 6 deletions

View File

@ -14,11 +14,15 @@ class TimelineQueueButtonHeader extends React.PureComponent {
message: PropTypes.object.isRequired, message: PropTypes.object.isRequired,
threshold: PropTypes.number, threshold: PropTypes.number,
intl: PropTypes.object.isRequired, intl: PropTypes.object.isRequired,
autoload: PropTypes.bool,
autoloadThreshold: PropTypes.number,
}; };
static defaultProps = { static defaultProps = {
count: 0, count: 0,
threshold: 400, threshold: 400,
autoload: true,
autoloadThreshold: 50,
}; };
state = { state = {
@ -26,9 +30,6 @@ class TimelineQueueButtonHeader extends React.PureComponent {
} }
componentDidMount() { componentDidMount() {
this.window = window;
this.documentElement = document.scrollingElement || document.documentElement;
this.attachScrollListener(); this.attachScrollListener();
} }
@ -36,17 +37,30 @@ class TimelineQueueButtonHeader extends React.PureComponent {
this.detachScrollListener(); this.detachScrollListener();
} }
componentDidUpdate(prevProps, prevState) {
const { scrollTop } = (document.scrollingElement || document.documentElement);
const { count, onClick, autoload, autoloadThreshold } = this.props;
if (autoload && scrollTop <= autoloadThreshold && count !== prevProps.count) {
onClick();
}
}
attachScrollListener() { attachScrollListener() {
this.window.addEventListener('scroll', this.handleScroll); window.addEventListener('scroll', this.handleScroll);
} }
detachScrollListener() { detachScrollListener() {
this.window.removeEventListener('scroll', this.handleScroll); window.removeEventListener('scroll', this.handleScroll);
} }
handleScroll = throttle(() => { handleScroll = throttle(() => {
const { scrollTop } = (document.scrollingElement || document.documentElement); const { scrollTop } = (document.scrollingElement || document.documentElement);
const { threshold } = this.props; const { threshold, onClick, autoload, autoloadThreshold } = this.props;
if (autoload && scrollTop <= autoloadThreshold) {
onClick();
}
if (scrollTop > threshold) { if (scrollTop > threshold) {
this.setState({ scrolled: true }); this.setState({ scrolled: true });