Skip to content

Commit

Permalink
Merge pull request #26690 from wildan-m/wildan/fix/18555/hover-stuck
Browse files Browse the repository at this point in the history
Resolve stuck hover due to the mouse leave event not triggered.
  • Loading branch information
arosiclair authored Sep 11, 2023
2 parents 7f2cf16 + be86cb3 commit 252a54b
Showing 1 changed file with 21 additions and 0 deletions.
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

0 comments on commit 252a54b

Please sign in to comment.