Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Wrap toggleScrollListener calls in requestAnimationFrame callback.",
"packageName": "@fluentui/react-positioning",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,21 @@ function useContainerRef(updatePosition: () => void, enabled: boolean) {
Object.assign(container.style, { position: 'fixed', left: 0, top: 0, margin: 0 });
}

toggleScrollListener(container, prevContainer, updatePosition);

updatePosition();
// toggleScrollListener requires computed styles; thus use RAF to prevent forced style reevaluation.
window.requestAnimationFrame(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to use a proper window instance, otherwise requestAnimationFrame may not be triggered properly.

You can get it from useFluent() (it's already used in this component):

const { targetDocument } = useFluent();

targetDocument?.defaultView?.requestAnimationFrame()

toggleScrollListener(container, prevContainer, updatePosition);
updatePosition();
});
});
}

function useTargetRef(updatePosition: () => void) {
return useCallbackRef<HTMLElement | PositioningVirtualElement | null>(null, (target, prevTarget) => {
toggleScrollListener(target, prevTarget, updatePosition);

updatePosition();
// toggleScrollListener requires computed styles; thus use RAF to prevent forced style reevaluation.
window.requestAnimationFrame(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All requestAnimationFrame() calls should be cleaned up on a component to avoid reference/memory leaks or throwing exceptions. Typically it's:

const requestRef = React.useRef();

// ---
requestRef.current = requestAnimationFrame();
// ---

React.useEffect(() => {
  return () => {
    cancelAnimationFrame(requestRef.current);
  };
}, []);

toggleScrollListener(target, prevTarget, updatePosition);
updatePosition();
});
});
}

Expand Down