Skip to content

Commit cf6427e

Browse files
committed
Refactor removal tracker
1 parent c721288 commit cf6427e

File tree

2 files changed

+24
-52
lines changed

2 files changed

+24
-52
lines changed

src/decorators/trackRemoval.js

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,44 @@
1+
/**
2+
* Tracking target removing from DOM.
3+
* It's nessesary to hide tooltip when it's target disappears.
4+
* Otherwise, the tooltip would be shown forever until another target
5+
* is triggered.
6+
*
7+
* If MutationObserver is not available, this feature just doesn't work.
8+
*/
9+
110
// https://hacks.mozilla.org/2012/05/dom-mutationobserver-reacting-to-dom-changes-without-killing-browser-performance/
211
const getMutationObserverClass = () => {
312
return window.MutationObserver ||
413
window.WebKitMutationObserver ||
514
window.MozMutationObserver
615
}
716

8-
const isMutationObserverAvailable = () => {
9-
return getMutationObserverClass() != null
10-
}
11-
12-
class ObserverBasedRemovalTracker {
13-
constructor (tooltip) {
14-
this.tooltip = tooltip
15-
16-
this.observer = null
17-
this.inited = false
18-
}
19-
20-
init () {
21-
if (this.inited) {
22-
this.unbind()
23-
}
24-
this.inited = true
25-
17+
export default function (target) {
18+
target.prototype.bindRemovalTracker = function () {
2619
const MutationObserver = getMutationObserverClass()
27-
if (!MutationObserver) {
28-
return
29-
}
20+
if (MutationObserver == null) return
3021

31-
this.observer = new MutationObserver((mutations) => {
22+
const observer = new MutationObserver((mutations) => {
3223
for (const mutation of mutations) {
3324
for (const element of mutation.removedNodes) {
34-
if (element === this.tooltip.state.currentTarget) {
35-
this.tooltip.hideTooltip()
25+
if (element === this.state.currentTarget) {
26+
this.hideTooltip()
3627
return
3728
}
3829
}
3930
}
4031
})
4132

42-
this.observer.observe(window.document, { childList: true, subtree: true })
43-
}
44-
45-
unbind () {
46-
if (this.observer) {
47-
this.observer.disconnect()
48-
this.observer = null
49-
}
50-
this.inited = false
51-
}
52-
}
33+
observer.observe(window.document, { childList: true, subtree: true })
5334

54-
export default function (target) {
55-
target.prototype.bindRemovalTracker = function () {
56-
if (isMutationObserverAvailable()) {
57-
this.removalTracker = new ObserverBasedRemovalTracker(this)
58-
this.removalTracker.init()
59-
}
35+
this.removalTracker = observer
6036
}
6137

6238
target.prototype.unbindRemovalTracker = function () {
63-
this.removalTracker.unbind()
64-
this.removalTracker = null
39+
if (this.removalTracker) {
40+
this.removalTracker.disconnect()
41+
this.removalTracker = null
42+
}
6543
}
6644
}

src/index.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ class ReactTooltip extends Component {
9292
this.bind([
9393
'showTooltip',
9494
'updateTooltip',
95-
'checkSameTarget',
9695
'hideTooltip',
9796
'globalRebuild',
9897
'globalShow',
@@ -169,8 +168,6 @@ class ReactTooltip extends Component {
169168
const {id, globalEventOff} = this.props
170169
let targetArray = this.getTargetArray(id)
171170

172-
this.bindRemovalTracker()
173-
174171
targetArray.forEach(target => {
175172
const isCaptureMode = this.isCapture(target)
176173
const effect = this.getEffect(target)
@@ -196,6 +193,9 @@ class ReactTooltip extends Component {
196193
window.removeEventListener(globalEventOff, this.hideTooltip)
197194
window.addEventListener(globalEventOff, this.hideTooltip, false)
198195
}
196+
197+
// Track removal of targetArray elements from DOM
198+
this.bindRemovalTracker()
199199
}
200200

201201
/**
@@ -339,12 +339,6 @@ class ReactTooltip extends Component {
339339
}
340340
}
341341

342-
checkSameTarget (e) {
343-
if (this.state.currentTarget === e.currentTarget) {
344-
this.hideTooltip(e)
345-
}
346-
}
347-
348342
/**
349343
* When mouse leave, hide tooltip
350344
*/

0 commit comments

Comments
 (0)