forked from arkon/ng-click-outside
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: put code for emit on blur into dedicated directive (#55)
* feat: put code for emit on blur into dedicated directive BREAKING CHANGE: emitOnBlur property is now an Directive NgClickOutsideEmitOnBlurDirective
- Loading branch information
Showing
7 changed files
with
113 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,4 @@ testem.log | |
# System Files | ||
.DS_Store | ||
Thumbs.db | ||
/.nx/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
projects/ng-click-outside2/src/lib/ng-click-outside-emit-on-blur.directive.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import {NgClickOutsideEmitOnBlurDirective} from './ng-click-outside-emit-on-blur.directive'; | ||
import {ComponentFixture, TestBed} from "@angular/core/testing"; | ||
import {DOCUMENT} from "@angular/common"; | ||
import {Component, ViewChild} from "@angular/core"; | ||
import {By} from "@angular/platform-browser"; | ||
|
||
|
||
@Component({ | ||
// eslint-disable-next-line @angular-eslint/component-selector | ||
selector: 'test-click', | ||
standalone: true, | ||
imports: [NgClickOutsideEmitOnBlurDirective], | ||
template: ` | ||
<button id="b-1" (blurWindow)="blurWindowButton1 = blurWindowButton1 + 1" | ||
(click)="clickButton1 = clickButton1 + 1" clickOutsideEmitOnBlur | ||
></button> | ||
` | ||
}) | ||
class TestClickOutsideComponent { | ||
@ViewChild(NgClickOutsideEmitOnBlurDirective) ngClickOutsideEmitOnBlurDirective?: NgClickOutsideEmitOnBlurDirective | ||
blurWindowButton1 = 0; | ||
clickButton1 = 0; | ||
} | ||
|
||
|
||
describe('NgClickOutsideEmitOnBlurDirective', () => { | ||
let component: TestClickOutsideComponent; | ||
let fixture: ComponentFixture<TestClickOutsideComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [TestClickOutsideComponent], | ||
providers: [{provider: DOCUMENT, useValue: document}] | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(TestClickOutsideComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}) | ||
|
||
it('should create an instance', () => { | ||
expect(component.ngClickOutsideEmitOnBlurDirective).toBeDefined(); | ||
}); | ||
|
||
it('should emit on window blur', () => { | ||
const button1 = fixture.debugElement.query(By.css('#b-1')); | ||
|
||
button1.nativeElement.click(); | ||
document.defaultView!.dispatchEvent(new Event('blur')); | ||
|
||
expect(component.clickButton1).toBe(1); | ||
expect(component.blurWindowButton1).toBe(1); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
projects/ng-click-outside2/src/lib/ng-click-outside-emit-on-blur.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {afterNextRender, Directive, EventEmitter, inject, NgZone, OnDestroy, Output} from '@angular/core'; | ||
import {DOCUMENT} from "@angular/common"; | ||
|
||
/** | ||
* emits an event when user clicks outside of applications' window while it's visible. | ||
* Especially useful if page contains iframes. | ||
*/ | ||
@Directive({ | ||
selector: '[clickOutsideEmitOnBlur]', | ||
standalone: true | ||
}) | ||
export class NgClickOutsideEmitOnBlurDirective implements OnDestroy { | ||
private _ngZone = inject(NgZone); | ||
private document = inject<Document>(DOCUMENT); | ||
@Output() blurWindow: EventEmitter<Event> = new EventEmitter<Event>(); | ||
|
||
constructor() { | ||
this._onWindowBlur = this._onWindowBlur.bind(this); | ||
afterNextRender(() => this._initWindowBlurListener()) | ||
} | ||
|
||
ngOnDestroy() { | ||
this._removeWindowBlurListener(); | ||
} | ||
|
||
private _initWindowBlurListener() { | ||
this._ngZone.runOutsideAngular(() => { | ||
this.document.defaultView?.addEventListener('blur', this._onWindowBlur); | ||
}); | ||
} | ||
|
||
/** | ||
* Resolves problem with outside click on iframe | ||
* @see https://github.com/arkon/ng-click-outside/issues/32 | ||
*/ | ||
private _onWindowBlur(ev: Event) { | ||
if (!this.document.hidden) { | ||
this._ngZone.run(() => this.blurWindow.emit(ev)); | ||
} | ||
} | ||
|
||
private _removeWindowBlurListener() { | ||
this._ngZone.runOutsideAngular(() => { | ||
this.document.defaultView?.removeEventListener('blur', this._onWindowBlur); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters