0

I want to detect if the mouse left the browser window and not just a specific div therefore not using the (mouseleave) etc. methods in HTML. Is there a way?

UPDATE:

ParentComponent:

<div class="parent" (dragover)="openDialogComponent()"></div>

@HostListener('window:dragleave') onMouseLeave() {
    console.log('leave window?');
}

What actually happens: When files are dragged over ParentComponent, DialogComponent opens. When files are dragged over DialogComponent, it closes because it left ParentComponent area thus giving a flickering effect on the popup.

What I want to happen: How do you close DialogComponent when mouse leaves both Parent and Dialog components, or in other words, when mouse/drag events leave the BROWSER/WHOLE WINDOW?

1 Answer 1

0

You can use HostListener for this,

@HostListener('window:mouseleave', ['$event.target']) onMouseLeave() {
    // do something
}

The key is the DOM event that the directive listens to. To listen to global events, add the target to the event name. The target can be window, document or body.

Refer the Official Documentation.

Sign up to request clarification or add additional context in comments.

3 Comments

but doesn't it only work for components and not the window as a whole?
For global events, give the target in the hostlistener. Please have a look at the updated answer.
@AngelaAmarapala It does not work when I give the window target in the hostlistener but work if I did not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.