fix(desktop): re-measure find-bar offset when the files pane toggles or resizes
The salvaged measurement effect only listened to window `resize`, so opening, closing, or drag-resizing the files pane while the find bar was up left the bar in a stale position (either still covering the pane's header or floating mid-window after the pane closed). Harden the measurement path: - ResizeObserver on the aside tracks drag-resizes of the pane rail. - A body-scoped childList MutationObserver catches the pane mounting or unmounting mid-find-session and retargets the ResizeObserver when the aside's identity changes (pane reopened, panes flipped). - All triggers coalesce into a single rAF-batched measure; everything (observers, listener, pending frame) tears down when the bar closes. Also extends find-bar.test.tsx with three positioning tests: default right-4 when the pane is closed, parked left of an open pane, and re-measuring when the pane opens/closes while the bar is up.
This commit is contained in:
@@ -610,3 +610,60 @@ describe('FindBar', () => {
|
||||
expect(bridge.stopFindInPage).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
// ── Files-pane-aware positioning ────────────────────────────────────────────
|
||||
|
||||
describe('FindBar files pane positioning', () => {
|
||||
function mountAside(width = 240) {
|
||||
// eslint-disable-next-line no-restricted-globals -- the component queries the live document for the aside
|
||||
const aside = document.createElement('aside')
|
||||
aside.setAttribute('aria-label', 'Right sidebar')
|
||||
// jsdom has no layout — stub the rect the component measures.
|
||||
aside.getBoundingClientRect = () =>
|
||||
({ left: window.innerWidth - width, width } as DOMRect)
|
||||
// eslint-disable-next-line no-restricted-globals -- must land in the real DOM the component measures
|
||||
document.body.appendChild(aside)
|
||||
|
||||
return aside
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
// eslint-disable-next-line no-restricted-globals -- cleanup of the real DOM the component measures
|
||||
for (const aside of document.querySelectorAll('aside[aria-label="Right sidebar"]')) {
|
||||
aside.remove()
|
||||
}
|
||||
})
|
||||
|
||||
it('keeps the default right-4 position when the pane is closed', async () => {
|
||||
openFindBar()
|
||||
renderFindBar()
|
||||
|
||||
const bar = await screen.findByRole('search')
|
||||
expect(bar.style.right).toBe('')
|
||||
})
|
||||
|
||||
it('parks the bar left of the pane when it is open', async () => {
|
||||
mountAside(240)
|
||||
openFindBar()
|
||||
renderFindBar()
|
||||
|
||||
const bar = await screen.findByRole('search')
|
||||
await waitFor(() => expect(bar.style.right).toBe('calc(240px + 0.75rem)'))
|
||||
})
|
||||
|
||||
it('re-measures when the pane opens or closes while the bar is up', async () => {
|
||||
openFindBar()
|
||||
renderFindBar()
|
||||
|
||||
const bar = await screen.findByRole('search')
|
||||
expect(bar.style.right).toBe('')
|
||||
|
||||
// Pane opens mid-session: the MutationObserver path must re-measure.
|
||||
const aside = mountAside(300)
|
||||
await waitFor(() => expect(bar.style.right).toBe('calc(300px + 0.75rem)'))
|
||||
|
||||
// Pane closes again: the bar falls back to the default position.
|
||||
aside.remove()
|
||||
await waitFor(() => expect(bar.style.right).toBe(''))
|
||||
})
|
||||
})
|
||||
|
||||
@@ -72,6 +72,14 @@ export function FindBar() {
|
||||
// floating right rail. The find bar is `fixed right-4` by default, which
|
||||
// would cover the files pane's header + first rows when it is open. Measure
|
||||
// the pane's live rect and park the bar just left of it instead.
|
||||
//
|
||||
// The pane can open, close, or be drag-resized *while the bar is open* (its
|
||||
// visibility lives in the contrib workspace registry, not a store we can
|
||||
// subscribe to from here), so a window `resize` listener alone is not
|
||||
// enough: a ResizeObserver tracks width drags of the current aside, and a
|
||||
// body-scoped MutationObserver catches the aside mounting/unmounting and
|
||||
// re-attaches the ResizeObserver to the new node. All paths coalesce into
|
||||
// one rAF-batched measure; everything tears down when the bar closes.
|
||||
useEffect(() => {
|
||||
if (!active) {
|
||||
setFilesPaneRight(null)
|
||||
@@ -79,8 +87,30 @@ export function FindBar() {
|
||||
return undefined
|
||||
}
|
||||
|
||||
let rafId: null | number = null
|
||||
let observedAside: Element | null = null
|
||||
|
||||
const resizeObserver =
|
||||
typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(() => scheduleMeasure())
|
||||
|
||||
const measure = () => {
|
||||
rafId = null
|
||||
const aside = document.querySelector('aside[aria-label="Right sidebar"]')
|
||||
|
||||
// The aside can be replaced wholesale (pane closed and reopened, panes
|
||||
// flipped) — retarget the ResizeObserver whenever its identity changes.
|
||||
if (aside !== observedAside) {
|
||||
if (observedAside) {
|
||||
resizeObserver?.unobserve(observedAside)
|
||||
}
|
||||
|
||||
if (aside) {
|
||||
resizeObserver?.observe(aside)
|
||||
}
|
||||
|
||||
observedAside = aside
|
||||
}
|
||||
|
||||
const rect = aside?.getBoundingClientRect()
|
||||
|
||||
if (rect && rect.width > 0 && rect.left < window.innerWidth) {
|
||||
@@ -90,10 +120,28 @@ export function FindBar() {
|
||||
}
|
||||
}
|
||||
|
||||
measure()
|
||||
window.addEventListener('resize', measure)
|
||||
const scheduleMeasure = () => {
|
||||
rafId ??= requestAnimationFrame(measure)
|
||||
}
|
||||
|
||||
return () => window.removeEventListener('resize', measure)
|
||||
// Catches the pane opening/closing while the bar is up. Scoped to
|
||||
// childList mutations only (no attributes/characterData), and the bar is
|
||||
// a transient overlay, so the observer lives only for the find session.
|
||||
const mutationObserver = new MutationObserver(scheduleMeasure)
|
||||
mutationObserver.observe(document.body, { childList: true, subtree: true })
|
||||
|
||||
measure()
|
||||
window.addEventListener('resize', scheduleMeasure)
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('resize', scheduleMeasure)
|
||||
mutationObserver.disconnect()
|
||||
resizeObserver?.disconnect()
|
||||
|
||||
if (rafId != null) {
|
||||
cancelAnimationFrame(rafId)
|
||||
}
|
||||
}
|
||||
}, [active, pathname])
|
||||
|
||||
// Subscribe to found-in-page results from the main process. Refcounted in
|
||||
|
||||
Reference in New Issue
Block a user