Skip to content

Commit 38be8ad

Browse files
committed
fix(tooltip): rendering in react v18
- manually append anchor on render - limit calls to getOrCreateNode - upgrade to latest version of @popperjs/core
1 parent 624f43a commit 38be8ad

2 files changed

Lines changed: 15 additions & 16 deletions

File tree

packages/charts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"typecheck": "tsc -p ./tsconfig.src.json --noEmit && tsc -p ./tsconfig.nocomments.json --noEmit"
3333
},
3434
"dependencies": {
35-
"@popperjs/core": "^2.4.0",
35+
"@popperjs/core": "^2.11.8",
3636
"bezier-easing": "^2.1.0",
3737
"chroma-js": "^2.1.0",
3838
"classnames": "^2.2.6",

packages/charts/src/components/portal/tooltip_portal.tsx

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,17 @@ const TooltipPortalComponent = ({
9393
// eslint-disable-next-line react-hooks/exhaustive-deps
9494
}, [(anchor as PortalAnchorRef)?.current ?? (anchor as PositionedPortalAnchorRef)?.appendRef?.current]);
9595

96+
const portalNode = useMemo(() => {
97+
return getOrCreateNode(`echTooltipPortal${scope}__${chartId}`, 'echTooltipPortal__invisible', undefined, zIndex);
98+
}, [chartId, scope, zIndex]);
99+
96100
/**
97101
* This must not be removed from DOM throughout life of this component.
98102
* Otherwise the portal will loose reference to the correct node.
99103
*/
100-
const portalNodeElement = getOrCreateNode(
101-
`echTooltipPortal${scope}__${chartId}`,
102-
'echTooltipPortal__invisible',
103-
undefined,
104-
zIndex,
105-
);
106-
107-
const portalNode = useRef(portalNodeElement);
104+
useEffect(() => {
105+
document.body.appendChild(portalNode);
106+
});
108107

109108
/**
110109
* Popper instance used to manage position of tooltip.
@@ -126,7 +125,7 @@ const TooltipPortalComponent = ({
126125
if (!visible) return;
127126

128127
const { fallbackPlacements, placement, boundary, offset, boundaryPadding } = popperSettings;
129-
popper.current = createPopper(anchorNode, portalNode.current, {
128+
popper.current = createPopper(anchorNode, portalNode, {
130129
strategy: 'absolute',
131130
placement,
132131
modifiers: [
@@ -178,7 +177,7 @@ const TooltipPortalComponent = ({
178177

179178
useEffect(() => {
180179
setPopper();
181-
const nodeCopy = portalNode.current;
180+
const nodeCopy = portalNode;
182181

183182
return () => {
184183
if (nodeCopy.parentNode) {
@@ -223,20 +222,20 @@ const TooltipPortalComponent = ({
223222

224223
useEffect(() => {
225224
if (!position && !skipPositioning) {
226-
portalNode.current.classList.add('echTooltipPortal__invisible');
225+
portalNode.classList.add('echTooltipPortal__invisible');
227226
return;
228227
}
229-
portalNode.current.classList.remove('echTooltipPortal__invisible');
230-
}, [position, skipPositioning]);
228+
portalNode.classList.remove('echTooltipPortal__invisible');
229+
}, [portalNode.classList, position, skipPositioning]);
231230

232231
useEffect(() => {
233232
if (popper.current) {
234233
updateAnchorDimensions();
235234
void popper.current.update();
236235
}
237-
}, [updateAnchorDimensions, popper]);
236+
}, [updateAnchorDimensions]);
238237

239-
return createPortal(children, portalNode.current);
238+
return createPortal(children, portalNode, 'ech-tooltip-portal');
240239
};
241240

242241
TooltipPortalComponent.displayName = 'TooltipPortal';

0 commit comments

Comments
 (0)