Skip to content

Commit

Permalink
fix(table): avoid unnecessary re-rendering if TableRowMenu doesn't ex…
Browse files Browse the repository at this point in the history
…ist (#2630)
  • Loading branch information
rivka-ungar authored Dec 8, 2024
1 parent b0033f0 commit e8d093e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const TableRowMenu = forwardRef(
style={{ top: menuButtonPosition }}
onMouseEnter={onMouseOverRowMenu}
onMouseLeave={onMouseLeaveRowMenu}
data-row-menu-id={rowId}
>
<MenuButton
id={id}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const TableRowMenuProvider = ({ value, children }: TableRowMenuProviderPr
const { tableRootRef, hoveredRowRef, isMenuOpen, resetHoveredRow, setHoveredRowRef, setIsMenuOpen } = value;
const rowMouseLeaveTimeoutId = useRef<ReturnType<typeof setTimeout>>(null);
const [menuButtonPosition, setMenuButtonPosition] = useState(0);
const hasMenuRef = useRef<boolean | null>(null);

const resetTimeout = useCallback(() => {
clearTimeout(rowMouseLeaveTimeoutId.current);
Expand All @@ -15,12 +16,18 @@ export const TableRowMenuProvider = ({ value, children }: TableRowMenuProviderPr

const onMouseOverRow = useCallback(
(rowRef: React.RefObject<HTMLDivElement>) => {
if (isMenuOpen || hoveredRowRef?.current === rowRef.current) return;
if (hasMenuRef.current === false || isMenuOpen || hoveredRowRef?.current === rowRef.current) return;

resetTimeout();
setHoveredRowRef(rowRef);
const tableRootTop = tableRootRef.current.getBoundingClientRect().top;
const rowTop = rowRef.current.getBoundingClientRect().top;
setMenuButtonPosition(rowTop - tableRootTop);
if (hasMenuRef.current === null) {
requestAnimationFrame(() => {
hasMenuRef.current = !!document?.querySelector(`[data-row-menu-id]`);
});
}
},
[isMenuOpen, hoveredRowRef, resetTimeout, setHoveredRowRef, tableRootRef]
);
Expand Down

0 comments on commit e8d093e

Please sign in to comment.