@bcodes/ngx-interact-outside v1.0.1
Listen for mouse down events outside of a host component, and mouse leave events
for the host component. For touch enabled devices, touchstart can be used
separately or in conjunction with mousedown. You may want to preventDefault() in
the touchstart interactOutsideEvent handler to prevent emulated mousedown mouseclick etc,
as this Directive does not call preventDefault or stopPropagation on any events
- @Input
isListening:booleandefault true. Determines ifOutputevents are emitted. This can be toggled true and false to update the event emitting at runtime - @Input
listenMouseDownOutsidedefault true. Listen formousedownevents outside host i.e.mousedownevent occurred outside the host and hosts child components - @Input
listenTouchStartOutsidedefault false. Listen fortouchstartevents outside host i.e.touchstartevent occurred outside the host and hosts child components @Input
listenMouseLeavedefault false. Listen for mouseleave event on host i.e. leaves the host component (not triggered while still on child component)@Output
interactOutsideEvent: EventEmitter<MouseEvent | TouchEvent>emits themousedownand/ortouchstartevents@Output
mouseLeaveEvent: EventEmitter<MouseEvent>Example
<div bcInteractOutside [isListening]="isMenuOpen" [listenMouseDownOutside]="true" [listenTouchStartOutside]="true" [listenMouseLeave]="true" (mouseLeaveEvent)="handleMouseLeave()" (interactOutsideEvent)="handleMouseDownOutside($event)">interactOutsideEventis based onhost<HTMLElement>.contains(event<MouseEvent>.target)- Event triggered if the host element does not contain the target of the event
- In the example below,
mousedownortouchstartondocument, x, y, zwould all triggerinteractOutsideEvent mouseLeaveEventtriggers only when the mouse leaves thehostelement. As long as the mouse is over one of thehostchild components, mouse leave will not fire. It will also not fire when moving between a hosts child components. e.g. in example below, moving fromA->Hostwould not trigger the event+----------+ | document | +----+-----+ | +--------------------------+ | | | +--+---+ +---+---+ +---+---+ | x | | y | | z | +--+---+ +-------+ +-------+ | +--+---+ | Host | +--+---+ | +--+---+ | A | +--+---+