Skip to content

Commit 1eaec1e

Browse files
crisbetotinayuangao
authored andcommitted
refactor(dialog): switch to common keyboard handling and move keyboard logic into individual dialog (#9078)
* Refactors the dialogs to use the common keyboard provider, rather than binding to the document. * Moves the responsibility for handling the keyboard events from the dialog provider to the individual dialogs.
1 parent ec56cb2 commit 1eaec1e

File tree

4 files changed

+15
-34
lines changed

4 files changed

+15
-34
lines changed

src/lib/dialog/dialog-ref.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import {OverlayRef, GlobalPositionStrategy} from '@angular/cdk/overlay';
10+
import {ESCAPE} from '@angular/cdk/keycodes';
1011
import {filter} from 'rxjs/operators/filter';
1112
import {take} from 'rxjs/operators/take';
1213
import {DialogPosition} from './dialog-config';
@@ -68,6 +69,10 @@ export class MatDialogRef<T, R = any> {
6869
this._afterClosed.complete();
6970
this.componentInstance = null!;
7071
});
72+
73+
_overlayRef.keydownEvents()
74+
.pipe(filter(event => event.keyCode === ESCAPE && !this.disableClose))
75+
.subscribe(() => this.close());
7176
}
7277

7378
/**

src/lib/dialog/dialog.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
import {Directionality} from '@angular/cdk/bidi';
10-
import {ESCAPE} from '@angular/cdk/keycodes';
1110
import {
1211
BlockScrollStrategy,
1312
Overlay,
@@ -30,7 +29,6 @@ import {
3029
import {Observable} from 'rxjs/Observable';
3130
import {defer} from 'rxjs/observable/defer';
3231
import {of as observableOf} from 'rxjs/observable/of';
33-
import {filter} from 'rxjs/operators/filter';
3432
import {startWith} from 'rxjs/operators/startWith';
3533
import {Subject} from 'rxjs/Subject';
3634
import {MatDialogConfig} from './dialog-config';
@@ -238,11 +236,6 @@ export class MatDialog {
238236
});
239237
}
240238

241-
// Close when escape keydown event occurs
242-
overlayRef.keydownEvents().pipe(
243-
filter(event => event.keyCode === ESCAPE && !dialogRef.disableClose)
244-
).subscribe(() => dialogRef.close());
245-
246239
if (componentOrTemplateRef instanceof TemplateRef) {
247240
dialogContainer.attachTemplatePortal(
248241
new TemplatePortal<T>(componentOrTemplateRef, null!,

src/material-experimental/dialog/dialog-ref.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99

1010
import {OverlayRef, GlobalPositionStrategy, OverlaySizeConfig} from '@angular/cdk/overlay';
11+
import {ESCAPE} from '@angular/cdk/keycodes';
1112
import {Observable} from 'rxjs/Observable';
1213
import {map} from 'rxjs/operators/map';
14+
import {filter} from 'rxjs/operators/filter';
1315
import {DialogPosition} from './dialog-config';
1416
import {CdkDialogContainer} from './dialog-container';
1517

@@ -52,6 +54,11 @@ export class DialogRef<T, R = any> {
5254
this._overlayRef.dispose();
5355
this.componentInstance = null!;
5456
});
57+
58+
// Close when escape keydown event occurs
59+
_overlayRef.keydownEvents()
60+
.pipe(filter(event => event.keyCode === ESCAPE && !this.disableClose))
61+
.subscribe(() => this.close());
5562
}
5663

5764
/** Gets an observable that emits when the overlay's backdrop has been clicked. */

src/material-experimental/dialog/dialog.ts

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
ComponentRef
1616
} from '@angular/core';
1717
import {ComponentPortal, PortalInjector, TemplatePortal} from '@angular/cdk/portal';
18-
import {ESCAPE} from '@angular/cdk/keycodes';
1918
import {Observable} from 'rxjs/Observable';
2019
import {Subject} from 'rxjs/Subject';
2120
import {DialogRef} from './dialog-ref';
@@ -46,9 +45,6 @@ import {
4645
* Service to open modal dialogs.
4746
*/
4847
export class Dialog {
49-
/** A the handleKeydown method bound to the class instance. */
50-
private _boundKeydown = this._handleKeydown.bind(this);
51-
5248
/** Stream that emits when all dialogs are closed. */
5349
get _afterAllClosed(): Observable<void> {
5450
return this._parentDialog ? this._parentDialog.afterAllClosed : this._afterAllClosedBase;
@@ -83,10 +79,6 @@ export class Dialog {
8379
if (!_parentDialog && location) {
8480
location.subscribe(() => this.closeAll());
8581
}
86-
87-
this.afterAllClosed.subscribe(() => {
88-
document.removeEventListener('keydown', this._boundKeydown);
89-
});
9082
}
9183

9284
/** Gets an open dialog by id. */
@@ -134,18 +126,16 @@ export class Dialog {
134126
}
135127

136128
/**
137-
* Registers a keydown listener, and forwards emitting events for when dialogs are opened and all
138-
* dialogs are closed.
129+
* Forwards emitting events for when dialogs are opened and all dialogs are closed.
139130
*/
140131
private registerDialogRef(dialogRef: DialogRef<any>): void {
141-
if (!this.openDialogs.length) {
142-
document.addEventListener('keydown', this._boundKeydown);
143-
}
144132
this.openDialogs.push(dialogRef);
133+
145134
let dialogOpenSub = dialogRef.afterOpen().subscribe(() => {
146135
this.afterOpen.next(dialogRef);
147136
dialogOpenSub.unsubscribe();
148137
});
138+
149139
let dialogCloseSub = dialogRef.afterClosed().subscribe(() => {
150140
let dialogIdx = this._openDialogs.indexOf(dialogRef);
151141
if (dialogIdx !== -1) { this._openDialogs.splice(dialogIdx, 1); }
@@ -157,20 +147,6 @@ export class Dialog {
157147
});
158148
}
159149

160-
/**
161-
* Handles global key presses while there are open dialogs. Closes the
162-
* top dialog when the user presses escape.
163-
*/
164-
private _handleKeydown(event: KeyboardEvent): void {
165-
const topDialog = this.openDialogs[this.openDialogs.length - 1];
166-
const canClose = topDialog ? !topDialog.disableClose : false;
167-
168-
if (event.keyCode === ESCAPE && canClose) {
169-
topDialog.close();
170-
}
171-
}
172-
173-
174150
/**
175151
* Creates an overlay config from a dialog config.
176152
* @param config The dialog configuration.

0 commit comments

Comments
 (0)