diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b5ae4d1a8..566cb5c70b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased - React] -- Nothing yet! +### Fixes + +- Ensure portal root exists in the DOM ([#950](https://github.com/tailwindlabs/headlessui/pull/950)) ## [Unreleased - Vue] diff --git a/packages/@headlessui-react/src/components/portal/portal.tsx b/packages/@headlessui-react/src/components/portal/portal.tsx index 24cd3148ad..1c46f51e7a 100644 --- a/packages/@headlessui-react/src/components/portal/portal.tsx +++ b/packages/@headlessui-react/src/components/portal/portal.tsx @@ -34,6 +34,15 @@ function usePortalTarget(): HTMLElement | null { return document.body.appendChild(root) }) + // Ensure the portal root is always in the DOM + useEffect(() => { + if (target === null) return + + if (!document.body.contains(target)) { + document.body.appendChild(target) + } + }, [target]) + useEffect(() => { if (forceInRoot) return if (groupTarget === null) return