Skip to content

Commit

Permalink
Merge pull request #147 from oaknational/fix/tooltip-document-bug
Browse files Browse the repository at this point in the history
fix(tooltip): dynamically set domelement
  • Loading branch information
benprotheroe authored Apr 18, 2024
2 parents 82d847a + 9ee7f21 commit 8c7acbe
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 8c7acbe

Please sign in to comment.