Skip to content
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

feat(Modal): Allow passing in an element selector to append modal to #1817

Merged
merged 3 commits into from Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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