Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class OffFocus extends React.Component {
constructor(){
super()
const handler = e => {
if(
this.state.focused
&&
e.target.nodeType
&&
!this.refs.wrapper.contains(e.target)
)
this.props.callback()
}
this.listener = [
'focus',
handler,
true,
]
this.state = {
focused : false,
}
}
componentDidMount(){
addEventListener(...this.listener)
}
componentWillUnmount(){
removeEventListener(...this.listener)
}
render = () =>
<div ref='wrapper'>
{this.children}
</div>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.