From a7e8223896ad6ca249b75383f4f2d80f89ded3fc Mon Sep 17 00:00:00 2001 From: 7w1 Date: Sun, 8 Mar 2026 16:20:44 -0500 Subject: [PATCH] fix scroll when clicking message links --- .changeset/a_fix_scroll_links.md | 5 +++ src/app/features/room/RoomTimeline.tsx | 53 ++++++++++++-------------- 2 files changed, 30 insertions(+), 28 deletions(-) create mode 100644 .changeset/a_fix_scroll_links.md diff --git a/.changeset/a_fix_scroll_links.md b/.changeset/a_fix_scroll_links.md new file mode 100644 index 00000000..035098f5 --- /dev/null +++ b/.changeset/a_fix_scroll_links.md @@ -0,0 +1,5 @@ +--- +sable: patch +--- + +Fix message links sometimes scrolling to bottom of timeline instead of message + maybe other scroll bugs. diff --git a/src/app/features/room/RoomTimeline.tsx b/src/app/features/room/RoomTimeline.tsx index 4ea606b8..51e155a5 100644 --- a/src/app/features/room/RoomTimeline.tsx +++ b/src/app/features/room/RoomTimeline.tsx @@ -620,9 +620,13 @@ export function RoomTimeline({ } const atBottomAnchorRef = useRef(null); - const [atBottom, setAtBottom] = useState(true); + + const [atBottom, setAtBottomState] = useState(true); const atBottomRef = useRef(atBottom); - atBottomRef.current = atBottom; + const setAtBottom = useCallback((val: boolean) => { + setAtBottomState(val); + atBottomRef.current = val; + }, []); const scrollRef = useRef(null); const scrollToBottomRef = useRef({ @@ -734,6 +738,7 @@ export function RoomTimeline({ if (!alive()) return; const evLength = getTimelinesEventsCount(lTimelines); + setAtBottom(false); setFocusItem({ index: evtAbsIndex, scrollTo: true, @@ -747,7 +752,7 @@ export function RoomTimeline({ }, }); }, - [alive] + [alive, setAtBottom] ), useCallback(() => { if (!alive()) return; @@ -829,6 +834,8 @@ export function RoomTimeline({ evtTimeline && getEventIdAbsoluteIndex(timeline.linkedTimelines, evtTimeline, evtId); if (typeof absoluteIndex === 'number') { + setAtBottom(false); + const scrolled = scrollToItem(absoluteIndex, { behavior: reducedMotion ? 'instant' : 'smooth', align: 'center', @@ -837,14 +844,14 @@ export function RoomTimeline({ if (onScroll) onScroll(scrolled); setFocusItem({ index: absoluteIndex, - scrollTo: false, + scrollTo: !scrolled, highlight, }); } else { loadEventTimeline(evtId); } }, - [room, timeline, scrollToItem, loadEventTimeline, reducedMotion] + [room, timeline, scrollToItem, loadEventTimeline, reducedMotion, setAtBottom] ); useLiveTimelineRefresh( @@ -911,11 +918,15 @@ export function RoomTimeline({ }, [mx, room, hideReads]); const debounceSetAtBottom = useDebounce( - useCallback((entry: IntersectionObserverEntry) => { - if (!entry.isIntersecting) setAtBottom(false); - }, []), + useCallback( + (entry: IntersectionObserverEntry) => { + if (!entry.isIntersecting) setAtBottom(false); + }, + [setAtBottom] + ), { wait: 1000 } ); + useIntersectionObserver( useCallback( (entries) => { @@ -932,7 +943,7 @@ export function RoomTimeline({ } } }, - [debounceSetAtBottom, tryAutoMarkAsRead] + [debounceSetAtBottom, tryAutoMarkAsRead, setAtBottom] ), useCallback( () => ({ @@ -1006,36 +1017,20 @@ export function RoomTimeline({ const contentEl = scrollEl?.firstElementChild as HTMLElement; if (!scrollEl || !contentEl) return () => {}; - let lastScrollTop = scrollEl.scrollTop; - let userIsScrollingUp = false; - - // Track if user is scrolling up - const handleScroll = () => { - if (scrollEl.scrollTop < lastScrollTop) { - userIsScrollingUp = true; - } else if (scrollEl.scrollHeight - scrollEl.scrollTop <= scrollEl.clientHeight + 10) { - userIsScrollingUp = false; - } - lastScrollTop = scrollEl.scrollTop; - }; - const forceScroll = () => { // if the user isn't scrolling jump down to latest content - if (!userIsScrollingUp) { - scrollToBottom(scrollEl, 'instant'); - } + if (!atBottomRef.current) return; + scrollToBottom(scrollEl, 'instant'); }; const resizeObserver = new ResizeObserver(() => { requestAnimationFrame(forceScroll); }); - scrollEl.addEventListener('scroll', handleScroll, { passive: true }); resizeObserver.observe(contentEl); return () => { resizeObserver.disconnect(); - scrollEl.removeEventListener('scroll', handleScroll); }; }, [room]); @@ -1090,7 +1085,9 @@ export function RoomTimeline({ // reach the true bottom (e.g. after images finish loading or the // virtual keyboard shifts the viewport). if (behavior === 'instant') { - setTimeout(() => scrollToBottom(scrollEl, 'instant'), 80); + setTimeout(() => { + scrollToBottom(scrollEl, 'instant'); + }, 80); } } }