I may just be thinking about this wrong because I'm doing it in Angular and over complicating, but what I'm trying to do is setup my click event so it only triggers when an element is clicked, but not it's child. I'm trying to setup a modal, where if you click the background overlay it closes, but obviously I don't want it closing if the user interacts with the modal.
<div class="overlay">
<div class="modal">
</div>
</div>
So far, I've created this:
@HostListener('document:click', ['$event.target']) public onClick(targetElement: HTMLElement) {
const clickedInside = this._elementRef.nativeElement.contains(targetElement);
if (!clickedInside && targetElement.class.indexOf('overlay') && targetElement.parentElement.tagName === 'GP-MODAL') {
//do stuff
}
}
Where _elementRef
is the Angular ElementRef
. The problem is it feels like an inefficient way of doing it: trigger on any click, only continue on certain elements. It feels more ideal to trigger a click on .overlay
and then somehow not have it go off in .wrapper
, but I can't think of how to do it. Any advice?