Last active
September 5, 2019 09:05
-
-
Save donaldpipowitch/07e2c9ec0d597df8cb3073d7c53333a9 to your computer and use it in GitHub Desktop.
Angular directive to (conditionally) track focusout outside of an element (children are ignored)
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
div [my-focusout-outside]="shouldTrack" (focusoutOutside)="doSomething()">Hello</div> |
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
import { | |
Directive, | |
Input, | |
Output, | |
EventEmitter, | |
ElementRef, | |
OnDestroy | |
} from '@angular/core'; | |
@Directive({ | |
selector: '[my-focusout-outside]' | |
}) | |
export class FocusoutOutsideDirective implements OnDestroy { | |
@Input() | |
set 'my-focusout-outside'(listen: boolean) { | |
if (listen) { | |
document.addEventListener('focusout', this.onFocusout); | |
} else { | |
document.removeEventListener('focusout', this.onFocusout); | |
} | |
} | |
@Output() focusoutOutside = new EventEmitter<void>(); | |
constructor(private elementRef: ElementRef) {} | |
private onFocusout = (_event: any) => { | |
// note: next TS release probably uses FocusEvent correctly | |
// see https://github.com/microsoft/TSJS-lib-generator/pull/696 | |
const event: FocusEvent = _event; | |
// ignore switching to something completely different (e.g. dev tools) | |
if (event.relatedTarget === null) return; | |
const focusInside = this.elementRef.nativeElement.contains( | |
event.relatedTarget | |
); | |
if (!focusInside) { | |
this.focusoutOutside.emit(); | |
} | |
}; | |
ngOnDestroy() { | |
document.removeEventListener('focusout', this.onFocusout); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment