Skip to content

Commit 1d8fa0b

Browse files
fix: wrong scroll position when switching DMs (RocketChat#36662)
1 parent 17bca96 commit 1d8fa0b

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@rocket.chat/meteor": patch
3+
---
4+
5+
Fixes scroll issue when moving between channels or DMs

apps/meteor/client/views/room/body/hooks/useRestoreScrollPosition.spec.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ describe('useRestoreScrollPosition', () => {
3636
expect(screen.getByTestId('scrollable-element')).toHaveProperty('scrollTop', 123);
3737
});
3838

39+
it('should jump to bottom if atBottom is true', () => {
40+
const store = {
41+
scroll: 123,
42+
atBottom: true,
43+
update: jest.fn(),
44+
};
45+
(RoomManager.getStore as jest.Mock).mockReturnValue(store);
46+
47+
const Test = () => {
48+
const { innerRef } = useRestoreScrollPosition('GENERAL');
49+
return (
50+
<div ref={innerRef} style={{ height: '100px', overflowY: 'scroll' }} data-testid='scrollable-element'>
51+
<div style={{ height: '800px' }}></div>
52+
</div>
53+
);
54+
};
55+
56+
render(<Test />);
57+
58+
expect(screen.getByTestId('scrollable-element')).toBeInTheDocument();
59+
expect(screen.getByTestId('scrollable-element')).toHaveStyle({ height: '100px', overflowY: 'scroll' });
60+
expect(screen.getByTestId('scrollable-element')).toHaveProperty('scrollTop', 0);
61+
});
62+
3963
it('should do nothing if no previous scroll position is stored', () => {
4064
const store = {
4165
scroll: undefined,

apps/meteor/client/views/room/body/hooks/useRestoreScrollPosition.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export function useRestoreScrollPosition(rid: string, wait = 100) {
1414
return;
1515
}
1616
const store = RoomManager.getStore(rid);
17+
if (store?.atBottom) {
18+
node.scrollTop = node.scrollHeight;
19+
node.scrollLeft = 30;
20+
}
1721
if (!jumpToRef.current && store?.scroll !== undefined && !store.atBottom) {
1822
node.scrollTop = store.scroll;
1923
node.scrollLeft = 30;

0 commit comments

Comments
 (0)