Skip to content

Commit

Permalink
fix(tooltip): dynamically set domelement
Browse files Browse the repository at this point in the history
  • Loading branch information
benprotheroe committed Apr 18, 2024
1 parent 82d847a commit 9ee7f21
Showing 1 changed file with 39 additions and 28 deletions.
67 changes: 39 additions & 28 deletions src/components/molecules/OakTooltip/OakTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ export const OakTooltip = ({
children,
tooltip,
isOpen,
domContainer = document.body,
domContainer,
...props
}: OakTooltipProps) => {
const [targetElement, setTargetElement] = useState<Element | null>(null);
const [isIntersecting, setIsIntersecting] = useState(true);
const [domContainerState, setDomContainerState] = useState<
Element | undefined
>(domContainer);
const isVisible = isOpen && isIntersecting;

/**
Expand Down Expand Up @@ -123,39 +126,47 @@ export const OakTooltip = ({
};
}, [targetElement]);

useLayoutEffect(() => {
if (domContainerState) {
return;
}
setDomContainerState(document.body);
}, [domContainerState]);

return (
<>
{createPortal(
isVisible && (
<OakBox
$position="fixed"
style={overlayStyle}
$pointerEvents="none"
$zIndex="modal-dialog"
>
{domContainerState &&
createPortal(
isVisible && (
<OakBox
$width="fit-content"
$height="fit-content"
$position="absolute"
{...getTooltipPositionProps(tooltipPosition)}
$position="fixed"
style={overlayStyle}
$pointerEvents="none"
$zIndex="modal-dialog"
>
<InternalTooltip
$background="bg-decorative5-main"
$color="text-primary"
$pv="inner-padding-m"
$ph="inner-padding-xl"
$font="heading-light-7"
tooltipPosition={tooltipPosition}
{...props}
{...borderRadiusProps}
<OakBox
$width="fit-content"
$height="fit-content"
$position="absolute"
{...getTooltipPositionProps(tooltipPosition)}
>
{tooltip}
</InternalTooltip>
<InternalTooltip
$background="bg-decorative5-main"
$color="text-primary"
$pv="inner-padding-m"
$ph="inner-padding-xl"
$font="heading-light-7"
tooltipPosition={tooltipPosition}
{...props}
{...borderRadiusProps}
>
{tooltip}
</InternalTooltip>
</OakBox>
</OakBox>
</OakBox>
),
domContainer,
)}
),
domContainerState,
)}
<div
ref={(domElement) => {
setTargetElement(domElement?.firstElementChild ?? null);
Expand Down

0 comments on commit 9ee7f21

Please sign in to comment.