-
Notifications
You must be signed in to change notification settings - Fork 810
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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={ | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 I would suggest a straightforward comment like:
|
||
|
||
closeTimeoutMS={ | ||
0 | ||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: missing whitespace: onOverlayRightClick: () => null |
||
}; | ||
|
||
static defaultStyles = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -299,6 +300,14 @@ export default class ModalPortal extends Component { | |
this.shouldClose = null; | ||
}; | ||
|
||
handleOverlayRightClick = event => { | ||
if(event.target == this.overlay) | ||
{ | ||
event.preventDefault(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may want to allow the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}; | ||
|
@@ -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 = { | ||
|
There was a problem hiding this comment.
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 thecontextmenu
event (see MDN docs):Some alternate ways to trigger the
contextmenu
event beyond right click:ctrl
+ click on MacOSshift+f10
on Windowsfn+12
on MacOS (with "Alternate Pointer Events" enabled)...And some screen readers have their own triggers, too.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one I didn't know. :)
There was a problem hiding this comment.
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.