From d8536709b7d6093de62698b5f0dd3da5bece2e8c Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Sat, 15 Aug 2026 01:56:43 -0700 Subject: [PATCH] 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. --- apps/desktop/src/components/find-bar.test.tsx | 57 +++++++++++++++++++ apps/desktop/src/components/find-bar.tsx | 54 +++++++++++++++++- 2 files changed, 108 insertions(+), 3 deletions(-) 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