@@ -8,6 +8,7 @@ import 'dart:html';
88import 'package:angular/angular.dart' ;
99import 'package:angular_components/focus/focus_trap.dart' ;
1010import 'package:angular_components/laminate/components/modal/modal.dart' ;
11+ import 'package:angular_components/model/a11y/keyboard_handler_mixin.dart' ;
1112import 'package:angular_components/utils/browser/dom_service/dom_service.dart' ;
1213import 'package:angular_components/utils/disposer/disposer.dart' ;
1314
@@ -30,7 +31,9 @@ import 'package:angular_components/utils/disposer/disposer.dart';
3031 directives: [FocusTrapComponent , NgIf ],
3132 changeDetection: ChangeDetectionStrategy .OnPush ,
3233)
33- class MaterialDialogComponent implements AfterContentChecked , OnDestroy {
34+ class MaterialDialogComponent
35+ with KeyboardHandlerMixin
36+ implements AfterContentChecked , OnDestroy {
3437 final HtmlElement _rootElement;
3538 final DomService _domService;
3639 final ChangeDetectorRef _changeDetector;
@@ -48,7 +51,9 @@ class MaterialDialogComponent implements AfterContentChecked, OnDestroy {
4851 bool _shouldListenForFullscreenChanges = false ;
4952
5053 MaterialDialogComponent (this ._rootElement, this ._domService,
51- this ._changeDetector, @Optional () this ._modal);
54+ this ._changeDetector, @Optional () this ._modal) {
55+ escapeHandler = _defaultEscapeHandler;
56+ }
5257
5358 @ViewChild ('main' , read: HtmlElement )
5459 set main (HtmlElement element) {
@@ -62,6 +67,11 @@ class MaterialDialogComponent implements AfterContentChecked, OnDestroy {
6267 }));
6368 }
6469
70+ /// Function to handle escape key events from the dialog. By default it tries
71+ /// to close the parent modal, if any.
72+ @Input ()
73+ KeyboardEventHandler escapeHandler;
74+
6575 /// Error to show up in the error section of the dialog.
6676 @Input ()
6777 String error;
@@ -146,6 +156,20 @@ class MaterialDialogComponent implements AfterContentChecked, OnDestroy {
146156 }
147157 }
148158
159+ @override
160+ void handleEscapeKey (KeyboardEvent event) {
161+ if (escapeHandler != null ) {
162+ escapeHandler (event);
163+ }
164+ }
165+
166+ void _defaultEscapeHandler (KeyboardEvent event) {
167+ if (_modal != null ) {
168+ event.preventDefault ();
169+ _modal.close ();
170+ }
171+ }
172+
149173 @override
150174 void ngAfterContentChecked () {
151175 if (_shouldListenForFullscreenChanges) {
0 commit comments