Skip to content

Commit

Permalink
Restore Modal.setAppElement() functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
evoyy committed Mar 25, 2016
1 parent 1a0a069 commit c13fed9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ var elementClass = require('element-class');
var renderSubtreeIntoContainer = require("react-dom").unstable_renderSubtreeIntoContainer;

var SafeHTMLElement = ExecutionEnvironment.canUseDOM ? window.HTMLElement : {};
var AppElement = ExecutionEnvironment.canUseDOM ? document.body : {appendChild: function() {}};

var Modal = module.exports = React.createClass({

displayName: 'Modal',
statics: {
setAppElement: ariaAppHider.setElement,
setAppElement: function(element) {
AppElement = ariaAppHider.setElement(element);
},
injectCSS: function() {
"production" !== process.env.NODE_ENV
&& console.warn('React-Modal: injectCSS has been deprecated ' +
Expand Down Expand Up @@ -43,7 +46,7 @@ var Modal = module.exports = React.createClass({
componentDidMount: function() {
this.node = document.createElement('div');
this.node.className = 'ReactModalPortal';
document.body.appendChild(this.node);
AppElement.appendChild(this.node);
this.renderPortal(this.props);
},

Expand All @@ -53,7 +56,7 @@ var Modal = module.exports = React.createClass({

componentWillUnmount: function() {
ReactDOM.unmountComponentAtNode(this.node);
document.body.removeChild(this.node);
AppElement.removeChild(this.node);
},

renderPortal: function(props) {
Expand Down
1 change: 1 addition & 0 deletions lib/helpers/ariaAppHider.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function setElement(element) {
element = 'length' in el ? el[0] : el;
}
_element = element || _element;
return _element;
}

function hide(appElement) {
Expand Down
5 changes: 5 additions & 0 deletions specs/Modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ describe('Modal', function () {
var node = document.createElement('div');
Modal.setAppElement(app);
ReactDOM.render(React.createElement(Modal, {isOpen: true}), node);
var modalParent = app.querySelector('.ReactModalPortal').parentNode;
assert.notEqual(modalParent, document.body);
assert.equal(modalParent, app);
equal(app.getAttribute('aria-hidden'), 'true');
ariaAppHider.resetForTesting();
ReactDOM.unmountComponentAtNode(node);
Expand All @@ -53,6 +56,7 @@ describe('Modal', function () {
return React.DOM.div({}, React.createElement(Modal, {isOpen: true, ariaHideApp: false}, 'hello'));
}
});
Modal.setAppElement(document.body);
ReactDOM.render(React.createElement(App), node);
var modalParent = document.body.querySelector('.ReactModalPortal').parentNode;
equal(modalParent, document.body);
Expand All @@ -76,6 +80,7 @@ describe('Modal', function () {
equal(props.closeTimeoutMS, 0);
ReactDOM.unmountComponentAtNode(node);
ariaAppHider.resetForTesting();
Modal.setAppElement(document.body); // restore default
});

it('removes the portal node', function() {
Expand Down

0 comments on commit c13fed9

Please sign in to comment.