In MUI v3 losing focus of the window would stall the timer for a snackbar and then resume when focus was returned to the window.
In MUI v4, regardless of whether or not the snackbar is open, onClose is automatically called whenever a window with a snackbar loses and then regains focus unless disableWindowBlurListener is set, which sacrifices the pause/resume of the timeout functionality.
Link: https://codesandbox.io/embed/createreactapp-l9k45
Currently this is causing my app to erroneously fire notification dismiss actions.
| Tech | Version |
|--------------|---------|
| Material-UI | v4.0.1 |
| React | v16.8.6 |
| React-DOM | v16.8.6 |
@drewgaze I fail to see a problem. The only close event I see is with a clickaway reason, which is legit.
Could you provide more information?
It's a change in behavior from the v3 version of this component. I can handle it in my own code so that onClose is only called if the Snackbar is open but I don't understand if this change is intentional.
Here's a video that hopefully demonstrates the issue better.
@drewgaze Ok thanks, I can reproduce the problem. It's an issue with the hook logic. I don't understand why it works with v3, but anyway, what do you think of this fix? Do you want to submit a pull request? :)
diff --git a/packages/material-ui/src/Snackbar/Snackbar.js b/packages/material-ui/src/Snackbar/Snackbar.js
index 327622d1b..b92cc5b9c 100644
--- a/packages/material-ui/src/Snackbar/Snackbar.js
+++ b/packages/material-ui/src/Snackbar/Snackbar.js
@@ -149,7 +149,9 @@ const Snackbar = React.forwardRef(function Snackbar(props, ref) {
);
React.useEffect(() => {
- if (open) setAutoHideTimer();
+ if (open) {
+ setAutoHideTimer();
+ }
return () => {
clearTimeout(timerAutoHide.current);
@@ -203,7 +205,7 @@ const Snackbar = React.forwardRef(function Snackbar(props, ref) {
};
React.useEffect(() => {
- if (!disableWindowBlurListener) {
+ if (!disableWindowBlurListener && open) {
window.addEventListener('focus', handleResume);
window.addEventListener('blur', handlePause);
@@ -214,7 +216,7 @@ const Snackbar = React.forwardRef(function Snackbar(props, ref) {
}
return undefined;
- }, [disableWindowBlurListener, handleResume]);
+ }, [disableWindowBlurListener, handleResume, open]);
// So we only render active snackbars.
if (!open && exited) {
cc @joshwooding
This problem also happens to reset the cookie we set in the documentation, displaying the notifications many times.
Marking as important since it affects the docs :)
I'm taking care of it, the notification popup on the documentation is driving me crazy.