-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[ClickAwayListener] Is being fired without an onClick event after upgrading react and react-dom to v17 #23215
Comments
Oh wow, here's what's going on:
In React 16 the host in 2. is the document, in React 17 the host is an element in the body, hence the difference. |
This change internally produces the expected behavior of the codesandbox. It's unclear if we want to move forward with it. diff --git a/packages/material-ui/src/ClickAwayListener/ClickAwayListener.js b/packages/material-ui/src/ClickAwayListener/ClickAwayListener.js
index a25f43baca..e31c371924 100644
--- a/packages/material-ui/src/ClickAwayListener/ClickAwayListener.js
+++ b/packages/material-ui/src/ClickAwayListener/ClickAwayListener.js
@@ -49,6 +49,7 @@ function ClickAwayListener(props) {
// Given developers can stop the propagation of the synthetic event,
// we can only be confident with a positive value.
const insideReactTree = syntheticEventRef.current;
@@ -130,9 +131,12 @@ function ClickAwayListener(props) {
const mappedMouseEvent = mapEventPropToEvent(mouseEvent);
const doc = ownerDocument(nodeRef.current);
- doc.addEventListener(mappedMouseEvent, handleClickAway);
+ // Use a setTimeout to avoid capturing the click that has tricker the mount of the ClickAwayListener component.
+ const timeout = setTimeout(() => {
+ doc.addEventListener(mappedMouseEvent, handleClickAway);
+ });
return () => {
+ clearTimeout(timeout);
doc.removeEventListener(mappedMouseEvent, handleClickAway);
};
} @eps1lon What do you think? It's also probably time to do? :) diff --git a/packages/material-ui/package.json b/packages/material-ui/package.json
index be6c895d41..c0ab3251f4 100644
--- a/packages/material-ui/package.json
+++ b/packages/material-ui/package.json
@@ -44,7 +44,7 @@
"@emotion/core": "^10.0.27",
"@emotion/styled": "^10.0.27",
"@types/react": "^16.8.6",
- "react": "^16.8.0",
+ "react": "^16.8.0 || ^17.0.0",
"react-dom": "^16.8.0"
},
"peerDependenciesMeta": { |
This is a problem in our UI library built on top of Material as well. Curiously, changing the We'd love to see it fixed though 🙏 |
@dargue3 The only downside I'm aware of is that click away will fire false-positive when moving the scrollbar on nested containers (not the body). |
The use case is being able to close the slide-out menu while clicking anywhere besides for the menu |
That's the use case 👆 |
I'm closing the use case isn't valid, use the |
Thank you for your immediate assistance,
Thank you! |
What about the use case where you have a |
@stevewillard Do you have a reproduction? |
Sandbox for the issue @stevewillard stated. |
@wkerswell-gresham Thanks. I would propose we move forward with #23215 (comment). Do we have another alternative? |
Is it safe to stop propagation instead? |
@artisonian Do you have a working diff to propose? Stopping propagation is generally considered a bad practice. If the event should absolutely not be consumed by a parent, then yes, but we have to be certain. |
I spent some time in the source code...didn't realize the event listeners are being attached inside of an effect block. Never mind |
I am seeing this issue as well with a |
@wkerswell-gresham A workaround: https://codesandbox.io/s/material-ui-issue-forked-zb3i5?file=/src/Demo.js console.log("settingsMenuOpen", settingsMenuOpen);
const handleClick = (event) => {
const target = event.currentTarget;
+ setTimeout(() => {
setAnchorEl(anchorEl ? null : target);
+ });
};
|
Stopping propagation on |
I'm just noticing we have the same issue with a Sandbox for the issue. |
@mforman1 Thanks for the reproduction. Do you want to work on the proposed fix? We would probably only miss a test case to ensure we don't have regression in the future. It would also involve the |
I understand that everybody wants to switch right now but note that we don't officially support React 17 yet (hence the peer dependency mismatch warning you should see). We fixed most compat issue but some still slip through since we ourselves haven't fully switched yet. Material-UI + React 17 is something you should consider as unstable so do your end-users a favor and don't switch to it in production yet. React 17 hasn't any big features for end-users anyway. When it's safer to switch to React 17 we'll definitely announce that in the changelog. But it's only been out for a few days so the ecosystem needs some time to adjust. Thanks for understanding! Current investigation: |
Tracking investigation in #23215 (comment). Another workaround for now is <Popper
id="menuButton"
open={settingsMenuOpen}
anchorEl={anchorEl}
+ disablePortal
> if you don't rely on portaling. |
Will this fix be going into v4, or will it be v5 only? |
This fix will be backported to the next v4 feature release but that one will also include deprecation warnings. The recommendation in #23215 (comment) still stands: Material-UI v4 + React 17 is unstable and not fit for production yet. |
Always check this open condition when rendering middle of the popper. |
On my app I have a menu that worked fine until I upgraded
react
andreact-dom
.After upgrading
react
andreact-dom
from version16.14.0
to version17.0.0
- on menu click, the menu flashes-it opens and closes right away.Current Behavior 😯
On menu click- the menu opens and closes right away, for some reason the
ClickAwayListener
is being fired.Expected Behavior 🤔
On menu click- the menu should open, on click away it should close.
Steps to Reproduce 🕹
Steps:
react
andreact-dom
to v17.0.0
Codesandbox:
https://codesandbox.io/s/material-ui-issue-forked-byfgm?file=/src/index.jsInvalid. New repro: https://codesandbox.io/s/material-ui-issue-forked-13053?file=/src/Demo.jsYour Environment 🌎
The text was updated successfully, but these errors were encountered: