Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([issue-720]): address codex review — keep content-change re-measure synchronous

[._.]/ Adam Eivyc4422fce77a9
diff --git a/client/src/components/media/CollectionPickerShell.jsx b/client/src/components/media/CollectionPickerShell.jsx
index 3e25cd0c0a..f8cc441f20 100644
--- a/client/src/components/media/CollectionPickerShell.jsx
+++ b/client/src/components/media/CollectionPickerShell.jsx
@@ -60,8 +60,19 @@ export default function CollectionPickerShell({
const {
popoverRef: menuRef,
style,
- reposition,
- } = usePopoverPosition({ open, width, minWidth, gap: MENU_GAP, position: 'above', anchorRef });
+ } = usePopoverPosition({
+ open,
+ width,
+ minWidth,
+ gap: MENU_GAP,
+ position: 'above',
+ anchorRef,
+ // The popover height changes as the user filters/searches or the list loads;
+ // the hook re-measures synchronously (pre-paint) when these change. `filtered`
+ // is a pure function of these plus `excludeId`, so they cover its height
+ // effect without forward-referencing it.
+ contentDeps: [collectionsState, query, excludeId],
+ });
// Parents may pass inline arrow handlers — read through a ref so the
// event-listener effect doesn't tear down on every parent render.
@@ -104,14 +115,6 @@ export default function CollectionPickerShell({
return q ? base.filter((c) => c.name.toLowerCase().includes(q)) : base;
}, [collectionsState, query, excludeId]);
- // The popover height changes as the user filters/searches or the list loads,
- // so re-measure on those content changes (the hook already re-flows on
- // scroll/resize). reposition is stable while the anchor/sizing props hold.
- useEffect(() => {
- if (!open) return;
- reposition();
- }, [open, reposition, filtered, query, collectionsState]);
-
// Close on outside-click / Escape — placement and scroll/resize reflow are
// owned by usePopoverPosition; this effect only handles dismissal.
useEffect(() => {
diff --git a/client/src/hooks/usePopoverPosition.js b/client/src/hooks/usePopoverPosition.js
index 9a33444de6..708a713e96 100644
--- a/client/src/hooks/usePopoverPosition.js
+++ b/client/src/hooks/usePopoverPosition.js
@@ -23,6 +23,12 @@ import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react
// - reposition → force a re-measure (e.g. after the popover's content height
// changes from filtering/search) without waiting for a scroll/resize event.
//
+// `contentDeps` is a dependency array of values whose change alters the
+// popover's rendered height (search query, filtered list, load state). The
+// hook re-measures synchronously (in useLayoutEffect, before paint) whenever
+// any of them change, so a content-height change can't paint one frame at the
+// stale position before correcting.
+//
// Placement: `position: 'above'` prefers opening above the trigger and flips
// below only when there isn't room above; `'below'` does the inverse. Both
// clamp into the viewport with VIEWPORT_PADDING on every edge.
@@ -43,6 +49,7 @@ export default function usePopoverPosition({
gap = 8,
position = 'above',
anchorRef = null,
+ contentDeps = [],
} = {}) {
const ownTriggerRef = useRef(null);
// Follow a parent-owned anchor when one is supplied; otherwise place this
@@ -95,15 +102,19 @@ export default function usePopoverPosition({
});
}, [triggerRef, width, minWidth, gap, position]);
- // Measure synchronously on open so the popover paints in place; clear on close
- // so the next open re-measures (and renders hidden until it does).
+ // Measure synchronously on open (and on any content-height change) so the
+ // popover paints in place; clear on close so the next open re-measures (and
+ // renders hidden until it does). The content-change re-measure stays in
+ // useLayoutEffect — moving it to useEffect would paint one frame at the stale
+ // height before correcting when the rendered content grows/shrinks.
useLayoutEffect(() => {
if (!open) {
setStyle(null);
return;
}
reposition();
- }, [open, reposition]);
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [open, reposition, ...contentDeps]);
useEffect(() => {
if (!open) return undefined;
diff --git a/client/src/hooks/usePopoverPosition.test.jsx b/client/src/hooks/usePopoverPosition.test.jsx
index 4d9dd60cd6..1ccb32af62 100644
--- a/client/src/hooks/usePopoverPosition.test.jsx
+++ b/client/src/hooks/usePopoverPosition.test.jsx
@@ -120,6 +120,29 @@ describe('usePopoverPosition', () => {
expect(result.current.style.top).toBe('194px');
});
+ it('re-measures synchronously when a contentDep changes', () => {
+ const popover = fakeEl({ height: 200, width: 288 });
+ const { result, rerender } = renderHook(
+ ({ dep }) => usePopoverPosition({ open: true, width: 288, gap: 8, contentDeps: [dep] }),
+ { initialProps: { dep: 'a' } },
+ );
+ act(() => {
+ result.current.triggerRef.current = fakeEl({ top: 400, bottom: 430, left: 500, right: 600, height: 30 });
+ result.current.popoverRef.current = popover;
+ result.current.reposition();
+ });
+ // top = 400 - 200 - 8 = 192.
+ expect(result.current.style.top).toBe('192px');
+ // The rendered content grew taller; change the dep and let the hook's
+ // layout effect re-measure off the new popover height (300).
+ act(() => {
+ popover.getBoundingClientRect = () => ({ height: 300, width: 288 });
+ rerender({ dep: 'b' });
+ });
+ // top = 400 - 300 - 8 = 92.
+ expect(result.current.style.top).toBe('92px');
+ });
+
it('re-measures on window resize while open', () => {
const { result } = renderHook(() => usePopoverPosition({ open: true, width: 288, gap: 8 }));
act(() => {