Created
May 17, 2015 13:19
-
-
Save andrey-kazakov/16771ce1ce1182e32b47 to your computer and use it in GitHub Desktop.
React focus monitor mixin (click or focus outside of monitored node calls a handler)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
componentWillUnmount: function() { | |
this.endFocusMonitor(); | |
}, | |
startFocusMonitor: function() { | |
document.addEventListener('focusin', this.checkFocusTarget); | |
document.addEventListener('click', this.checkFocusTarget); | |
}, | |
checkFocusTarget: function(event) { | |
var element = event.target, | |
monitoredNode = this.getFocusMonitoredNode(); | |
while (element) { | |
if (element == monitoredNode) { | |
break; | |
} | |
element = element.parentNode; | |
} | |
if (!element) { | |
this.onFocusMonitoredNodeBlur(); | |
} | |
}, | |
endFocusMonitor: function() { | |
document.removeEventListener('focusin', this.checkFocusTarget) | |
document.removeEventListener('click', this.checkFocusTarget); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment