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

[Added] Modal Close on right click at overlay #963

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function App() {
isOpen={modalIsOpen}
onAfterOpen={afterOpenModal}
onRequestClose={closeModal}
onOverlayRightClick={closeModal}
style={customStyles}
contentLabel="Example Modal"
>
Expand Down
6 changes: 6 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ import ReactModal from 'react-modal';
/* Function that will be run when the modal is requested
to be closed (either by clicking on overlay or pressing ESC).
Note: It is not called if isOpen is changed by other means. */}

onOverlayRightClick={
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would propose onOverlayContextMenu as a better name for this prop, per @diasbruno's suggestion here.

We are passing this to React's onContextMenu event handler, which maps to the contextmenu event (see MDN docs):

The contextmenu event fires when the user attempts to open a context menu. This event is typically triggered by clicking the right mouse button, or by pressing the context menu key.

Some alternate ways to trigger the contextmenu event beyond right click:

  • ctrl + click on MacOS
  • shift+f10 on Windows
  • fn+12 on MacOS (with "Alternate Pointer Events" enabled)

...And some screen readers have their own triggers, too.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that if we do decide to rename the prop, it may be worth updating the commits/PR title so that the wording in the changelog is accurate.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fn+12 on MacOS (with "Alternate Pointer Events" enabled)

This one I didn't know. :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, agree. Better to keep the names consistent.

handleRequestCloseFunc
/* Function that will be run when the modal is requested
to be closed (either by right clicking on overlay or pressing ESC).
Note: It is not called if isOpen is changed by other means. */}
Comment on lines +58 to +60
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is out of date given that 06c1c04 changed the prop to be a pass-through to onContextMenu instead of actually closing the modal. It is also not triggered by the esc key.

I would suggest a straightforward comment like:

Function that will be run when the context menu is triggered on the overlay (e.g., by right clicking or by pressing the context menu).


closeTimeoutMS={
0
Expand Down
1 change: 1 addition & 0 deletions examples/bootstrap/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class App extends Component {
closeTimeoutMS={150}
isOpen={this.state.modalIsOpen}
onRequestClose={this.handleModalCloseRequest}
onOverlayRightClick={this.handleModalCloseRequest}
>
<div className="modal-content">
<div className="modal-header">
Expand Down
1 change: 1 addition & 0 deletions src/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Modal extends Component {
]),
onAfterOpen: PropTypes.func,
onRequestClose: PropTypes.func,
onOverlayRightClick: PropTypes.func,
closeTimeoutMS: PropTypes.number,
ariaHideApp: PropTypes.bool,
shouldFocusAfterRender: PropTypes.bool,
Expand Down
13 changes: 12 additions & 1 deletion src/components/ModalPortal.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default class ModalPortal extends Component {
onAfterOpen: PropTypes.func,
onAfterClose: PropTypes.func,
onRequestClose: PropTypes.func,
onOverlayRightClick: PropTypes.func,
closeTimeoutMS: PropTypes.number,
shouldFocusAfterRender: PropTypes.bool,
shouldCloseOnOverlayClick: PropTypes.bool,
Expand Down Expand Up @@ -299,6 +300,15 @@ export default class ModalPortal extends Component {
this.shouldClose = null;
};

handleOverlayRightClick = (event) => {
if (this.shouldClose === null) this.shouldClose = true;
else if (!this.shouldClose) this.shouldClose = null;
if (this.shouldClose) {
event.preventDefault();
this.props.onOverlayRightClick(event);
}
};
diasbruno marked this conversation as resolved.
Show resolved Hide resolved

handleContentOnMouseUp = () => {
this.shouldClose = false;
};
Expand Down Expand Up @@ -375,7 +385,8 @@ export default class ModalPortal extends Component {
className: this.buildClassName("overlay", overlayClassName),
style: { ...overlayStyles, ...this.props.style.overlay },
onClick: this.handleOverlayOnClick,
onMouseDown: this.handleOverlayOnMouseDown
onMouseDown: this.handleOverlayOnMouseDown,
onContextMenu: this.handleOverlayRightClick,
};

const contentProps = {
Expand Down