Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material - Not closing Material snackbar

I am using Angular and Angular Material version 5.

Is there a way to keep the snackbar open?

For example, if an error happens, open a snackbar and set a duration or a timeout on that snackbar to be closed after 5 seconds. If another error happens, within those 5 seconds, how can I instead of closing this one, and opening another snackbar, to just increase duration of this one for few seconds?

Here is stackblitz url: https://stackblitz.com/edit/angular-j4ww8y

like image 872
BrS Avatar asked Apr 03 '18 15:04

BrS


2 Answers

This doesn't answer the original question but, if you want a snackbar with indefinite duration: snackbar.open("Message", "Action", {duration: undefined});

like image 166
Manuel Avatar answered Oct 29 '22 16:10

Manuel


You could try it the other way round:

Set a very high duration on the timeout, then, if no error occurs during the next 5 seconds, close it with the dismiss method

https://material.angular.io/components/snack-bar/api

You can add a custom component inside the snackbar, instead of just text. That custom component can be in charge of updating the message

https://material.angular.io/components/snack-bar/examples

Btw, in your stackblitz, the snackbar never closes because you declared duration as

duration: 5000;

instead of

duration = 5000;
like image 2
David Avatar answered Oct 29 '22 17:10

David