Merge pull request #261 from 7w1/fix/scroll-patches

fix scroll when clicking message links
This commit is contained in:
7w1 authored and GitHub committed 2026-03-08 16:27:11 -05:00
commit 6cb66b9882
2 files changed
+30 -28

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
sable: patch
---
Fix message links sometimes scrolling to bottom of timeline instead of message + maybe other scroll bugs.
+25 -28
View File
@@ -620,9 +620,13 @@ export function RoomTimeline({
}
const atBottomAnchorRef = useRef<HTMLElement>(null);
const [atBottom, setAtBottom] = useState<boolean>(true);
const [atBottom, setAtBottomState] = useState<boolean>(true);
const atBottomRef = useRef(atBottom);
atBottomRef.current = atBottom;
const setAtBottom = useCallback((val: boolean) => {
setAtBottomState(val);
atBottomRef.current = val;
}, []);
const scrollRef = useRef<HTMLDivElement>(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);
}
}
}