Skip to content

Instantly share code, notes, and snippets.

@donaldpipowitch
Last active September 5, 2019 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donaldpipowitch/07e2c9ec0d597df8cb3073d7c53333a9 to your computer and use it in GitHub Desktop.
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)
div [my-focusout-outside]="shouldTrack" (focusoutOutside)="doSomething()">Hello</div>
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