Skip to content

Ability to anchor modals relatively to its context rather than just the body. #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var Modal = module.exports = React.createClass({
},

propTypes: {
relative: React.PropTypes.bool,
isOpen: React.PropTypes.bool.isRequired,
style: React.PropTypes.shape({
content: React.PropTypes.object,
Expand All @@ -34,16 +35,18 @@ var Modal = module.exports = React.createClass({

getDefaultProps: function () {
return {
relative: false,
isOpen: false,
ariaHideApp: true,
closeTimeoutMS: 0
};
},

componentDidMount: function() {
this.anchor = (this.props.relative) ? ReactDOM.findDOMNode(this).parentNode : document.body;
this.node = document.createElement('div');
this.node.className = 'ReactModalPortal';
document.body.appendChild(this.node);
this.anchor.appendChild(this.node);
this.renderPortal(this.props);
},

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

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

renderPortal: function(props) {
if (props.isOpen) {
elementClass(document.body).add('ReactModal__Body--open');
elementClass(this.anchor).add('ReactModal__Body--open');
} else {
elementClass(document.body).remove('ReactModal__Body--open');
elementClass(this.anchor).remove('ReactModal__Body--open');
}

if (props.ariaHideApp) {
Expand Down
50 changes: 26 additions & 24 deletions lib/components/ModalPortal.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,30 @@ var CLASS_NAMES = {
}
};

var defaultStyles = {
overlay: {
position : 'fixed',
top : 0,
left : 0,
right : 0,
bottom : 0,
backgroundColor : 'rgba(255, 255, 255, 0.75)'
},
content: {
position : 'absolute',
top : '40px',
left : '40px',
right : '40px',
bottom : '40px',
border : '1px solid #ccc',
background : '#fff',
overflow : 'auto',
WebkitOverflowScrolling : 'touch',
borderRadius : '4px',
outline : 'none',
padding : '20px'
function defaultStyles(relative) {
return {
overlay: {
position : relative ? 'absolute' : 'fixed',
top : 0,
left : 0,
right : 0,
bottom : 0,
backgroundColor : 'rgba(255, 255, 255, 0.75)'
},
content: {
position : 'absolute',
top : '40px',
left : '40px',
right : '40px',
bottom : '40px',
border : '1px solid #ccc',
background : '#fff',
overflow : 'auto',
WebkitOverflowScrolling : 'touch',
borderRadius : '4px',
outline : 'none',
padding : '20px'
}
}
};

Expand Down Expand Up @@ -179,12 +181,12 @@ var ModalPortal = module.exports = React.createClass({
div({
ref: "overlay",
className: this.buildClassName('overlay', this.props.overlayClassName),
style: Assign({}, defaultStyles.overlay, this.props.style.overlay || {}),
style: Assign({}, defaultStyles(this.props.relative).overlay, this.props.style.overlay || {}),
onClick: this.handleOverlayClick
},
div({
ref: "content",
style: Assign({}, defaultStyles.content, this.props.style.content || {}),
style: Assign({}, defaultStyles(this.props.relative).content, this.props.style.content || {}),
className: this.buildClassName('content', this.props.className),
tabIndex: "-1",
onClick: stopPropagation,
Expand Down
25 changes: 25 additions & 0 deletions specs/Modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ describe('Modal', function () {
ReactDOM.unmountComponentAtNode(node);
});

it('relative modals render into context, not in body', function() {
var node = document.createElement('div');
var App = React.createClass({
render: function() {
return React.DOM.div({ id: 'context' }, React.createElement(Modal, {isOpen: true, relative: true}, 'hello'));
}
});
ReactDOM.render(React.createElement(App), node);
var modalParent = node.querySelector('.ReactModalPortal').parentNode;
equal(modalParent.id, 'context')
ReactDOM.unmountComponentAtNode(node)
});

it('modal overlays have default fixed position', function() {
var modal = renderModal({isOpen: true});
equal(modal.portal.refs.overlay.style.position, 'fixed');
unmountModal();
});

it('relative modal overlays have default absolute position', function() {
var modal = renderModal({isOpen: true, relative: true});
equal(modal.portal.refs.overlay.style.position, 'absolute');
unmountModal();
});

it('renders into the body, not in context', function() {
var node = document.createElement('div');
var App = React.createClass({
Expand Down