Last active
March 18, 2017 07:05
-
-
Save rafatwork/2ccc008bd3bcd1a721e083ce001eebfa to your computer and use it in GitHub Desktop.
react-popover onMouseEnter/onMouseLeave example
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
import React from 'react'; | |
import Popover from 'react-popover'; | |
class HelloWorld extends React.Component { | |
constructor() { | |
super(); | |
this.showPopover = this.showPopover.bind(this); | |
this.hidePopover = this.hidePopover.bind(this); | |
this.trackMouse = this.trackMouse.bind(this); | |
this.untrackMouse = this.untrackMouse.bind(this); | |
this.state = { | |
isOpen: false, | |
mouseTracked: false, | |
}; | |
} | |
showPopover() { | |
this.setState({ isOpen: true }); | |
} | |
hidePopover() { | |
this.setState({ isOpen: false }); | |
} | |
trackMouse() { | |
this.setState({ mouseTracked: true }); | |
if (!this.state.isOpen) { | |
this.showPopover(); | |
} | |
} | |
untrackMouse() { | |
this.setState({ mouseTracked: false }); | |
setTimeout(() => { | |
if (!this.state.mouseTracked) { | |
this.hidePopover(); | |
} | |
}, 100); | |
} | |
render() { | |
const popoverBody = ( | |
<div onMouseEnter={this.trackMouse} onMouseLeave={this.untrackMouse}> | |
{'Hello World!'} | |
</div> | |
); | |
return ( | |
<Popover body={popoverBody} isOpen={this.state.isOpen}> | |
<div onMouseEnter={this.trackMouse} onMouseLeave={this.untrackMouse}> | |
{'Greet the world...'} | |
</div> | |
</Popover> | |
); | |
} | |
} | |
export default HelloWorld; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment