Skip to content

Commit

Permalink
robust SpeedSearchMenu size measurement
Browse files Browse the repository at this point in the history
In the CI environment, the resize observer behaves differently, and the resize callback is not initially called, leaving the lastSize ref with the default arbitrary value of `{width: 0, height: 0}`.
Not sure what makes useResizeObserver behave differently,
but the code now makes more sense regardless.
  • Loading branch information
alirezamirian committed Oct 2, 2023
1 parent f8a6475 commit f62faf9
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions packages/jui/src/Menu/SpeedSearchMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,38 @@ function useSpeedSearchMenu<T>(
}
});

const lastSize = useRef({ width: 0, height: 0 });
const lastSize = useRef<{ width: number; height: number } | null>(null);
const [size, setSize] = useState<{ width: number; height: number } | null>(
null
);
const measureSize = () => {
const { offsetWidth = 0, offsetHeight = 0 } = containerRef.current || {};
if (offsetWidth > 0 && offsetHeight > 0) {
lastSize.current = {
width: offsetWidth,
height: offsetHeight,
};
}
};
const isSearchActive =
speedSearch.active && speedSearch.searchTerm.length > 0;
useResizeObserver({
ref: containerRef,
onResize: useEventCallback(() => {
if (!isSearchActive) {
lastSize.current = {
width: containerRef.current?.offsetWidth ?? 0,
height: containerRef.current?.offsetHeight ?? 0,
};
measureSize();
}
}),
});
useLayoutEffect(() => {
if (isSearchActive) {
setSize(lastSize.current);
if (lastSize.current) {
setSize(lastSize.current);
}
} else {
if (!lastSize.current) {
measureSize();
}
setSize(null);
}
}, [isSearchActive]);
Expand Down

0 comments on commit f62faf9

Please sign in to comment.