bitwarden-estensione-browser/libs/angular/src/directives/autofocus.directive.ts

28 lines
728 B
TypeScript
Raw Normal View History

2021-12-16 13:36:21 +01:00
import { Directive, ElementRef, Input, NgZone } from "@angular/core";
import { take } from "rxjs/operators";
2021-12-16 13:36:21 +01:00
import { Utils } from "jslib-common/misc/utils";
2018-08-01 15:13:49 +02:00
@Directive({
2021-12-16 13:36:21 +01:00
selector: "[appAutofocus]",
})
export class AutofocusDirective {
2021-12-16 13:36:21 +01:00
@Input() set appAutofocus(condition: boolean | string) {
this.autofocus = condition === "" || condition === true;
}
2021-12-16 13:36:21 +01:00
private autofocus: boolean;
2021-12-16 13:36:21 +01:00
constructor(private el: ElementRef, private ngZone: NgZone) {}
2021-12-16 13:36:21 +01:00
ngOnInit() {
if (!Utils.isMobileBrowser && this.autofocus) {
if (this.ngZone.isStable) {
this.el.nativeElement.focus();
} else {
this.ngZone.onStable.pipe(take(1)).subscribe(() => this.el.nativeElement.focus());
}
}
2021-12-16 13:36:21 +01:00
}
}