Skip to content

Commit 7ada8cf

Browse files
TheSharpieOneeddywashere
authored andcommitted
feat(Modal): allow for multiple modals (#138)
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.
1 parent 7bf5d0a commit 7ada8cf

2 files changed

Lines changed: 4 additions & 11 deletions

File tree

src/Modal.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ class Modal extends React.Component {
9797

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

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

111-
removeEvents() {
112-
document.removeEventListener('click', this.handleBackdropClick, true);
113-
document.removeEventListener('keyup', this.handleEscape, false);
114-
}
115-
116110
hide() {
117111
this.renderIntoSubtree();
118-
this.removeEvents();
119112
}
120113

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

126119
document.body.appendChild(this._element);
127-
document.addEventListener('click', this.handleBackdropClick, true);
128-
document.addEventListener('keyup', this.handleEscape, false);
129120

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

145136
// check if modal should receive focus
146137
if (this._focus) {
147-
this._element.focus();
138+
this._dialog.parentNode.focus();
148139
this._focus = false;
149140
}
150141
}
@@ -160,6 +151,8 @@ class Modal extends React.Component {
160151
transitionAppearTimeout={300}
161152
transitionEnterTimeout={300}
162153
transitionLeaveTimeout={300}
154+
onClickCapture={this.handleBackdropClick}
155+
onKeyUp={this.handleEscape}
163156
className="modal"
164157
style={{ display: 'block' }}
165158
tabIndex="-1"

test/Modal.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ describe('Modal', () => {
344344

345345
expect(isOpen).toBe(true);
346346

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

350350
expect(isOpen).toBe(false);

0 commit comments

Comments
 (0)