Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Created September 12, 2018 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barneycarroll/98cb503e53007195278a614de34a8890 to your computer and use it in GitHub Desktop.
Save barneycarroll/98cb503e53007195278a614de34a8890 to your computer and use it in GitHub Desktop.
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