Skip to content

Commit

Permalink
fix(FloatingPortal): prevent undefined id with unconditional render…
Browse files Browse the repository at this point in the history
…ing in React <18 (#2908)
  • Loading branch information
atomiks committed May 18, 2024
1 parent fec4c04 commit 5c114c2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-buses-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@floating-ui/react": patch
---

fix(FloatingPortal): prevent `undefined` id with unconditional rendering in React <18
5 changes: 5 additions & 0 deletions packages/react/src/components/FloatingPortal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export function useFloatingPortalNode(
}, [portalNode]);

useModernLayoutEffect(() => {
// Wait for the uniqueId to be generated before creating the portal node in
// React <18 (using `useFloatingId` instead of the native `useId`).
// https://github.com/floating-ui/floating-ui/issues/2778
if (!uniqueId) return;
if (portalNodeRef.current) return;
const existingIdRoot = id ? document.getElementById(id) : null;
if (!existingIdRoot) return;
Expand All @@ -79,6 +83,7 @@ export function useFloatingPortalNode(
}, [id, uniqueId]);

useModernLayoutEffect(() => {
if (!uniqueId) return;
if (portalNodeRef.current) return;

let container = root || portalContext?.portalNode;
Expand Down

0 comments on commit 5c114c2

Please sign in to comment.