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

Commit e76512f

Browse files
feat(modal): add modal-open class to body on modal open
Closes #1254
1 parent 709e679 commit e76512f

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

misc/test-lib/helpers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ beforeEach(function() {
44
this.addMatchers({
55
toHaveClass: function(cls) {
66
this.message = function() {
7-
return "Expected '" + angular.mock.dump(this.actual) + "' to have class '" + cls + "'.";
7+
return "Expected '" + this.actual + "'" + (this.isNot ? ' not ' : ' ') + "to have class '" + cls + "'.";
88
};
99

1010
return this.actual.hasClass(cls);

src/modal/modal.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ angular.module('ui.bootstrap.modal', [])
107107
.factory('$modalStack', ['$document', '$compile', '$rootScope', '$$stackedMap',
108108
function ($document, $compile, $rootScope, $$stackedMap) {
109109

110+
var OPENED_MODAL_CLASS = 'modal-open';
111+
110112
var backdropjqLiteEl, backdropDomEl;
111113
var backdropScope = $rootScope.$new(true);
112114
var openedWindows = $$stackedMap.createNew();
@@ -129,13 +131,15 @@ angular.module('ui.bootstrap.modal', [])
129131

130132
function removeModalWindow(modalInstance) {
131133

134+
var body = $document.find('body').eq(0);
132135
var modalWindow = openedWindows.get(modalInstance).value;
133136

134137
//clean up the stack
135138
openedWindows.remove(modalInstance);
136139

137140
//remove window DOM element
138141
modalWindow.modalDomEl.remove();
142+
body.toggleClass(OPENED_MODAL_CLASS, openedWindows.length() > 0);
139143

140144
//remove backdrop if no longer needed
141145
if (backdropDomEl && backdropIndex() == -1) {
@@ -185,7 +189,7 @@ angular.module('ui.bootstrap.modal', [])
185189
var modalDomEl = $compile(angularDomEl)(modal.scope);
186190
openedWindows.top().value.modalDomEl = modalDomEl;
187191
body.append(modalDomEl);
188-
192+
body.addClass(OPENED_MODAL_CLASS);
189193
};
190194

191195
$modalStack.close = function (modalInstance, result) {

src/modal/test/modal.spec.js

+19
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ describe('$modal', function () {
8888
var body = $document.find('body');
8989
body.find('div.modal').remove();
9090
body.find('div.modal-backdrop').remove();
91+
body.removeClass('modal-open');
9192
});
9293

9394
function open(modalOptions) {
@@ -435,5 +436,23 @@ describe('$modal', function () {
435436

436437
expect($document).toHaveBackdrop();
437438
});
439+
440+
it('should add "modal-open" class when a modal gets opened', function () {
441+
442+
var body = $document.find('body');
443+
expect(body).not.toHaveClass('modal-open');
444+
445+
var modal1 = open({template: '<div>Content1</div>'});
446+
expect(body).toHaveClass('modal-open');
447+
448+
var modal2 = open({template: '<div>Content1</div>'});
449+
expect(body).toHaveClass('modal-open');
450+
451+
dismiss(modal1);
452+
expect(body).toHaveClass('modal-open');
453+
454+
dismiss(modal2);
455+
expect(body).not.toHaveClass('modal-open');
456+
});
438457
});
439458
});

0 commit comments

Comments
 (0)