From b7f42ec84bb5c2a3399e80d82e40058b0914811a Mon Sep 17 00:00:00 2001 From: Denis Skripchenko Date: Wed, 28 Oct 2026 17:17:32 +0300 Subject: [PATCH] fix: navigation follows sections without scrolling the host page Co-Authored-By: Claude Opus 5.5 --- .changeset/nav-scroll-page-jump.md | 5 +++++ scripts/browser-check.mjs | 28 ++++++++++++++++++++++++++++ src/vue/OpenApiReference.vue | 19 +++++++++++++++++-- 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 .changeset/nav-scroll-page-jump.md diff --git a/.changeset/nav-scroll-page-jump.md b/.changeset/nav-scroll-page-jump.md new file mode 100644 index 0000000..783befe --- /dev/null +++ b/.changeset/nav-scroll-page-jump.md @@ -0,0 +1,5 @@ +--- +'@dskripchenko/openapi': patch +--- + +**An embedded reference no longer pulls the page while it scrolls.** The navigation followed the section in view with `scrollIntoView`, which scrolls every scrolling ancestor, the host page among them: in a box on a longer page (a wiki macro, an article) whose top had scrolled past the window, each new section pulled the page back to show its link, and scrolling jerked back and forth. The navigation and the search hits now scroll only their own list. diff --git a/scripts/browser-check.mjs b/scripts/browser-check.mjs index dc0f64c..a619ddd 100644 --- a/scripts/browser-check.mjs +++ b/scripts/browser-check.mjs @@ -1187,6 +1187,34 @@ try { await cq.waitForTimeout(300) check('container: picking an item closes the drawer', (await cq.locator('.oa-nav.is-open').count()) === 0) + // Embedded in a longer page (a wiki macro): scrolling the reference moves its + // navigation, never the page around it. + const host = await browser.newPage({ viewport: { width: 1440, height: 800 } }) + await host.goto(`${base}?spec=kitchen&box=1300&boxHeight=640`) + await host.waitForSelector('.oa-op__doc') + await host.evaluate(() => { + const pad = () => Object.assign(document.createElement('div'), { style: 'height: 1200px' }) + const box = document.querySelector('.playground-box') + // As the embed sets it: the sticky navigation fills the box. + box.style.setProperty('--oa-viewport-h', '640px') + box.before(pad()) + box.after(pad()) + // The box's top above the window, as when the reader scrolls past it. + window.scrollTo(0, 1500) + }) + const hostY = await host.evaluate(() => window.scrollY) + const moved = [] + for (let i = 1; i <= 12; i += 1) { + await host.evaluate((y) => (document.querySelector('.playground-box').scrollTop = y), i * 400) + await host.waitForTimeout(120) + const y = await host.evaluate(() => window.scrollY) + if (y !== hostY) moved.push(y) + } + const followed = await host.evaluate(() => document.querySelector('.oa-nav .is-active')?.textContent?.trim()) + check('embedded: scrolling the reference keeps the page in place', moved.length === 0, `page at ${hostY}, moved to ${moved.join(', ')}`) + check('embedded: the navigation still follows the section in view', !!followed && !/List orders/.test(followed), followed) + await host.close() + // A small spec renders whole, even opened while off-screen (the embed's lazy two-spec reference). await cq.goto(`${base}?multi=1&box=1100&spacer=3000`) await cq.waitForFunction(() => !!window.__reference) diff --git a/src/vue/OpenApiReference.vue b/src/vue/OpenApiReference.vue index 3005f5f..c5ab174 100644 --- a/src/vue/OpenApiReference.vue +++ b/src/vue/OpenApiReference.vue @@ -196,7 +196,22 @@ async function moveCursor(step: number): Promise { resultsOpen.value = true cursor.value = (cursor.value + step + hits.value.length) % hits.value.length await nextTick() - root.value?.querySelector(`#${searchId}-${cursor.value}`)?.scrollIntoView({ block: 'nearest' }) + const item = root.value?.querySelector(`#${searchId}-${cursor.value}`) + revealIn(item, item?.closest('.oa-search__results')) +} + +/** + * Brings an item into view inside its own scrolling list. Not + * scrollIntoView(): that scrolls every scrolling ancestor too, the page + * among them, and a reference embedded in a longer page would pull the + * page back to its navigation while the reader scrolls. + */ +function revealIn(item: HTMLElement | null | undefined, list: HTMLElement | null | undefined): void { + if (!item || !list) return + const i = item.getBoundingClientRect() + const l = list.getBoundingClientRect() + if (i.top < l.top) list.scrollTop -= l.top - i.top + else if (i.bottom > l.bottom) list.scrollTop += i.bottom - l.bottom } function onSearchKey(ev: KeyboardEvent): void { @@ -355,7 +370,7 @@ function watchSections(): void { if (!top || top === active.value) return active.value = top const link = root.value?.querySelector(`.oa-nav [href="#${CSS.escape(top)}"]`) - link?.scrollIntoView({ block: 'nearest' }) + revealIn(link, link?.closest('.oa-nav__scroll')) }, { rootMargin: '-15% 0px -70% 0px' }, )