Skip to content

Commit 1d495a6

Browse files
spendiasbruno
authored andcommitted
[fixed] Add shouldCloseOnEsc prop
shouldCloseOnEsc: Add prop and logical check to keyHandler shouldCloseOnEsc: Add to docs/README.md
1 parent d98f091 commit 1d495a6

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

Diff for: docs/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ import ReactModal from 'react-modal';
7979
Boolean indicating if the overlay should close the modal
8080
*/
8181
shouldCloseOnOverlayClick={true}
82+
/*
83+
Boolean indicating if pressing the esc key should close the modal
84+
Note: By disabling the esc key from closing the modal you may introduce an accessibility issue.
85+
*/
86+
shouldCloseOnEsc={true}
8287
/*
8388
String indicating the role of the modal, allowing the 'dialog' role to be applied if desired.
8489
*/

Diff for: specs/Modal.events.spec.js

+28-11
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,35 @@ export default () => {
4242
document.activeElement.should.be.eql(content);
4343
});
4444

45-
it("should close on Esc key event", () => {
46-
const requestCloseCallback = sinon.spy();
47-
const modal = renderModal({
48-
isOpen: true,
49-
shouldCloseOnOverlayClick: true,
50-
onRequestClose: requestCloseCallback
45+
describe("shouldCloseOnEsc", () => {
46+
context("when true", () => {
47+
it("should close on Esc key event", () => {
48+
const requestCloseCallback = sinon.spy();
49+
const modal = renderModal({
50+
isOpen: true,
51+
shouldCloseOnEsc: true,
52+
onRequestClose: requestCloseCallback
53+
});
54+
escKeyDown(mcontent(modal));
55+
requestCloseCallback.called.should.be.ok();
56+
// Check if event is passed to onRequestClose callback.
57+
const event = requestCloseCallback.getCall(0).args[0];
58+
event.should.be.ok();
59+
});
60+
});
61+
62+
context("when false", () => {
63+
it("should not close on Esc key event", () => {
64+
const requestCloseCallback = sinon.spy();
65+
const modal = renderModal({
66+
isOpen: true,
67+
shouldCloseOnEsc: false,
68+
onRequestClose: requestCloseCallback
69+
});
70+
escKeyDown(mcontent(modal));
71+
requestCloseCallback.called.should.be.false;
72+
});
5173
});
52-
escKeyDown(mcontent(modal));
53-
requestCloseCallback.called.should.be.ok();
54-
// Check if event is passed to onRequestClose callback.
55-
const ev = requestCloseCallback.getCall(0).args[0];
56-
ev.should.be.ok();
5774
});
5875

5976
describe("shouldCloseOnoverlayClick", () => {

Diff for: src/components/Modal.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ export default class Modal extends Component {
4343
parentSelector: PropTypes.func,
4444
aria: PropTypes.object,
4545
role: PropTypes.string,
46-
contentLabel: PropTypes.string
46+
contentLabel: PropTypes.string,
47+
shouldCloseOnEsc: PropTypes.bool
4748
};
4849
/* eslint-enable react/no-unused-prop-types */
4950

@@ -54,6 +55,7 @@ export default class Modal extends Component {
5455
ariaHideApp: true,
5556
closeTimeoutMS: 0,
5657
shouldFocusAfterRender: true,
58+
shouldCloseOnEsc: true,
5759
shouldCloseOnOverlayClick: true,
5860
parentSelector() {
5961
return document.body;

Diff for: src/components/ModalPortal.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ export default class ModalPortal extends Component {
4747
role: PropTypes.string,
4848
contentLabel: PropTypes.string,
4949
aria: PropTypes.object,
50-
children: PropTypes.node
50+
children: PropTypes.node,
51+
shouldCloseOnEsc: PropTypes.bool
5152
};
5253

5354
constructor(props) {
@@ -195,7 +196,8 @@ export default class ModalPortal extends Component {
195196
if (event.keyCode === TAB_KEY) {
196197
scopeTab(this.content, event);
197198
}
198-
if (event.keyCode === ESC_KEY) {
199+
200+
if (this.props.shouldCloseOnEsc && event.keyCode === ESC_KEY) {
199201
event.preventDefault();
200202
this.requestClose(event);
201203
}

0 commit comments

Comments
 (0)