Skip to content

Commit

Permalink
feat(Modal): allow for multiple modals (#138)
Browse files Browse the repository at this point in the history
Resolves #137
Moves the click and keyup listeners from the document to the
`.modal` div, which wraps the modal-dialog. This allows for each
modal to be individually triggered to close.
  • Loading branch information
TheSharpieOne authored and eddywashere committed Sep 14, 2016
1 parent 7bf5d0a commit 7ada8cf
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
13 changes: 3 additions & 10 deletions src/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ class Modal extends React.Component {

destroy() {
const classes = document.body.className.replace('modal-open', '');
this.removeEvents();

if (this._element) {
ReactDOM.unmountComponentAtNode(this._element);
Expand All @@ -108,14 +107,8 @@ class Modal extends React.Component {
document.body.className = classNames(classes).trim();
}

removeEvents() {
document.removeEventListener('click', this.handleBackdropClick, true);
document.removeEventListener('keyup', this.handleEscape, false);
}

hide() {
this.renderIntoSubtree();
this.removeEvents();
}

show() {
Expand All @@ -124,8 +117,6 @@ class Modal extends React.Component {
this._element.setAttribute('tabindex', '-1');

document.body.appendChild(this._element);
document.addEventListener('click', this.handleBackdropClick, true);
document.addEventListener('keyup', this.handleEscape, false);

document.body.className = classNames(
classes,
Expand All @@ -144,7 +135,7 @@ class Modal extends React.Component {

// check if modal should receive focus
if (this._focus) {
this._element.focus();
this._dialog.parentNode.focus();
this._focus = false;
}
}
Expand All @@ -160,6 +151,8 @@ class Modal extends React.Component {
transitionAppearTimeout={300}
transitionEnterTimeout={300}
transitionLeaveTimeout={300}
onClickCapture={this.handleBackdropClick}
onKeyUp={this.handleEscape}
className="modal"
style={{ display: 'block' }}
tabIndex="-1"
Expand Down
2 changes: 1 addition & 1 deletion test/Modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ describe('Modal', () => {

expect(isOpen).toBe(true);

document.getElementsByClassName('modal-backdrop')[0].click();
document.getElementsByClassName('modal')[0].click();
jasmine.clock().tick(300);

expect(isOpen).toBe(false);
Expand Down

0 comments on commit 7ada8cf

Please sign in to comment.