Skip to content

Commit 3f20eea

Browse files
committed
feat(modal): add ability to change class on body
- Add support for `openedClass` to allow overriding the default modal class added & removed on the `body` element (`modal-open`) Closes angular-ui#2633
1 parent abfd26c commit 3f20eea

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/modal/docs/readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The `$modal` service has only one method: `open(options)` where available option
1717
* `windowClass` - additional CSS class(es) to be added to a modal window template
1818
* `windowTemplateUrl` - a path to a template overriding modal's window template
1919
* `size` - optional suffix of modal window class. The value used is appended to the `modal-` class, i.e. a value of `sm` gives `modal-sm`
20+
* `openedClass` - class added to the `body` element when the modal is opened. Defaults to `modal-open`
2021

2122
Global defaults may be set for `$modal` via `$modalProvider.options`.
2223

src/modal/modal.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ angular.module('ui.bootstrap.modal', [])
272272
openedWindows.remove(modalInstance);
273273

274274
removeAfterAnimate(modalWindow.modalDomEl, modalWindow.modalScope, function() {
275-
body.toggleClass(OPENED_MODAL_CLASS, openedWindows.length() > 0);
275+
body.toggleClass(modalInstance.openedClass || OPENED_MODAL_CLASS, openedWindows.length() > 0);
276276
});
277277
checkRemoveBackdrop();
278278

@@ -385,7 +385,8 @@ angular.module('ui.bootstrap.modal', [])
385385
renderDeferred: modal.renderDeferred,
386386
modalScope: modal.scope,
387387
backdrop: modal.backdrop,
388-
keyboard: modal.keyboard
388+
keyboard: modal.keyboard,
389+
openedClass: modal.openedClass
389390
});
390391

391392
var body = $document.find('body').eq(0),
@@ -419,7 +420,7 @@ angular.module('ui.bootstrap.modal', [])
419420
openedWindows.top().value.modalDomEl = modalDomEl;
420421
openedWindows.top().value.modalOpener = modalOpener;
421422
body.append(modalDomEl);
422-
body.addClass(OPENED_MODAL_CLASS);
423+
body.addClass(modal.openedClass || OPENED_MODAL_CLASS);
423424
$modalStack.clearFocusListCache();
424425
};
425426

@@ -619,7 +620,8 @@ angular.module('ui.bootstrap.modal', [])
619620
backdropClass: modalOptions.backdropClass,
620621
windowClass: modalOptions.windowClass,
621622
windowTemplateUrl: modalOptions.windowTemplateUrl,
622-
size: modalOptions.size
623+
size: modalOptions.size,
624+
openedClass: modalOptions.openedClass
623625
});
624626

625627
}, function resolveError(reason) {

src/modal/test/modal.spec.js

+23
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,29 @@ describe('$modal', function () {
752752

753753
});
754754

755+
describe('openedClass', function () {
756+
757+
it('should add the modal-open class to the body element by default', function () {
758+
open({
759+
template: '<div>dummy modal</div>'
760+
});
761+
762+
expect($document.find('body')).toHaveClass('modal-open');
763+
});
764+
765+
it('should add the custom class to the body element', function () {
766+
open({
767+
template: '<div>dummy modal</div>',
768+
openedClass: 'foo'
769+
});
770+
771+
var body = $document.find('body');
772+
773+
expect(body).toHaveClass('foo');
774+
expect(body).not.toHaveClass('modal-open');
775+
});
776+
});
777+
755778
});
756779

757780
describe('multiple modals', function () {

0 commit comments

Comments
 (0)