Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 7x 7x | import { Directive, ElementRef, EventEmitter, HostListener, Output } from '@angular/core';
/**
* This directive can be set on a html tag to check focus outside of the tag.
*/
@Directive({
selector: '[ishFocusOutside]',
})
export class FocusOutsideDirective {
constructor(private elementRef: ElementRef) {}
/**
* Event to tell the listener, when focus moves outside the target element
*/
@Output() isFocusedOutside = new EventEmitter<boolean>();
/**
* Method to check if focus is outside of the targetElement. Emits true when focus moves outside.
*/
@HostListener('document:focusin', ['$event.target'])
onFocusIn(targetElement: ElementRef): void {
Iif (!this.elementRef.nativeElement.contains(targetElement)) {
this.isFocusedOutside.emit(true);
}
}
}
|