Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nav-scroll-page-jump.md
Original file line number Diff line number Diff line change
@@ -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.
28 changes: 28 additions & 0 deletions scripts/browser-check.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 17 additions & 2 deletions src/vue/OpenApiReference.vue
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,22 @@ async function moveCursor(step: number): Promise<void> {
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<HTMLElement>(`#${searchId}-${cursor.value}`)
revealIn(item, item?.closest<HTMLElement>('.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 {
Expand Down Expand Up @@ -355,7 +370,7 @@ function watchSections(): void {
if (!top || top === active.value) return
active.value = top
const link = root.value?.querySelector<HTMLElement>(`.oa-nav [href="#${CSS.escape(top)}"]`)
link?.scrollIntoView({ block: 'nearest' })
revealIn(link, link?.closest<HTMLElement>('.oa-nav__scroll'))
},
{ rootMargin: '-15% 0px -70% 0px' },
)
Expand Down
Loading