What I'm trying to do:
- Create a 'resize' angular directive that allows the user to drag borders of DOM elements and call custom application logic (done)
- ensure clicks don't bubble up to parent components.
stackblitz link -> stackblitz example
The component hierarchy is as such
- app.component > parent.component > child.component (the child component uses the 'resize' directive)
in the 'resize.directive'.
- the mousedown event is captured and
event.stopPropagation()
&event.preventDefault()
are used to prevent event bubbling - the mousedown event is only capture if the user's mouse is over the borders
the child.component
- is a simple div the uses the 'resize' directive
the parent component
- uses
@HostListener('click', ['$event'])
- I need to listen to 'click' events on the parent container to identify when it has been selected. so I can't simply get rid of it.
as you mouse over the inner child component, you can click to drag and resize the borders of the element. what I'm struggling with is the event propagation of the 'click' handler in the parent component.
My issue is that the @HostListener
click event is being fired from the parent component, when I'm not expecting it to be. i.e. if you drag the borders of the child component. on mouse up, the click event of the ParentComponent
is fired.
My understanding is that
- to prevent event propagation of an click event, it should be captured at the mousedown event. hence
stopPropagation()
yet the parent component is still pickup the click event.
There is a lot of comments around using return false
. however, based on other issues, that doesn't seem to be the cause, as indicated here.
that being said, even if I add return false
to the directive, the event still bubbles up.
Any insights into what I might be missing is appreciated.