diff --git a/src/app/features/room/RoomTimeline.tsx b/src/app/features/room/RoomTimeline.tsx index 661b225a5..2d680610d 100644 --- a/src/app/features/room/RoomTimeline.tsx +++ b/src/app/features/room/RoomTimeline.tsx @@ -2271,6 +2271,45 @@ export function RoomTimeline({ } } + const prevBackwardStatus = useRef(backwardStatus); + const scrollAnchor = useRef<{ id: string; offsetTop: number } | null>(null); + + useLayoutEffect(() => { + const scrollEl = scrollRef.current; + if (!scrollEl) return; + + // When going to loading store the exact position + if (prevBackwardStatus.current === 'idle' && backwardStatus === 'loading') { + const topMessage = scrollEl.querySelector('[data-message-id]'); + if (topMessage) { + scrollAnchor.current = { + id: topMessage.getAttribute('data-message-id') || '', + offsetTop: (topMessage as HTMLElement).offsetTop, + }; + } + } + + // After fetch finishes calculate new content and force scroll to position hopefully + if (prevBackwardStatus.current === 'loading' && backwardStatus === 'idle') { + if (scrollAnchor.current) { + const anchorNode = scrollEl.querySelector( + `[data-message-id="${scrollAnchor.current.id}"]` + ) as HTMLElement; + + if (anchorNode) { + const delta = anchorNode.offsetTop - scrollAnchor.current.offsetTop; + + if (delta > 0) { + scrollEl.scrollBy({ top: delta, behavior: 'instant' }); + } + } + scrollAnchor.current = null; + } + } + + prevBackwardStatus.current = backwardStatus; + }, [backwardStatus, processedEvents]); + return ( {unreadInfo?.readUptoEventId && !unreadInfo?.inLiveTimeline && (