diff --git a/components/drawer/nz-drawer.component.ts b/components/drawer/nz-drawer.component.ts index 4470239c5c8..ebbf39773f5 100644 --- a/components/drawer/nz-drawer.component.ts +++ b/components/drawer/nz-drawer.component.ts @@ -286,6 +286,10 @@ export class NzDrawerComponent extends NzDrawerRef if (this.document) { this.previouslyFocusedElement = this.document.activeElement as HTMLElement; this.previouslyFocusedElement.blur(); + + if (typeof this.elementRef.nativeElement.focus === 'function') { + Promise.resolve().then(() => this.elementRef.nativeElement.focus()); + } } } @@ -297,7 +301,8 @@ export class NzDrawerComponent extends NzDrawerRef } private restoreFocus(): void { - if (this.previouslyFocusedElement) { + // We need the extra check, because IE can set the `activeElement` to null in some cases. + if (this.previouslyFocusedElement && typeof this.previouslyFocusedElement.focus === 'function') { this.previouslyFocusedElement.focus(); } if (this.focusTrap) { diff --git a/components/drawer/nz-drawer.service.ts b/components/drawer/nz-drawer.service.ts index dbc5e2d0d4a..40a256a0d87 100644 --- a/components/drawer/nz-drawer.service.ts +++ b/components/drawer/nz-drawer.service.ts @@ -15,6 +15,8 @@ export class DrawerBuilderForService { constructor(private overlay: Overlay, private options: NzDrawerOptions) { this.createDrawer(); this.updateOptions(options); + // Prevent repeatedly open drawer when tap focus element. + this.drawerRef.instance.savePreviouslyFocusedElement(); this.drawerRef.instance.nzOnViewInit .pipe(takeUntil(this.unsubscribe$)) .subscribe(() => { diff --git a/components/modal/nz-modal.component.ts b/components/modal/nz-modal.component.ts index e4fe9011a27..ac621b94727 100644 --- a/components/modal/nz-modal.component.ts +++ b/components/modal/nz-modal.component.ts @@ -477,7 +477,8 @@ export class NzModalComponent extends NzModalRef impleme } private restoreFocus(): void { - if (this.previouslyFocusedElement) { + // We need the extra check, because IE can set the `activeElement` to null in some cases. + if (this.previouslyFocusedElement && typeof this.previouslyFocusedElement.focus === 'function') { this.previouslyFocusedElement.focus(); } if (this.focusTrap) {