Skip to content

Instantly share code, notes, and snippets.

@andrey-kazakov
Created May 17, 2015 13:19
Show Gist options
  • Save andrey-kazakov/16771ce1ce1182e32b47 to your computer and use it in GitHub Desktop.
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)
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