From fb0fe332ee05c5ee6bee66b9b401106f96066b1f Mon Sep 17 00:00:00 2001 From: Eivind Fjeldstad Date: Tue, 8 Mar 2022 17:08:19 +0100 Subject: [PATCH] fix(Tooltip): fix premature removal of portal and prevent zombies --- .../src/components/tooltip/TooltipPortal.js | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/packages/dnb-eufemia/src/components/tooltip/TooltipPortal.js b/packages/dnb-eufemia/src/components/tooltip/TooltipPortal.js index 2aeac4bd65b..3fe69bc3c69 100644 --- a/packages/dnb-eufemia/src/components/tooltip/TooltipPortal.js +++ b/packages/dnb-eufemia/src/components/tooltip/TooltipPortal.js @@ -39,9 +39,12 @@ export default class TooltipPortal extends React.PureComponent { init = () => { const { group, active } = this.props - tooltipPortal[group] = tooltipPortal[group] || {} - tooltipPortal[group].node || - (tooltipPortal[group].node = this.useRootElement()) + tooltipPortal[group] = tooltipPortal[group] || { + node: this.useRootElement(), + count: 0, + } + + tooltipPortal[group].count++ this.setState({ isMounted: true, active }, () => { if (!this.isMainGorup()) { @@ -69,7 +72,7 @@ export default class TooltipPortal extends React.PureComponent { } }) } else if (!active && prevProps.active) { - tooltipPortal[group].timeout = setTimeout(() => { + this.timeout = tooltipPortal[group].timeout = setTimeout(() => { this.setState({ active: false }, () => { if (!this.isMainGorup()) { this.renderPortal() @@ -87,19 +90,25 @@ export default class TooltipPortal extends React.PureComponent { componentWillUnmount() { const { group } = this.props + + clearTimeout(this.timeout) + if (tooltipPortal[group]) { + tooltipPortal[group].count-- + if (!this.isMainGorup()) { ReactDOM.unmountComponentAtNode(tooltipPortal[group].node) } - clearTimeout(tooltipPortal[group].timeout) - try { - document.body.removeChild(tooltipPortal[group].node) - } catch (e) { - // - } + if (tooltipPortal[group].count === 0) { + try { + document.body.removeChild(tooltipPortal[group].node) + } catch (e) { + // + } - tooltipPortal[group] = null + tooltipPortal[group] = null + } } } @@ -147,7 +156,7 @@ export default class TooltipPortal extends React.PureComponent { const targetElement = this.getTargetElement() const { group } = this.props - if (tooltipPortal[group].timeout) { + if (!this.isMainGorup() && tooltipPortal[group]) { clearTimeout(tooltipPortal[group].timeout) }