npm i @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
-
@Input
isListening:boolean
default true. Determines ifOutput
events are emitted. This can be toggled true and false to update the event emitting at runtime -
@Input
listenMouseDownOutside
default true. Listen formousedown
events outside host i.e.mousedown
event occurred outside the host and hosts child components -
@Input
listenTouchStartOutside
default false. Listen fortouchstart
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<MouseEvent | TouchEvent>
emits themousedown
and/ortouchstart
events -
@Output
mouseLeaveEvent: EventEmitter<MouseEvent>
<div bcInteractOutside
[isListening]="isMenuOpen"
[listenMouseDownOutside]="true"
[listenTouchStartOutside]="true"
[listenMouseLeave]="true"
(mouseLeaveEvent)="handleMouseLeave()"
(interactOutsideEvent)="handleMouseDownOutside($event)">
interactOutsideEvent
is 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,
mousedown
ortouchstart
ondocument, x, y, z
would all triggerinteractOutsideEvent
mouseLeaveEvent
triggers only when the mouse leaves thehost
element. As long as the mouse is over one of thehost
child 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->Host
would not trigger the event
+----------+
| document |
+----+-----+
|
+--------------------------+
| | |
+--+---+ +---+---+ +---+---+
| x | | y | | z |
+--+---+ +-------+ +-------+
|
+--+---+
| Host |
+--+---+
|
+--+---+
| A |
+--+---+
The library is packaged using ng-packagr, and this repo is based on ng-packagr-template: an Angular CLI + ng-packagr starter kit (with testing, build, packaging + support for Angular CLI)
Run either of the following commands with npm run
"build:lib": "rimraf dist && ng-packagr -p lib/package.json",
"test:lib": "npm run build:lib && ng test"
The tests are contained in the lib/test
folder. The files to be tested are imported from the dist/lib
folder, so we are testing the bundled library
import { LibaryModule } from '../../dist/lib';
The test files are located outside of the root src
folder and required the following changes to the test setup:
tsconfig.spec.json
"include": [
"../lib/**/*.spec.ts",
"**/*.spec.ts",
"**/*.d.ts"
]
tests.ts
const context_lib = require.context('../lib', true, /\.spec\.ts$/);
context_lib.keys().map(context_lib);
Run the tests with ng test
, or npm run test:lib
to do a build and test
A .travis.yml
config file controls the CI when commits are made. This builds the library, and runs the tests against the bundled library
This project is licensed under the terms of the MIT license