Skip to content

Commit

Permalink
Merge pull request reactjs#108 from evoyy/pr/override-anchor-to-docum…
Browse files Browse the repository at this point in the history
…ent-body

[fixed] Restore Modal.setAppElement() functionality
  • Loading branch information
claydiffrient committed Mar 26, 2016
2 parents b5e38cf + c13fed9 commit fe46c63
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 @@ -45,7 +48,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 @@ -55,7 +58,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 @@ -31,6 +31,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 @@ -54,6 +57,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 @@ -78,6 +82,7 @@ describe('Modal', function () {
equal(props.shouldCloseOnOverlayClick, true);
ReactDOM.unmountComponentAtNode(node);
ariaAppHider.resetForTesting();
Modal.setAppElement(document.body); // restore default
});

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

0 comments on commit fe46c63

Please sign in to comment.