From a526a9f6afb1e1c599eacd54799b318af2fe1394 Mon Sep 17 00:00:00 2001 From: Julien Calixte Date: Mon, 4 May 2026 19:57:00 +0200 Subject: [PATCH] fix(scroll): snap to click-time scrollTop before smooth scroll Capture mainApp.scrollTop synchronously when addStackedNote runs and snap the scroll back to that value before scrollIntoView fires, so the smooth scroll begins from where the user actually tapped rather than from a position drifted by momentum or async work. --- src/hooks/useOverlay.hook.ts | 10 +++++++++- src/hooks/useRouteQueryStackedNotes.hook.ts | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/hooks/useOverlay.hook.ts b/src/hooks/useOverlay.hook.ts index 5e8c78c..df2be63 100644 --- a/src/hooks/useOverlay.hook.ts +++ b/src/hooks/useOverlay.hook.ts @@ -41,13 +41,21 @@ export const useOverlay = (listen = true) => { } const scrollToElement = (element: HTMLElement) => { + const mainApp = document.getElementById("main-app") + const clickTop = (window as unknown as { __scrollAtClick?: number }) + .__scrollAtClick + + if (mainApp && clickTop !== undefined) { + mainApp.scrollTop = clickTop + } + requestAnimationFrame(() => { - const mainApp = document.getElementById("main-app") const debug = document.getElementById("scroll-debug") if (debug && mainApp) { const er = element.getBoundingClientRect() const cr = mainApp.getBoundingClientRect() const lines = [ + `clickTop: ${clickTop ?? "n/a"}`, `before scrollTop: ${mainApp.scrollTop}`, `mainApp scrollH: ${mainApp.scrollHeight} clientH: ${mainApp.clientHeight}`, `body scrollY: ${window.scrollY} innerH: ${window.innerHeight}`, diff --git a/src/hooks/useRouteQueryStackedNotes.hook.ts b/src/hooks/useRouteQueryStackedNotes.hook.ts index 695c290..86deb7a 100644 --- a/src/hooks/useRouteQueryStackedNotes.hook.ts +++ b/src/hooks/useRouteQueryStackedNotes.hook.ts @@ -114,6 +114,10 @@ export const useRouteQueryStackedNotes = () => { selector?: string, hash?: string ) => { + const mainAppEl = document.getElementById("main-app") + ;(window as unknown as { __scrollAtClick?: number }).__scrollAtClick = + mainAppEl?.scrollTop ?? 0 + if (stackedNotes.value.includes(sha)) { scrollToFocusedNote({ noteId: selector ?? sha,