Skip to content

Commit 12d1ba6

Browse files
author
Jaap
committed
add check to see if user is close to top
1 parent c010559 commit 12d1ba6

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/components/InfiniteScrollPaginator/InfiniteScroll.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,24 @@ const InfiniteScroll = ({
7878
el.scrollHeight - parentElement.scrollTop - parentElement.clientHeight;
7979
reverseOffset = parentElement.scrollTop;
8080
}
81-
if (listenToScroll) listenToScroll(offset, reverseOffset);
81+
if (listenToScroll) {
82+
listenToScroll(offset, reverseOffset, threshold);
83+
}
8284

8385
// Here we make sure the element is visible as well as checking the offset
8486
if (
8587
(isReverse ? reverseOffset : offset) < Number(threshold) &&
8688
el.offsetParent !== null &&
87-
typeof loadMore === 'function'
89+
typeof loadMore === 'function' &&
90+
hasMore
8891
) {
8992
loadMore();
9093
}
91-
}, [useWindow, isReverse, threshold, listenToScroll, loadMore]);
94+
}, [hasMore, useWindow, isReverse, threshold, listenToScroll, loadMore]);
9295

9396
useEffect(() => {
9497
const scrollEl = useWindow ? window : scrollComponent.current?.parentNode;
95-
if (!hasMore || isLoading || !scrollEl) {
98+
if (isLoading || !scrollEl) {
9699
return () => {};
97100
}
98101

@@ -107,7 +110,7 @@ const InfiniteScroll = ({
107110
scrollEl.removeEventListener('scroll', scrollListener, useCapture);
108111
scrollEl.removeEventListener('resize', scrollListener, useCapture);
109112
};
110-
}, [hasMore, initialLoad, isLoading, scrollListener, useCapture, useWindow]);
113+
}, [initialLoad, isLoading, scrollListener, useCapture, useWindow]);
111114

112115
useEffect(() => {
113116
const scrollEl = useWindow ? window : scrollComponent.current?.parentNode;

src/components/MessageList/MessageList.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,11 @@ class MessageList extends PureComponent {
165165
this.setState({ newMessagesNotification: false });
166166
};
167167

168-
userScrolledUp = () => this.scrollOffset > 310;
168+
userScrolledUp = () => this.scrollOffset > 200;
169169

170-
listenToScroll = (offset) => {
170+
listenToScroll = (offset, reverseOffset, threshold) => {
171171
this.scrollOffset = offset;
172+
this.closeToTop = reverseOffset < threshold;
172173
if (this.state.newMessagesNotification && !this.userScrolledUp()) {
173174
this.setState({ newMessagesNotification: false });
174175
}
@@ -206,7 +207,7 @@ class MessageList extends PureComponent {
206207
onMessageLoadCaptured = () => {
207208
// A load event (emitted by e.g. an <img>) was captured on a message.
208209
// In some cases, the loaded asset is larger than the placeholder, which means we have to scroll down.
209-
if (!this.userScrolledUp()) {
210+
if (!this.userScrolledUp() && !this.closeToTop) {
210211
this.scrollToBottom();
211212
}
212213
};

0 commit comments

Comments
 (0)