Skip to content

Commit

Permalink
Merge pull request #1343 from dnbexperience/fix/tooltip-portal-premat…
Browse files Browse the repository at this point in the history
…ure-removal

fix(Tooltip): fix premature removal of portal and prevent zombies
  • Loading branch information
tujoworker committed Apr 4, 2022
2 parents bf6cb60 + fb0fe33 commit 62e72cd
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions packages/dnb-eufemia/src/components/tooltip/TooltipPortal.js
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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()
Expand All @@ -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
}
}
}

Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 62e72cd

Please sign in to comment.