diff --git a/apps/desktop/src/components/find-bar.test.tsx b/apps/desktop/src/components/find-bar.test.tsx index 625aa38d06..fcc107eb66 100644 --- a/apps/desktop/src/components/find-bar.test.tsx +++ b/apps/desktop/src/components/find-bar.test.tsx @@ -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('')) + }) +}) diff --git a/apps/desktop/src/components/find-bar.tsx b/apps/desktop/src/components/find-bar.tsx index 5851fc74ed..bffc6c0d48 100644 --- a/apps/desktop/src/components/find-bar.tsx +++ b/apps/desktop/src/components/find-bar.tsx @@ -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