diff --git a/src/components/molecules/OakTooltip/OakTooltip.tsx b/src/components/molecules/OakTooltip/OakTooltip.tsx index 802a4f41..334cd4ff 100644 --- a/src/components/molecules/OakTooltip/OakTooltip.tsx +++ b/src/components/molecules/OakTooltip/OakTooltip.tsx @@ -42,11 +42,14 @@ export const OakTooltip = ({ children, tooltip, isOpen, - domContainer = document.body, + domContainer, ...props }: OakTooltipProps) => { const [targetElement, setTargetElement] = useState(null); const [isIntersecting, setIsIntersecting] = useState(true); + const [domContainerState, setDomContainerState] = useState< + Element | undefined + >(domContainer); const isVisible = isOpen && isIntersecting; /** @@ -123,39 +126,47 @@ export const OakTooltip = ({ }; }, [targetElement]); + useLayoutEffect(() => { + if (domContainerState) { + return; + } + setDomContainerState(document.body); + }, [domContainerState]); + return ( <> - {createPortal( - isVisible && ( - + {domContainerState && + createPortal( + isVisible && ( - - {tooltip} - + + {tooltip} + + - - ), - domContainer, - )} + ), + domContainerState, + )}
{ setTargetElement(domElement?.firstElementChild ?? null);