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

Ability to close snackbars on click away #182

Closed
Brettm12345 opened this issue Oct 10, 2019 · 6 comments
Closed

Ability to close snackbars on click away #182

Brettm12345 opened this issue Oct 10, 2019 · 6 comments

Comments

@Brettm12345
Copy link

I would love to have a property I can pass to SnackbarProvider and enqueSnackbar that would add a click away listener to the snackbar so it would auto dismiss upon the next user interaction

@iamhosseindhv
Copy link
Owner

iamhosseindhv commented Oct 10, 2019

notistack doesn't close snackbars on clickaway. but that doesn't mean you can't close snackbars on clickaway. You can do the following once v0.9.4 is released.

 <SnackbarProvider
    ref={(ele) => { this.ref = ele; }}
    onClose={(event, reason, key) => {
        if (reason === 'clickaway') {
            this.ref.closeSnackbar(key);
        }
    // ...
}}

@dalborgo
Copy link

dalborgo commented Mar 3, 2020

 <SnackbarProvider
    ref={(ele) => { this.ref = ele; }}
    onClose={(event, reason, key) => {
        if (reason === 'clickaway') {
            this.ref.closeSnackbar(key);
        }
    // ...
}}

Inside the onClose method is it possible get the access to the snackbar properties? For example to avoid the clickaway handler if the snackbar is persistent.

@iamhosseindhv
Copy link
Owner

iamhosseindhv commented Mar 3, 2020

No but you can store the key of your persistent snackbar somewhere and decide if you want to close it or not by comparing the keys. An easier approach would be to pass your own key in enqueueSnackbar options.

key: ‘persistent-someuniqueid’

And in onClose check if key startsWith persistent.

This way you don’t have to bother storing keys of persistent snackbars.

@dalborgo
Copy link

dalborgo commented Mar 3, 2020

Thank you! I've chosen the second approach.

@Motoxpro
Copy link

Motoxpro commented May 3, 2020

Thanks for the help!

Since we have access to the currently open snacks, we can just see if the one we are trying to close has persist set to true.

Here is an example in typescript.

      onClose={(event, reason, key): void => {
        if (reason === 'clickaway') {
          if (!ref.current) {
            return;
          }
          const currentSnack = ref.current.state.snacks.filter(snack => snack.key === key);
          if (currentSnack?.[0] && !currentSnack[0].persist) {
            ref.current.closeSnackbar(key);
          }
        }
      }}
      ref={ref}

@mcasteller
Copy link

Hi, here is a working example for those using react functional components:

import {SnackbarProvider} from 'notistack'

const notistackRef = React.createRef()

const AlertProvider = ({children}) => (
  <SnackbarProvider
    ref={notistackRef}

    onClose={(event, reason, key) => {
      if (reason === 'clickaway') {
        notistackRef.current.closeSnackbar(key)
      }
    }}
  >
    {children}
  </SnackbarProvider>
)

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

5 participants