Directive for listening to MouseLeave, MouseDown, TouchStart outside of a HtmlElement - listeners can be toggled on/off individually with input bindings
npm install @bcodes/ngx-interact-outside 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:boolean default true. Determines if Output events are emitted. This can be toggled
true and false to update the event emitting at runtime
- @Input listenMouseDownOutside default true. Listen for mousedown events outside host
i.e. mousedown event occurred outside the host and hosts child components
- @Input listenTouchStartOutside default false. Listen for touchstart events outside host
i.e. touchstart event occurred outside the host and hosts child components
- @Input listenMouseLeave default false. Listen for mouseleave event on host
i.e. leaves the host component (not triggered while still on child component)
- @Output interactOutsideEvent: EventEmitter emits the mousedown and/or touchstart events
- @Output mouseLeaveEvent: EventEmitter
### Example
``html
`
[isListening]="isMenuOpen"
[listenMouseDownOutside]="true"
[listenTouchStartOutside]="true"
[listenMouseLeave]="true"
(mouseLeaveEvent)="handleMouseLeave()"
(interactOutsideEvent)="handleMouseDownOutside($event)">
interactOutsideEvent
- is based on host
mousedown
- Event triggered if the host element does not contain the target of the event
- In the example below, or touchstart on document, x, y, z would all trigger interactOutsideEvent
mouseLeaveEvent
- triggers only when the mouse leaves the host element. As long as the mouse is over one
host
of the child components, mouse leave will not fire. It will also not fire when moving between a
A->Host
hosts child components. e.g. in example below, moving from would not trigger the event
`
``
+----------+
| document |
+----+-----+
|
+--------------------------+
| | |
+--+---+ +---+---+ +---+---+
| x | | y | | z |
+--+---+ +-------+ +-------+
|
+--+---+
| Host |
+--+---+
|
+--+---+
| A |
+--+---+