Skip to content

Commit

Permalink
fix(module: drawer, modal): fix focus bug of IE
Browse files Browse the repository at this point in the history
  • Loading branch information
hsuanxyz committed Dec 5, 2018
1 parent ef10595 commit 443b0f8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion components/drawer/nz-drawer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ export class NzDrawerComponent<T = any, R = any, D = any> extends NzDrawerRef<R>
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());
}
}
}

Expand All @@ -297,7 +301,8 @@ export class NzDrawerComponent<T = any, R = any, D = any> extends NzDrawerRef<R>
}

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) {
Expand Down
1 change: 1 addition & 0 deletions components/drawer/nz-drawer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class DrawerBuilderForService<R> {
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(() => {
Expand Down
6 changes: 5 additions & 1 deletion components/modal/nz-modal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ export class NzModalComponent<T = any, R = any> extends NzModalRef<T, R> 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 {
Expand All @@ -477,7 +481,7 @@ export class NzModalComponent<T = any, R = any> extends NzModalRef<T, R> impleme
}

private restoreFocus(): void {
if (this.previouslyFocusedElement) {
if (this.previouslyFocusedElement && typeof this.previouslyFocusedElement.focus === 'function') {
this.previouslyFocusedElement.focus();
}
if (this.focusTrap) {
Expand Down

0 comments on commit 443b0f8

Please sign in to comment.