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 all commits
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
3 changes: 2 additions & 1 deletion examples/basic/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const examples = [
Forms,
MultipleModals,
NestedModals,
ReactRouter
ReactRouter,
ModalClose
];

class App extends Component {
Expand Down
50 changes: 50 additions & 0 deletions examples/basic/modal_close_right_click/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { Component } from "react";
import Modal from "react-modal";

class ModalClose extends Component {
constructor(props) {
super(props);
this.state = {
isOpen: false,
};
}

toggleModal = (event) => {
this.setState({ isOpen: !this.state.isOpen });
};
handleRightClick = (event) => {
this.setState({ isOpen: false });
};
Comment on lines +12 to +17
Copy link

Choose a reason for hiding this comment

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

Nit: missing a newline in between these two functions.


render() {
return (
<div>
<button
type="button"
className="btn btn-primary"
onClick={this.toggleModal}
>
Open Modal
</button>
<Modal
isOpen={this.state.isOpen}
onOverlayRightClick={this.handleRightClick}
>
<h1>Click Right on the Overlay to close the modal</h1>
Copy link

Choose a reason for hiding this comment

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

Nit: rephrase to "Right click on the overlay to close the modal"

<button
type="button"
className="btn btn-primary"
onClick={this.toggleModal}
>
Close
</button>
</Modal>
</div>
);
}
}

export default {
label: "Modal close on right click",
app: ModalClose,
};
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
4 changes: 3 additions & 1 deletion 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 Expand Up @@ -100,7 +101,8 @@ class Modal extends Component {
preventScroll: false,
parentSelector: () => document.body,
overlayElement: (props, contentEl) => <div {...props}>{contentEl}</div>,
contentElement: (props, children) => <div {...props}>{children}</div>
contentElement: (props, children) => <div {...props}>{children}</div>,
onOverlayRightClick: ()=>null
Copy link

Choose a reason for hiding this comment

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

Nit: missing whitespace:

onOverlayRightClick: () => null

};

static defaultStyles = {
Expand Down
12 changes: 11 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,14 @@ export default class ModalPortal extends Component {
this.shouldClose = null;
};

handleOverlayRightClick = event => {
if(event.target == this.overlay)
{
event.preventDefault();
Copy link

Choose a reason for hiding this comment

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

We may want to allow the this.props.onOverlayRightClick function to call event.preventDefault() if they want to, for more flexibility. @diasbruno what do you think? 🤔

Copy link
Collaborator

@diasbruno diasbruno Feb 10, 2024

Choose a reason for hiding this comment

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

Better for react-modal to not interfere in the event propagation. It would be better to let the user handles it. So, better to just forward the event if the handle is present.

this.props.onOverlayRightClick(event);
}
};
diasbruno marked this conversation as resolved.
Show resolved Hide resolved

handleContentOnMouseUp = () => {
this.shouldClose = false;
};
Expand Down Expand Up @@ -375,7 +384,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