Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rafatwork/2ccc008bd3bcd1a721e083ce001eebfa to your computer and use it in GitHub Desktop.
Save rafatwork/2ccc008bd3bcd1a721e083ce001eebfa to your computer and use it in GitHub Desktop.
react-popover onMouseEnter/onMouseLeave example
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