Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit d4b4480

Browse files
Marcy Suttonajoslin
authored andcommitted
feat(mdBottomSheet): add escape to close functionality
1 parent 477151a commit d4b4480

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/components/bottomSheet/bottomSheet.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,15 @@ function MdBottomSheetProvider($$interimElementProvider) {
117117

118118
/* @ngInject */
119119
function bottomSheetDefaults($animate, $mdConstant, $timeout, $$rAF, $compile, $mdTheming,
120-
$mdBottomSheet) {
120+
$mdBottomSheet, $rootElement) {
121121
var backdrop;
122122

123123
return {
124124
themable: true,
125125
targetEvent: null,
126126
onShow: onShow,
127127
onRemove: onRemove,
128+
escapeToClose: true
128129
};
129130

130131
function onShow(scope, element, options) {
@@ -153,6 +154,15 @@ function MdBottomSheetProvider($$interimElementProvider) {
153154
element[0].querySelector('[ng-click]')
154155
);
155156
focusableItems.eq(0).focus();
157+
158+
if (options.escapeToClose) {
159+
options.rootElementKeyupCallback = function(e) {
160+
if (e.keyCode === $mdConstant.KEY_CODE.ESCAPE) {
161+
$timeout($mdBottomSheet.cancel);
162+
}
163+
};
164+
$rootElement.on('keyup', options.rootElementKeyupCallback);
165+
}
156166
});
157167

158168
}
Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,44 @@
11
describe('$mdBottomSheet service', function() {
2-
beforeEach(module('material.components.mdBottomSheet', 'ngAnimateMock'));
2+
beforeEach(module('material.components.bottomSheet', 'ngAnimateMock'));
3+
4+
describe('#build()', function() {
5+
it('should escapeToClose == true', inject(function($mdBottomSheet, $rootScope, $rootElement, $timeout, $animate, $mdConstant) {
6+
var parent = angular.element('<div>');
7+
$mdBottomSheet.show({
8+
template: '<md-bottom-sheet>',
9+
parent: parent,
10+
escapeToClose: true
11+
});
12+
$rootScope.$apply();
13+
14+
$animate.triggerCallbacks();
15+
16+
expect(parent.find('md-bottom-sheet').length).toBe(1);
17+
18+
$rootElement.triggerHandler({type: 'keyup',
19+
keyCode: $mdConstant.KEY_CODE.ESCAPE
20+
});
21+
22+
$timeout.flush();
23+
expect(parent.find('md-bottom-sheet').length).toBe(0);
24+
}));
25+
26+
it('should escapeToClose == false', inject(function($mdBottomSheet, $rootScope, $rootElement, $timeout, $animate, $mdConstant) {
27+
var parent = angular.element('<div>');
28+
$mdBottomSheet.show({
29+
template: '<md-bottom-sheet>',
30+
parent: parent,
31+
escapeToClose: false
32+
});
33+
$rootScope.$apply();
34+
35+
$animate.triggerCallbacks();
36+
37+
expect(parent.find('md-bottom-sheet').length).toBe(1);
38+
39+
$rootElement.triggerHandler({ type: 'keyup', keyCode: $mdConstant.KEY_CODE.ESCAPE });
40+
41+
expect(parent.find('md-bottom-sheet').length).toBe(1);
42+
}));
43+
});
344
});

0 commit comments

Comments
 (0)