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

Dismiss button effect to closing all snackbars #219

Closed
cbakkurt opened this issue Jan 24, 2020 · 2 comments
Closed

Dismiss button effect to closing all snackbars #219

cbakkurt opened this issue Jan 24, 2020 · 2 comments

Comments

@cbakkurt
Copy link

I want to add that when I click on screen anywhere with mause, all snackbars will close.
I can do this with your onClose callback function when I click anywhere in screen, it will works fine. However, when I close single snackbar with dismiss button, it also close all snackbars.
How can I detect this situtations ?

My code is below,
When I use onClose callback method

public render() {
const notistackRef = React.createRef();
const onClickDismiss = key => () => {
(notistackRef.current as any).closeSnackbar(key);
};

return (
  <MuiThemeProvider theme={this.state.muiTheme}>
    <SnackbarProvider
      autoHideDuration={null}
      preventDuplicate
      ref={notistackRef}
      action={key => (
        <div>
          <IconButton onClick={onClickDismiss(key)} aria-label="delete">
            <DeleteIcon />
          </IconButton>
        </div>
      )}
      maxSnack={5}
      onClose={(event, reason) => {
        if (reason === "clickaway") {
          (notistackRef.current as any).closeSnackbar();
        }
      }}
    >
      {this.props.children}
    </SnackbarProvider>
  </MuiThemeProvider>
);

}
}

@nildakuzu
Copy link

The "key" paramater will be passed to onClose callback function as a part of this: #214
Then adding key === undefined to if statement which is in onClose that will close all snackbars when you click anywhere except snackbar dismiss button.

onClose={(event, reason, key) =>
{
if (key === undefined && reason === "clickaway") {
(notistackRef.current as any).closeSnackbar();
}
}}

As a side effect onClose calllback function is fired one more time.

@iamhosseindhv
Copy link
Owner

iamhosseindhv commented Jan 27, 2020

Thanks @nildakuzu for your contribution. That is not true.

@cbakkurt

However, when I close single snackbar with dismiss button, it also close all snackbars.
How can I detect this situations?

This is expected. Say we have 3 snackbars on the screen; you dismiss one of them using a "Dismiss" button; the moment you click on the button, clickaway is triggered for the other two snackbars.

The only way that I can think of is to pass the onClose event to individual snackbars and not globally to SnackbarProvider.

props.enqueueSnackbar('my message', {
    onClose: (event, reason) => {
        if (reason === 'clickaway') {
            props.closeSnackbar();
        }
        if (reason === undefined) {
            // you/user closed the snackbar
        }
    },
});

NOTE: In future reason = instructed when a snackbar is closed using closeSnackbar function.

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

No branches or pull requests

3 participants