-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[Modal] Should not call stopPropagation if onEscapeKeyDown=true #20611
Comments
@mentierd It seems that you are right, the current logic doesn't match the props' descriptions. What do you think of this diff? Do you want to submit a pull request? :) diff --git a/packages/material-ui/src/Modal/Modal.js b/packages/material-ui/src/Modal/Modal.js
index badf67093..dc8eb6cee 100644
--- a/packages/material-ui/src/Modal/Modal.js
+++ b/packages/material-ui/src/Modal/Modal.js
@@ -189,7 +189,7 @@ const Modal = React.forwardRef(function Modal(inProps, ref) {
// clicking a checkbox to check it, hitting a button to submit a form,
// and hitting left arrow to move the cursor in a text input etc.
// Only special HTML elements have these default behaviors.
- if (event.key !== 'Escape' || !isTopModal()) {
+ if (event.key !== 'Escape' || !isTopModal() || disableEscapeKeyDown) {
return;
}
@@ -200,7 +200,7 @@ const Modal = React.forwardRef(function Modal(inProps, ref) {
onEscapeKeyDown(event);
}
- if (!disableEscapeKeyDown && onClose) {
+ if (onClose) {
onClose(event, 'escapeKeyDown');
}
}; For curiosity, I have looked at how the problem is handled in a couple of alternatives:
|
I'd do it if @mentierd is too busy this weekend 😀 |
@oliviertassinari I tried to implement your solution but one test fail: it('when disableEscapeKeyDown should call only onEscapeKeyDownSpy', () => { What do you think about this diff? diff --git a/packages/material-ui/src/Modal/Modal.js b/packages/material-ui/src/Modal/Modal.js
index 1c38d39c4..748a70ec1 100644
--- a/packages/material-ui/src/Modal/Modal.js
+++ b/packages/material-ui/src/Modal/Modal.js
@@ -193,11 +193,11 @@ const Modal = React.forwardRef(function Modal(inProps, ref) {
return;
}
- // Swallow the event, in case someone is listening for the escape key on the body.
- event.stopPropagation();
-
if (onEscapeKeyDown) {
onEscapeKeyDown(event);
+ } else {
+ // Swallow the event, in case someone is listening for the escape key on the body.
+ event.stopPropagation();
}
if (!disableEscapeKeyDown && onClose) { |
@weslenng The test goes against the documentation: For v5, I think that we can drop |
I'm running into an issue where I need to handle the event propagation for the escape key on my own. It's impossible to get around because it's hard-coded in the handleKeyDown callback.
To be specific, I have a Popper rendered (via portal) over the modal, and when a user hits the Escape key, I want to close the Popper, but not the modal.
Current Behavior 😯
When passing onEscapeKeyDown and disableEscapeKeyDown props to Modal, I do not want the event.stopPropagation to be called.
Instead of allowing the onEscapeKeyDown to handle the event propagation; the escape keydown event's propagation is stopped.
Expected Behavior 🤔
When modal is provided onEscapeKeyDown, a develop should expect to handle all aspects of the event. What also might be nice is if the modal manager is passed as a second parameter to the callback.
The text was updated successfully, but these errors were encountered: