fix(notes): wait for stacked note element before scrolling on mobile

A single nextTick is not enough for a freshly added stacked note to be
in the DOM, so the mobile scroll target was computed against a null
element. Poll with requestAnimationFrame (mirroring scrollToHashInNote)
and use offsetTop, with an (index + 1) * height fallback.
This commit is contained in:
Julien Calixte
2026-04-29 10:52:07 +02:00
parent adb1bd5945
commit 17f015b686

View File

@@ -47,6 +47,30 @@ export const useRouteQueryStackedNotes = () => {
})
}
const scrollToNoteElement = (
cleanNoteId: string,
index: number,
attempts = 30
) => {
if (attempts <= 0) {
scrollToNote((index + 1) * height.value)
return
}
const element = document.querySelector(
`.note-${cleanNoteId}`
) as HTMLElement | null
if (element) {
scrollToNote(element.offsetTop)
return
}
requestAnimationFrame(() => {
scrollToNoteElement(cleanNoteId, index, attempts - 1)
})
}
type ScrollToFocusedNoteOptions = {
noteId?: string | null
notes?: string[]
@@ -65,13 +89,7 @@ export const useRouteQueryStackedNotes = () => {
if (isMobile.value) {
if (noteId) {
const cleanNoteId = noteId.replaceAll(":", "-")
const element = document.querySelector(
`.note-${cleanNoteId}`
) as HTMLElement
const top = (index + 1) * (element?.clientHeight ?? height.value)
scrollToNote(top)
scrollToNoteElement(noteId.replaceAll(":", "-"), index)
} else {
scrollToNote(0)
}