Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(module:modal): auto focus when open and restore focus when close #2188

Merged
merged 1 commit into from
Oct 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions components/modal/nz-modal.component.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FocusTrap, FocusTrapFactory } from '@angular/cdk/a11y';
import { Overlay, OverlayRef } from '@angular/cdk/overlay';
import { DOCUMENT } from '@angular/common';
import {
Expand Down Expand Up @@ -48,6 +49,8 @@ type AnimationState = 'enter' | 'leave' | null;
// tslint:disable-next-line:no-any
export class NzModalComponent<T = any, R = any> extends NzModalRef<T, R> implements OnInit, OnChanges, AfterViewInit, OnDestroy, ModalOptions<T> {
private unsubscribe$ = new Subject<void>();
private previouslyFocusedElement: HTMLElement;
private focusTrap: FocusTrap;

// tslint:disable-next-line:no-any
locale: any = {};
Expand Down Expand Up @@ -125,6 +128,7 @@ export class NzModalComponent<T = any, R = any> extends NzModalRef<T, R> impleme
private viewContainer: ViewContainerRef,
private nzMeasureScrollbarService: NzMeasureScrollbarService,
private modalControl: NzModalControlService,
private focusTrapFactory: FocusTrapFactory,
@Inject(NZ_MODAL_CONFIG) private config: NzModalConfig,
@Inject(DOCUMENT) private document: any) { // tslint:disable-line:no-any

Expand Down Expand Up @@ -289,6 +293,8 @@ export class NzModalComponent<T = any, R = any> extends NzModalRef<T, R> impleme
private handleVisibleStateChange(visible: boolean, animation: boolean = true, closeResult?: R): Promise<void> {
if (visible) { // Hide scrollbar at the first time when shown up
this.changeBodyOverflow(1);
this.savePreviouslyFocusedElement();
this.trapFocus();
}

return Promise
Expand All @@ -298,6 +304,7 @@ export class NzModalComponent<T = any, R = any> extends NzModalRef<T, R> impleme
this.nzAfterOpen.emit();
} else {
this.nzAfterClose.emit(closeResult);
this.restoreFocus();
this.changeBodyOverflow(); // Show/hide scrollbar when animation is over
}
});
Expand Down Expand Up @@ -443,6 +450,29 @@ export class NzModalComponent<T = any, R = any> extends NzModalRef<T, R> impleme
private mergeDefaultConfig(config: NzModalConfig): NzModalConfig {
return { ...NZ_MODAL_DEFAULT_CONFIG, ...config };
}

private savePreviouslyFocusedElement(): void {
if (this.document) {
this.previouslyFocusedElement = this.document.activeElement as HTMLElement;
this.previouslyFocusedElement.blur();
}
}

private trapFocus(): void {
if (!this.focusTrap) {
this.focusTrap = this.focusTrapFactory.create(this.elementRef.nativeElement);
}
this.focusTrap.focusInitialElementWhenReady();
}

private restoreFocus(): void {
if (this.previouslyFocusedElement) {
this.previouslyFocusedElement.focus();
}
if (this.focusTrap) {
this.focusTrap.destroy();
}
}
}

////////////
Expand Down