From 443b0f8df95e3f084799ce114cd7247c13592367 Mon Sep 17 00:00:00 2001 From: Hsuan Lee Date: Wed, 5 Dec 2018 17:46:27 +0800 Subject: [PATCH] fix(module: drawer, modal): fix focus bug of IE close #2388 --- components/drawer/nz-drawer.component.ts | 7 ++++++- components/drawer/nz-drawer.service.ts | 1 + components/modal/nz-modal.component.ts | 6 +++++- 3 files changed, 12 insertions(+), 2 deletions(-) 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..d0ec6214600 100644 --- a/components/drawer/nz-drawer.service.ts +++ b/components/drawer/nz-drawer.service.ts @@ -15,6 +15,7 @@ export class DrawerBuilderForService { constructor(private overlay: Overlay, private options: NzDrawerOptions) { this.createDrawer(); this.updateOptions(options); + 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..fef9625bc12 100644 --- a/components/modal/nz-modal.component.ts +++ b/components/modal/nz-modal.component.ts @@ -467,6 +467,10 @@ export class NzModalComponent extends NzModalRef impleme this.previouslyFocusedElement = this.document.activeElement as HTMLElement; this.previouslyFocusedElement.blur(); } + + if (typeof this.elementRef.nativeElement.focus === 'function') { + Promise.resolve().then(() => this.elementRef.nativeElement.focus()); + } } private trapFocus(): void { @@ -477,7 +481,7 @@ export class NzModalComponent extends NzModalRef impleme } private restoreFocus(): void { - if (this.previouslyFocusedElement) { + if (this.previouslyFocusedElement && typeof this.previouslyFocusedElement.focus === 'function') { this.previouslyFocusedElement.focus(); } if (this.focusTrap) {