Skip to content

Commit 5a0b1f0

Browse files
committed
create event for ESCAPE keydown
1 parent 70bd5fc commit 5a0b1f0

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/lib/sidenav/drawer.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ describe('MdDrawer', () => {
102102
expect(testComponent.backdropClickedCount).toBe(1);
103103
}));
104104

105+
it('should emit the escapeKeydown event when ESCAPE key is pressed', () => {
106+
let fixture = TestBed.createComponent(BasicTestApp);
107+
let testComponent: BasicTestApp = fixture.debugElement.componentInstance;
108+
let drawer = fixture.debugElement.query(By.directive(MdDrawer));
109+
expect(testComponent.escapeKeydownCount).toBe(0);
110+
111+
dispatchKeyboardEvent(drawer.nativeElement, 'keydown', ESCAPE);
112+
fixture.detectChanges();
113+
114+
expect(testComponent.escapeKeydownCount).toBe(1);
115+
});
116+
105117
it('should close when pressing escape', fakeAsync(() => {
106118
let fixture = TestBed.createComponent(BasicTestApp);
107119

@@ -386,7 +398,8 @@ class DrawerContainerTwoDrawerTestApp {
386398
<md-drawer-container (backdropClick)="backdropClicked()">
387399
<md-drawer #drawer position="start"
388400
(open)="open()"
389-
(close)="close()">
401+
(close)="close()"
402+
(escapeKeydown)="escapeKeydown()">
390403
<button #drawerButton>Content.</button>
391404
</md-drawer>
392405
<button (click)="drawer.open()" class="open" #openButton></button>
@@ -397,6 +410,7 @@ class BasicTestApp {
397410
openCount: number = 0;
398411
closeCount: number = 0;
399412
backdropClickedCount: number = 0;
413+
escapeKeydownCount: number = 0;
400414

401415
@ViewChild('drawerButton') drawerButton: ElementRef;
402416
@ViewChild('openButton') openButton: ElementRef;
@@ -413,6 +427,10 @@ class BasicTestApp {
413427
backdropClicked() {
414428
this.backdropClickedCount++;
415429
}
430+
431+
escapeKeydown() {
432+
this.escapeKeydownCount++;
433+
}
416434
}
417435

418436
@Component({

src/lib/sidenav/drawer.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class MdDrawer implements AfterContentInit, OnDestroy {
118118
/** Mode of the drawer; one of 'over', 'push' or 'side'. */
119119
@Input() mode: 'over' | 'push' | 'side' = 'over';
120120

121-
/** Whether the drawer can be closed with the escape key or not. */
121+
/** Whether the drawer can be closed with the escape key or by clicking on the backdrop. */
122122
@Input()
123123
get disableClose(): boolean { return this._disableClose; }
124124
set disableClose(value: boolean) { this._disableClose = coerceBooleanProperty(value); }
@@ -152,6 +152,9 @@ export class MdDrawer implements AfterContentInit, OnDestroy {
152152
/** Event emitted when the drawer's position changes. */
153153
@Output('positionChanged') onPositionChanged = new EventEmitter<void>();
154154

155+
/** Event emitted when ESCAPE is pressed. */
156+
@Output() escapeKeydown = new EventEmitter<void>();
157+
155158
/** @deprecated */
156159
@Output('align-changed') onAlignChanged = new EventEmitter<void>();
157160

@@ -259,9 +262,12 @@ export class MdDrawer implements AfterContentInit, OnDestroy {
259262
* @docs-private
260263
*/
261264
handleKeydown(event: KeyboardEvent) {
262-
if (event.keyCode === ESCAPE && !this.disableClose) {
263-
this.close();
264-
event.stopPropagation();
265+
if (event.keyCode === ESCAPE) {
266+
this.escapeKeydown.emit();
267+
if (!this.disableClose) {
268+
this.close();
269+
event.stopPropagation();
270+
}
265271
}
266272
}
267273

0 commit comments

Comments
 (0)