Skip to content

Commit a16949b

Browse files
committed
[Added] Modal Close on right click at overlay
1 parent 4030935 commit a16949b

File tree

5 files changed

+21
-1
lines changed

5 files changed

+21
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ function App() {
9393
isOpen={modalIsOpen}
9494
onAfterOpen={afterOpenModal}
9595
onRequestClose={closeModal}
96+
onOverlayRightClick={closeModal}
9697
style={customStyles}
9798
contentLabel="Example Modal"
9899
>

docs/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ import ReactModal from 'react-modal';
5252
/* Function that will be run when the modal is requested
5353
to be closed (either by clicking on overlay or pressing ESC).
5454
Note: It is not called if isOpen is changed by other means. */}
55+
56+
onOverlayRightClick={
57+
handleRequestCloseFunc
58+
/* Function that will be run when the modal is requested
59+
to be closed (either by right clicking on overlay or pressing ESC).
60+
Note: It is not called if isOpen is changed by other means. */}
5561

5662
closeTimeoutMS={
5763
0

examples/bootstrap/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class App extends Component {
3939
closeTimeoutMS={150}
4040
isOpen={this.state.modalIsOpen}
4141
onRequestClose={this.handleModalCloseRequest}
42+
onOverlayRightClick={this.handleModalCloseRequest}
4243
>
4344
<div className="modal-content">
4445
<div className="modal-header">

src/components/Modal.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class Modal extends Component {
6666
]),
6767
onAfterOpen: PropTypes.func,
6868
onRequestClose: PropTypes.func,
69+
onOverlayRightClick: PropTypes.func,
6970
closeTimeoutMS: PropTypes.number,
7071
ariaHideApp: PropTypes.bool,
7172
shouldFocusAfterRender: PropTypes.bool,

src/components/ModalPortal.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default class ModalPortal extends Component {
5555
onAfterOpen: PropTypes.func,
5656
onAfterClose: PropTypes.func,
5757
onRequestClose: PropTypes.func,
58+
onOverlayRightClick: PropTypes.func,
5859
closeTimeoutMS: PropTypes.number,
5960
shouldFocusAfterRender: PropTypes.bool,
6061
shouldCloseOnOverlayClick: PropTypes.bool,
@@ -299,6 +300,15 @@ export default class ModalPortal extends Component {
299300
this.shouldClose = null;
300301
};
301302

303+
handleOverlayRightClick = (event) => {
304+
if (this.shouldClose === null) this.shouldClose = true;
305+
else if (!this.shouldClose) this.shouldClose = null;
306+
if (this.shouldClose) {
307+
event.preventDefault();
308+
this.props.onOverlayRightClick(event);
309+
}
310+
};
311+
302312
handleContentOnMouseUp = () => {
303313
this.shouldClose = false;
304314
};
@@ -375,7 +385,8 @@ export default class ModalPortal extends Component {
375385
className: this.buildClassName("overlay", overlayClassName),
376386
style: { ...overlayStyles, ...this.props.style.overlay },
377387
onClick: this.handleOverlayOnClick,
378-
onMouseDown: this.handleOverlayOnMouseDown
388+
onMouseDown: this.handleOverlayOnMouseDown,
389+
onContextMenu: this.handleOverlayRightClick,
379390
};
380391

381392
const contentProps = {

0 commit comments

Comments
 (0)