Skip to content

Commit 47a61f2

Browse files
authored
Regression: Fix scroll to bottom (RocketChat#21731)
1 parent 7f33b8a commit 47a61f2

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

app/ui/client/views/app/room.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,10 @@ Meteor.startup(() => {
782782
callbacks.remove('streamNewMessage', this.data._id);
783783
});
784784

785+
const isAtBottom = function(element, scrollThreshold = 0) {
786+
return element.scrollTop + scrollThreshold >= element.scrollHeight - element.clientHeight;
787+
};
788+
785789
Template.roomOld.onRendered(function() {
786790
const { _id: rid } = this.data;
787791

@@ -794,16 +798,16 @@ Meteor.startup(() => {
794798
const store = NewRoomManager.getStore(rid);
795799

796800
const afterMessageGroup = () => {
797-
if (store.scroll) {
801+
if (store.scroll && !store.atBottom) {
798802
wrapper.scrollTop = store.scroll;
799803
} else {
800804
this.sendToBottom();
801805
}
802806
wrapper.removeEventListener('MessageGroup', afterMessageGroup);
803807

804808
wrapper.addEventListener('scroll', _.throttle(() => {
805-
store.update({ scroll: wrapper.scrollTop });
806-
}, 100));
809+
store.update({ scroll: wrapper.scrollTop, atBottom: isAtBottom(wrapper, 50) });
810+
}, 30));
807811
};
808812

809813
wrapper.addEventListener('MessageGroup', afterMessageGroup);
@@ -820,7 +824,7 @@ Meteor.startup(() => {
820824
const messageBox = $('.messages-box');
821825

822826
template.isAtBottom = function(scrollThreshold = 0) {
823-
if (wrapper.scrollTop + scrollThreshold >= wrapper.scrollHeight - wrapper.clientHeight) {
827+
if (isAtBottom(wrapper, scrollThreshold)) {
824828
newMessage.className = 'new-message background-primary-action-color color-content-background-color not';
825829
return true;
826830
}
@@ -857,7 +861,6 @@ Meteor.startup(() => {
857861
});
858862

859863
Tracker.afterFlush(() => {
860-
template.sendToBottomIfNecessary();
861864
wrapper.addEventListener('scroll', wheelHandler);
862865
});
863866

client/lib/RoomManager.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,33 @@ export class RoomStore extends Emitter<{
1717

1818
scroll?: number;
1919

20+
atBottom = true;
21+
2022
constructor(readonly rid: string) {
2123
super();
2224

2325
debug && this.on('changed', () => console.log(`RoomStore ${this.rid} changed`, this));
2426
}
2527

26-
update({ scroll, lastTime }: { scroll?: number; lastTime?: Date }): void {
28+
update({
29+
scroll,
30+
lastTime,
31+
atBottom,
32+
}: {
33+
scroll?: number;
34+
lastTime?: Date;
35+
atBottom?: boolean;
36+
}): void {
2737
if (scroll !== undefined) {
2838
this.scroll = scroll;
2939
}
3040
if (lastTime !== undefined) {
3141
this.lastTime = lastTime;
3242
}
43+
44+
if (atBottom !== undefined) {
45+
this.atBottom = atBottom;
46+
}
3347
if (scroll || lastTime) {
3448
this.emit('changed');
3549
}

0 commit comments

Comments
 (0)