Skip to content

Commit 13e76f0

Browse files
authored
Prevent render loop in Popper (radix-ui#3614)
1 parent 1aec62a commit 13e76f0

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

.changeset/rude-toys-think.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@radix-ui/react-popper': patch
3+
---
4+
5+
Prevent render loop in Popper

packages/react/popper/src/popper.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,16 @@ const PopperAnchor = React.forwardRef<PopperAnchorElement, PopperAnchorProps>(
7777
const ref = React.useRef<PopperAnchorElement>(null);
7878
const composedRefs = useComposedRefs(forwardedRef, ref);
7979

80+
const anchorRef = React.useRef<Measurable | null>(null);
8081
React.useEffect(() => {
81-
// Consumer can anchor the popper to something that isn't
82-
// a DOM node e.g. pointer position, so we override the
83-
// `anchorRef` with their virtual ref in this case.
84-
context.onAnchorChange(virtualRef?.current || ref.current);
82+
const previousAnchor = anchorRef.current;
83+
anchorRef.current = virtualRef?.current || ref.current;
84+
if (previousAnchor !== anchorRef.current) {
85+
// Consumer can anchor the popper to something that isn't
86+
// a DOM node e.g. pointer position, so we override the
87+
// `anchorRef` with their virtual ref in this case.
88+
context.onAnchorChange(anchorRef.current);
89+
}
8590
});
8691

8792
return virtualRef ? null : <Primitive.div {...anchorProps} ref={composedRefs} />;

0 commit comments

Comments
 (0)