@Trincot's answer almost worked for me. In my case, I deal with popover. When I click on the button, it launches a pop-up window displayed on top of the start button. Therefore, it document.elementFromPoint(e.clientX, e.clientY)returns a popover element, not a start button. Here is how I solved it:
mouseleave(ev: MouseEvent) {
const trigger: HTMLElement = document.getElementById('#myTrigger');
const triggerRect = trigger.getBoundingClientRect();
const falsePositive = isWithingARect(ev.clientX, ev.clientY, triggerRect);
if (!falsePositive) {
}
}
function isWithingARect(x: number, y: number, rect: ClientRect) {
const xIsWithin = x > rect.left && x < rect.right;
const yIsWithin = y > rect.top && y < rect.bottom;
return xIsWithin && yIsWithin;
}
source
share