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

[Snackbar] Add imperative methods to display them directly #18098

Open
jiangyh1024 opened this issue Oct 30, 2019 · 7 comments · May be fixed by #31991
Open

[Snackbar] Add imperative methods to display them directly #18098

jiangyh1024 opened this issue Oct 30, 2019 · 7 comments · May be fixed by #31991
Assignees
Labels
component: snackbar This is the name of the generic UI component, not the React module! new feature New feature or request priority: important This change can make a difference

Comments

@jiangyh1024
Copy link

jiangyh1024 commented Oct 30, 2019

Summary 💡

Support imperative calls for the sake of simplicity.

Examples 🌈

import { useSnackbar } from '@material-ui/core';

function MyComponent() {
  const snackbar = useSnackbar();

  return (
    <Button
      onClick={() => {
        snackbar({
          message: "My primary message for the user.",
          duration: 3000,
          variant: 'success',
        });
      }}
    >
      Trigger
    </Button>
  );
}
import { ConfigProvider } from '@material-ui/core';

<ConfigProvider snackbar={{ maxItems: 3 }} />

Motivation 🔦

Without such support, a developer has to set up a global event emitter system. It's cumbersome and tedious.

Benchmark

@jiangyh1024 jiangyh1024 changed the title Please add methods to Snackbar so we can call it directly Please add methods to Snackbar so we can call them directly Oct 30, 2019
@jiangyh1024

This comment has been minimized.

@jiangyh1024

This comment has been minimized.

@oliviertassinari oliviertassinari added component: snackbar This is the name of the generic UI component, not the React module! waiting for 👍 Waiting for upvotes labels Oct 30, 2019
@oliviertassinari
Copy link
Member

oliviertassinari commented Oct 30, 2019

@jiangyh1024 I'm 💯 onboard with you, I think that an imperative API makes a lot of sense. We recommend the usage of notistack for this problem, for now.

Follow the discussion with @leMaik in #17762 for more details. I think that we should snooze this problem for a good 6 months to see how people react.

@oliviertassinari oliviertassinari added the new feature New feature or request label Oct 30, 2019
@oliviertassinari oliviertassinari added priority: important This change can make a difference and removed waiting for 👍 Waiting for upvotes labels Nov 30, 2019
@oliviertassinari oliviertassinari changed the title Please add methods to Snackbar so we can call them directly [Snackbar] Add imperative methods to display them directly Nov 30, 2019
@oliviertassinari oliviertassinari removed the priority: important This change can make a difference label Dec 1, 2019
@oliviertassinari oliviertassinari added the priority: important This change can make a difference label Dec 19, 2019
@NearHuscarl
Copy link

NearHuscarl commented Sep 4, 2020

I manage to solve a similar problem with Dialog. It's a bit easier in my case since I don't have to keep track of the key, you can just pop the top Dialog if you want to close. See my answer here for more detail. Btw, in Flutter you can already do that using showDialog which is a built-in method. Hope this will come to Material-UI soon.

@kasperpeulen
Copy link

Without such support, a developer has to set up a global event emitter system. It's cumbersome and tedious.

Is that true? You could write it like this, no?

export default function MyComponent() {
  const [snackbar, setSnackbar] = useState<{ message: string; duration: number }>();
  const onClose = () => setSnackbar(undefined);
  return (
    <>
      <Button
        onClick={() => {
          setSnackbar({
            message: "My primary message for the user.",
            duration: 3000,
          });
        }}
      >
        Trigger
      </Button>
      <Portal>
        <Snackbar
          open={snackbar != null}
          autoHideDuration={snackbar?.duration}
          onClose={onClose}
          message={snackbar?.message}
        ></Snackbar>
      </Portal>
    </>
  );
}

@oliviertassinari
Copy link
Member

@kasperpeulen I think that your example precisely surfaces the issue. This is not enough. What happens if two Snackbar component tries to display a message? What's do we gain by writing this boilerplate, can't it be abstracted?

@ggcaponetto
Copy link

Maybe you want to check out the imperative Snackbar library that I built.
https://github.com/ggcaponetto/mui-gotit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: snackbar This is the name of the generic UI component, not the React module! new feature New feature or request priority: important This change can make a difference
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants