Skip to content

Commit 25fa42d

Browse files
add modal-open class to modal container when open
1 parent 0793e12 commit 25fa42d

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

docs/examples/ModalStatic.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var modalInstance = (
77
<Modal title="Modal title"
88
backdrop={false}
99
animation={false}
10+
container={mountNode}
1011
onRequestHide={handleHide}>
1112
<div className="modal-body">
1213
One fine body...

src/Modal.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ var Modal = React.createClass({
124124
this._onDocumentKeyupListener =
125125
EventListener.listen(document, 'keyup', this.handleDocumentKeyUp);
126126

127+
var container = (this.props.container && this.props.container.getDOMNode()) || document.body;
128+
container.className += container.className.length ? ' modal-open' : 'modal-open';
129+
127130
if (this.props.backdrop) {
128131
this.iosClickHack();
129132
}
@@ -137,6 +140,8 @@ var Modal = React.createClass({
137140

138141
componentWillUnmount: function () {
139142
this._onDocumentKeyupListener.remove();
143+
var container = (this.props.container && this.props.container.getDOMNode()) || document.body;
144+
container.className = container.className.replace(/ ?modal-open/, '');
140145
},
141146

142147
handleBackdropClick: function (e) {

test/ModalSpec.jsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,39 @@ describe('Modal', function () {
1616
assert.ok(ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'strong'));
1717
});
1818

19+
it('Should add modal-open class to the modal container while open', function(done) {
20+
21+
var Container = React.createClass({
22+
getInitialState: function() {
23+
return {modalOpen: true};
24+
},
25+
handleCloseModal: function() {
26+
this.setState({modalOpen: false});
27+
},
28+
render: function() {
29+
if (this.state.modalOpen) {
30+
return <Modal onRequestHide={this.handleCloseModal} container={this}>
31+
<strong>Message</strong>
32+
</Modal>;
33+
} else {
34+
return <span/>;
35+
}
36+
}
37+
});
38+
var instance = ReactTestUtils.renderIntoDocument(
39+
<Container />
40+
);
41+
assert.ok(instance.getDOMNode().className.match(/\modal-open\b/));
42+
43+
var backdrop = instance.getDOMNode().getElementsByClassName('modal-backdrop')[0];
44+
ReactTestUtils.Simulate.click(backdrop);
45+
setTimeout(function(){
46+
assert.equal(instance.getDOMNode().className.length, 0);
47+
done();
48+
}, 0);
49+
50+
});
51+
1952
it('Should close the modal when the backdrop is clicked', function (done) {
2053
var doneOp = function () { done(); };
2154
var instance = ReactTestUtils.renderIntoDocument(

0 commit comments

Comments
 (0)