fix: Direct links to comments do not always scroll to show visible mark, closes #6231

This commit is contained in:
Tom Moor
2024-01-04 18:52:57 -05:00
parent e82815e1d6
commit 2270340c76

View File

@@ -108,7 +108,7 @@ function CommentThread({
if (focused) {
// If the thread is already visible, scroll it into view immediately,
// otherwise wait for the sidebar to appear.
const isVisible =
const isThreadVisible =
(topRef.current?.getBoundingClientRect().left ?? 0) < window.innerWidth;
setTimeout(
@@ -125,18 +125,22 @@ function CommentThread({
parent.id !== "comments",
});
},
isVisible ? 0 : sidebarAppearDuration
isThreadVisible ? 0 : sidebarAppearDuration
);
setTimeout(() => {
const commentMarkElement = window.document?.getElementById(
`comment-${thread.id}`
);
commentMarkElement?.scrollIntoView({
behavior: "smooth",
block: "center",
});
}, 0);
const getCommentMarkElement = () =>
window.document?.getElementById(`comment-${thread.id}`);
const isMarkVisible = !!getCommentMarkElement();
setTimeout(
() => {
getCommentMarkElement()?.scrollIntoView({
behavior: "smooth",
block: "center",
});
},
isMarkVisible ? 0 : sidebarAppearDuration
);
}
}, [focused, thread.id]);