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 }