Skip to content
This repository was archived by the owner on May 20, 2023. It is now read-only.

Commit 75ae063

Browse files
cissyshinshahan
authored andcommitted
Close material dialog's parent modal on escape key by default.
PiperOrigin-RevId: 225044834
1 parent 027f1d9 commit 75ae063

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

angular_components/lib/material_dialog/material_dialog.dart

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'dart:html';
88
import 'package:angular/angular.dart';
99
import 'package:angular_components/focus/focus_trap.dart';
1010
import 'package:angular_components/laminate/components/modal/modal.dart';
11+
import 'package:angular_components/model/a11y/keyboard_handler_mixin.dart';
1112
import 'package:angular_components/utils/browser/dom_service/dom_service.dart';
1213
import '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) {

angular_components/lib/material_dialog/material_dialog.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
for details. All rights reserved. Use of this source code is governed by a
44
BSD-style license that can be found in the LICENSE file.
55
-->
6-
<focus-trap>
6+
<focus-trap (keyup)="onKeyUp($event)">
77
<div class="wrapper">
88
<header *ngIf="shouldShowHeader">
99
<ng-content select="[header]"></ng-content>

angular_components/lib/model/a11y/keyboard_handler_mixin.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import 'dart:html';
66

77
import 'package:angular_components/utils/browser/events/events.dart';
88

9+
/// Callback to handle a keyboard event
10+
typedef void KeyboardEventHandler(KeyboardEvent event);
11+
912
/// Base class for handling common key events implementing accessibility best
1013
/// practices.
1114
///

0 commit comments

Comments
 (0)