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

Conversation

ZainGulbaz
Copy link

Fixes #[837].

Changes proposed:

-Added a prop of function type 'onOverlayRightClick' that calls a function when ever we right click on modal's overlay.
-The user can then change modal's state isOpen to false, if he wants to clocse the modal on right click at overlay
- <Modal className="Modal__Bootstrap modal-dialog" isOpen={this.state.modalIsOpen} onRequestClose={this.handleModalCloseRequest} **onOverlayRightClick={this.handleModalCloseRequest}** >

Acceptance Checklist:

  • The commit message follows the guidelines in CONTRIBUTING.md.
  • Documentation (README.md) and examples have been updated as needed.
  • If this is a code change, a spec testing the functionality has been added.
  • If the commit message has [changed] or [removed], there is an upgrade path above.

@diasbruno
Copy link
Collaborator

We are going in the right direction, just made some comments. It must be the last change.
Obrigado pelo PR.

@diasbruno
Copy link
Collaborator

Awesome!! 🎉

Can you include a test for this, please?

@ZainGulbaz
Copy link
Author

ZainGulbaz commented Aug 28, 2022

@diasbruno I have added latest commit of example test case. Kindly let me know, if you have any suggestions.

@diasbruno
Copy link
Collaborator

diasbruno commented Aug 28, 2022

It looks great, but we also need to add an unit testing for this feature. Let me know if you need anything...

@ZainGulbaz
Copy link
Author

Is there any unit testing already done?

@diasbruno
Copy link
Collaborator

diasbruno commented Oct 20, 2022

All unit tests are here https://github.com/reactjs/react-modal/tree/master/specs

@doeg doeg mentioned this pull request Feb 10, 2024
4 tasks
Copy link

@doeg doeg left a comment

Choose a reason for hiding this comment

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

I realize this PR is quite old now, but it seems like it could be a useful feature! I left some review comments. :)

@@ -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.

Comment on lines +58 to +60
/* 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. */}
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).

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.

Comment on lines +12 to +17
toggleModal = (event) => {
this.setState({ isOpen: !this.state.isOpen });
};
handleRightClick = (event) => {
this.setState({ isOpen: false });
};
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.

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"

@@ -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

@honeymaro
Copy link

I'm sorry to interrupt, but I think it's awkward that the library provides click events.
To ensure consistency, you should add all basic events such as onClick, onMouseMove before adding onOverlayRightClick.

To implement the functionality of closing the modal when the user right-clicks, you can make use of overlayRef.

@diasbruno
Copy link
Collaborator

@honeymaro It depends.

You should use overlayRef only if you need to do something with the reference.
In this case, having 2 options is not a terrible idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants