Skip to content

Commit

Permalink
feat(Modal): Allow passing in an element selector to append mod… (#1817)
Browse files Browse the repository at this point in the history
* feat(Modal): Allow passing in an element selector to append modal to

* feat(Modal): Rename  property to

* Update Modal.js

Make modal fallback to body if the specified container is not found

Co-authored-by: Evan Sharp <dumbdrum@gmail.com>
  • Loading branch information
ramoncaldeira and TheSharpieOne committed Apr 22, 2020
1 parent 1595071 commit 8f8cc98
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
4 changes: 3 additions & 1 deletion docs/lib/Components/ModalsPage.js
Expand Up @@ -106,7 +106,9 @@ const ModalsPage = () => {
// if modal should be destructed/removed from DOM after closing
unmountOnClose: PropTypes.bool, // defaults to true
// if the element which triggered the modal to open should focused after the modal closes (see example somewhere below)
returnFocusAfterClose: PropTypes.bool // defaults to true
returnFocusAfterClose: PropTypes.bool, // defaults to true
// container to append the modal to
container: PropTypes.string // css selector, defauls to "body"
}`}
</PrismCode>
</pre>
Expand Down
11 changes: 7 additions & 4 deletions src/Modal.js
Expand Up @@ -57,7 +57,8 @@ const propTypes = {
PropTypes.func,
]),
unmountOnClose: PropTypes.bool,
returnFocusAfterClose: PropTypes.bool
returnFocusAfterClose: PropTypes.bool,
container: PropTypes.string
};

const propsToOmit = Object.keys(propTypes);
Expand All @@ -82,7 +83,8 @@ const defaultProps = {
timeout: TransitionTimeouts.Fade, // uses standard fade transition
},
unmountOnClose: true,
returnFocusAfterClose: true
returnFocusAfterClose: true,
container: 'body'
};

class Modal extends React.Component {
Expand Down Expand Up @@ -290,7 +292,8 @@ class Modal extends React.Component {
this._element.setAttribute('tabindex', '-1');
this._element.style.position = 'relative';
this._element.style.zIndex = this.props.zIndex;
document.body.appendChild(this._element);
this._mountContainer = document.querySelector(this.props.container) || document.querySelector('body');
this._mountContainer.appendChild(this._element);
}

this._originalBodyPadding = getOriginalBodyPadding();
Expand All @@ -308,7 +311,7 @@ class Modal extends React.Component {

destroy() {
if (this._element) {
document.body.removeChild(this._element);
this._mountContainer.removeChild(this._element);
this._element = null;
}

Expand Down

0 comments on commit 8f8cc98

Please sign in to comment.