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

Commit c60982b

Browse files
FjVillarSplaktar
authored andcommitted
feat(sidenav): add disable click and escape event (#11165)
1 parent ae3fb56 commit c60982b

File tree

6 files changed

+106
-6
lines changed

6 files changed

+106
-6
lines changed

src/components/sidenav/demoBasicUsage/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
angular
2-
.module('sidenavDemo1', ['ngMaterial'])
2+
.module('basicUsageSidenavDemo', ['ngMaterial'])
33
.controller('AppCtrl', function ($scope, $timeout, $mdSidenav, $log) {
44
$scope.toggleLeft = buildDelayedToggler('left');
55
$scope.toggleRight = buildToggler('right');

src/components/sidenav/demoCustomSidenav/script.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
angular
2-
.module('sidenavDemo2', ['ngMaterial'])
3-
.controller('AppCtrl', function ($scope, $timeout, $mdSidenav) {
2+
.module('customSidenavDemo', ['ngMaterial'])
3+
.controller('AppCtrl', function ($scope, $mdSidenav) {
44
$scope.toggleLeft = buildToggler('left');
5-
$scope.toggleRight = buildToggler('right');
65

76
function buildToggler(componentId) {
87
return function() {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<div ng-controller="AppCtrl" layout="column" style="height: 500px;" ng-cloak>
2+
3+
<section layout="row" flex>
4+
5+
<md-sidenav class="md-sidenav-left" md-component-id="closeEventsDisabled"
6+
md-whiteframe="4" md-disable-close-events>
7+
8+
<md-toolbar class="md-theme-indigo">
9+
<h1 class="md-toolbar-tools">Disabled Close Events</h1>
10+
</md-toolbar>
11+
12+
<md-content layout-margin="">
13+
<p>
14+
This sidenav is showing the backdrop, but users can't close the
15+
sidenav by clicking on the backdrop or pressing the 'Escape' key.
16+
</p>
17+
<md-button ng-click="toggleSidenav()" class="md-accent">
18+
Close this Sidenav
19+
</md-button>
20+
</md-content>
21+
22+
</md-sidenav>
23+
24+
<md-content flex layout-padding>
25+
26+
<div layout="column" layout-align="top center">
27+
<p>
28+
Developers can disable closing the sidenav on backdrop clicks and
29+
'Escape' key events.<br/>
30+
</p>
31+
32+
<div>
33+
<md-button ng-click="toggleSidenav()" class="md-raised md-primary">
34+
Open Sidenav
35+
</md-button>
36+
</div>
37+
38+
</div>
39+
40+
</md-content>
41+
42+
</section>
43+
44+
</div>
45+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
angular
2+
.module('disableCloseEventsSidenavDemo', ['ngMaterial'])
3+
.controller('AppCtrl', function ($scope, $mdSidenav) {
4+
$scope.toggleSidenav = buildToggler('closeEventsDisabled');
5+
6+
function buildToggler(componentId) {
7+
return function() {
8+
$mdSidenav(componentId).toggle();
9+
};
10+
}
11+
});

src/components/sidenav/sidenav.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ function SidenavFocusDirective() {
231231
*
232232
* @param {expression=} md-is-open A model bound to whether the sidenav is opened.
233233
* @param {boolean=} md-disable-backdrop When present in the markup, the sidenav will not show a backdrop.
234+
* @param {boolean=} md-disable-close-events When present in the markup, clicking the backdrop or pressing the 'Escape' key will not close the sidenav.
234235
* @param {string=} md-component-id componentId to use with $mdSidenav service.
235236
* @param {expression=} md-is-locked-open When this expression evaluates to true,
236237
* the sidenav 'locks open': it falls into the content's flow instead
@@ -302,6 +303,12 @@ function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $mdInterac
302303
if (!attr.hasOwnProperty('mdDisableBackdrop')) {
303304
backdrop = $mdUtil.createBackdrop(scope, "md-sidenav-backdrop md-opaque ng-enter");
304305
}
306+
307+
// If md-disable-close-events is set on the sidenav we will disable
308+
// backdrop click and Escape key events
309+
if (attr.hasOwnProperty('mdDisableCloseEvents')) {
310+
var disableCloseEvents = true;
311+
}
305312

306313
element.addClass('_md'); // private md component indicator for styling
307314
$mdTheming(element);
@@ -351,8 +358,12 @@ function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $mdInterac
351358
var focusEl = $mdUtil.findFocusTarget(element) || $mdUtil.findFocusTarget(element,'[md-sidenav-focus]') || element;
352359
var parent = element.parent();
353360

354-
parent[isOpen ? 'on' : 'off']('keydown', onKeyDown);
355-
if (backdrop) backdrop[isOpen ? 'on' : 'off']('click', close);
361+
// If the user hasn't set the disable close events property we are adding
362+
// click and escape events to close the sidenav
363+
if ( !disableCloseEvents ) {
364+
parent[isOpen ? 'on' : 'off']('keydown', onKeyDown);
365+
if (backdrop) backdrop[isOpen ? 'on' : 'off']('click', close);
366+
}
356367

357368
var restorePositioning = updateContainerPositions(parent, isOpen);
358369

src/components/sidenav/sidenav.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,40 @@ describe('mdSidenav', function() {
5959
expect($rootScope.show).toBe(false);
6060
}));
6161

62+
describe('disable click and Escape key events if md-disable-close-events is set to true',
63+
function() {
64+
it('should not close on escape and still show the backdrop',
65+
inject(function($rootScope, $material, $mdConstant, $timeout) {
66+
var el = setup('md-is-open="show" md-disable-close-events');
67+
$rootScope.$apply('show = true');
68+
69+
$material.flushOutstandingAnimations();
70+
el.parent().triggerHandler({
71+
type: 'keydown',
72+
keyCode: $mdConstant.KEY_CODE.ESCAPE
73+
});
74+
$timeout.flush();
75+
var backdrop = el.parent().find('md-backdrop');
76+
77+
expect($rootScope.show).toBe(true);
78+
expect(backdrop.length).toBe(1);
79+
}));
80+
81+
it('should not close on backdrop click and still show the backdrop',
82+
inject(function($rootScope, $material, $timeout) {
83+
var el = setup('md-is-open="show" md-disable-close-events');
84+
$rootScope.$apply('show = true');
85+
86+
$material.flushOutstandingAnimations();
87+
el.parent().find('md-backdrop').triggerHandler('click');
88+
$timeout.flush();
89+
var backdrop = el.parent().find('md-backdrop');
90+
91+
expect($rootScope.show).toBe(true);
92+
expect(backdrop.length).toBe(1);
93+
}));
94+
});
95+
6296
it('should show a backdrop by default', inject(function($rootScope, $material) {
6397
var el = setup('md-is-open="show"');
6498
$rootScope.$apply('show = true');

0 commit comments

Comments
 (0)