Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve stuck hover due to the mouse leave event not triggered. #26690

Merged
merged 10 commits into from
Sep 11, 2023
21 changes: 21 additions & 0 deletions src/components/Hoverable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Hoverable extends Component {
super(props);

this.handleVisibilityChange = this.handleVisibilityChange.bind(this);
this.checkHover = this.checkHover.bind(this);

this.state = {
isHovered: false,
Expand All @@ -23,6 +24,7 @@ class Hoverable extends Component {

componentDidMount() {
document.addEventListener('visibilitychange', this.handleVisibilityChange);
document.addEventListener('mouseover', this.checkHover);
}

componentDidUpdate(prevProps) {
Expand All @@ -37,6 +39,7 @@ class Hoverable extends Component {

componentWillUnmount() {
document.removeEventListener('visibilitychange', this.handleVisibilityChange);
document.removeEventListener('mouseover', this.checkHover);
}

/**
Expand All @@ -54,6 +57,24 @@ class Hoverable extends Component {
}
}

/**
* Checks the hover state of a component and updates it based on the event target.
* This is necessary to handle cases where the hover state might get stuck due to an unreliable mouseleave trigger,
* such as when an element is removed before the mouseleave event is triggered.
* @param {Event} e - The hover event object.
*/
checkHover(e) {
if (!this.wrapperView || !this.state.isHovered) {
return;
}

if (this.wrapperView.contains(e.target)) {
return;
}

this.setIsHovered(false);
}

handleVisibilityChange() {
if (document.visibilityState !== 'hidden') {
return;
Expand Down