Skip to content

Commit

Permalink
Render Tooltip via React Portal (#350)
Browse files Browse the repository at this point in the history
* fix: correct tooltip rendering

render tooltip via react portal - new prop `container`

closes issue #349

* fix: remove `container` prop

remove new `container` prop and render tooltip body in any case via react portal directly to document.body with a z-index of 1300
  • Loading branch information
jahe authored Aug 1, 2023
1 parent 6c8a17b commit 567af48
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as React from 'react';
import styled, { keyframes } from 'styled-components';
import { createPortal } from 'react-dom';
import { usePopper } from 'react-popper';
import { Placement } from '@popperjs/core/lib/enums';
import { variant } from 'styled-system';

import type { PropsWithChildren } from 'react';

import { MediaQueries, SemanticColors } from '../../essentials';
import { Elevation, MediaQueries, SemanticColors } from '../../essentials';
import { get } from '../../utils/themeGet';
import { Text } from '../Text/Text';
import { mapPlacementWithDeprecationWarning, TooltipPlacement } from './TooltipPlacement';
Expand Down Expand Up @@ -89,6 +90,7 @@ interface TooltipBodyProps {

const TooltipBody = styled.div<TooltipBodyProps>`
position: relative;
z-index: ${Elevation.TOOLTIP};
background-color: ${p =>
p.inverted ? SemanticColors.background.secondary : SemanticColors.background.primaryEmphasized};
padding: 0.25rem 0.5rem;
Expand Down Expand Up @@ -197,17 +199,20 @@ const Tooltip: React.FC<PropsWithChildren<TooltipProps>> = ({
onMouseOut: () => handleVisibilityChange(false),
ref: setTriggerReference
})}
{content && isVisible && (
<TooltipBody
ref={setContentReference}
inverted={inverted}
style={{ ...styles.popper }}
variant={attributes.popper?.['data-popper-placement']}
{...attributes.popper}
>
{dynamicContent}
</TooltipBody>
)}
{content &&
isVisible &&
createPortal(
<TooltipBody
ref={setContentReference}
inverted={inverted}
style={{ ...styles.popper }}
variant={attributes.popper?.['data-popper-placement']}
{...attributes.popper}
>
{dynamicContent}
</TooltipBody>,
document.body
)}
</>
);
};
Expand Down

0 comments on commit 567af48

Please sign in to comment.