From 2270340c76a24fcd2f4c5a1c0f7df023e5d25faf Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 4 Jan 2024 18:52:57 -0500 Subject: [PATCH] fix: Direct links to comments do not always scroll to show visible mark, closes #6231 --- .../Document/components/CommentThread.tsx | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/app/scenes/Document/components/CommentThread.tsx b/app/scenes/Document/components/CommentThread.tsx index ca42f4df3..1bfc741fa 100644 --- a/app/scenes/Document/components/CommentThread.tsx +++ b/app/scenes/Document/components/CommentThread.tsx @@ -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]);