Skip to content

Commit

Permalink
fix(Tooltip): ensure debounce function is cleaned-up (carbon-design-s…
Browse files Browse the repository at this point in the history
…ystem#1882)

Fixes carbon-design-system#1871.

Note that the feature this fix is applied to has been deprecated.
  • Loading branch information
asudoh authored and tw15egan committed Feb 15, 2019
1 parent 1553817 commit 1c00f04
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/components/Tooltip/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,21 @@ export default class Tooltip extends Component {
_tooltipEl = null;

componentDidMount() {
if (!this._debouncedHandleHover) {
this._debouncedHandleHover = debounce(this._handleHover, 200);
}
requestAnimationFrame(() => {
this.getTriggerPosition();
});
}

componentWillUnmount() {
if (this._debouncedHandleHover) {
this._debouncedHandleHover.cancel();
this._debouncedHandleHover = null;
}
}

static getDerivedStateFromProps({ open }, state) {
/**
* so that tooltip can be controlled programmatically through this `open` prop
Expand Down Expand Up @@ -284,7 +294,7 @@ export default class Tooltip extends Component {
* @type {Function}
* @private
*/
_debouncedHandleHover = debounce(this._handleHover, 200);
_debouncedHandleHover = null;

/**
* @returns {Element} The DOM element where the floating menu is placed in.
Expand Down Expand Up @@ -313,7 +323,11 @@ export default class Tooltip extends Component {
}
this.setState({ open: shouldOpen });
}
} else if (state && (state !== 'out' || !hadContextMenu)) {
} else if (
state &&
(state !== 'out' || !hadContextMenu) &&
this._debouncedHandleHover
) {
this._debouncedHandleHover(state, evt.relatedTarget);
}
};
Expand Down

0 comments on commit 1c00f04

Please sign in to comment.