From be006f08b47314f00cea3178c80c503a8f92481b Mon Sep 17 00:00:00 2001 From: Julien Calixte Date: Mon, 4 May 2026 18:15:10 +0200 Subject: [PATCH] fix(stacked-note): align mobile scroll target to element rect Replace the (index + 1) * clientHeight math and 80ms setTimeout with a scrollToElement helper that reads getBoundingClientRect inside rAF, so the smooth scroll starts from the user's actual position even when the note is freshly mounted. --- src/hooks/useOverlay.hook.ts | 16 +++++++++++++++- src/hooks/useRouteQueryStackedNotes.hook.ts | 4 ++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/hooks/useOverlay.hook.ts b/src/hooks/useOverlay.hook.ts index b6b29a8..1abba02 100644 --- a/src/hooks/useOverlay.hook.ts +++ b/src/hooks/useOverlay.hook.ts @@ -40,10 +40,24 @@ export const useOverlay = (listen = true) => { }, 80) } + const scrollToElement = (element: HTMLElement) => { + const mainApp = document.getElementById("main-app") + if (!mainApp) return + + requestAnimationFrame(() => { + const top = + element.getBoundingClientRect().top - + mainApp.getBoundingClientRect().top + + mainApp.scrollTop + mainApp.scrollTo({ top, behavior: "smooth" }) + }) + } + return { x, y, isMobile, - scrollToNote + scrollToNote, + scrollToElement } } diff --git a/src/hooks/useRouteQueryStackedNotes.hook.ts b/src/hooks/useRouteQueryStackedNotes.hook.ts index edb959b..695c290 100644 --- a/src/hooks/useRouteQueryStackedNotes.hook.ts +++ b/src/hooks/useRouteQueryStackedNotes.hook.ts @@ -18,7 +18,7 @@ export const useRouteQueryStackedNotes = () => { }) const { height } = useWindowSize() - const { scrollToNote, isMobile } = useOverlay(false) + const { scrollToNote, scrollToElement, isMobile } = useOverlay(false) const scrollToHashInNote = ( cleanSha: string, @@ -57,7 +57,7 @@ export const useRouteQueryStackedNotes = () => { ) as HTMLElement | null if (element) { - scrollToNote((index + 1) * element.clientHeight) + scrollToElement(element) return }