I will write this post because, despite it is a wonderful component, customizing Angular Material snackbars is easy but sometimes can be a little tricky.
What is a snackbar?
As they say in the documentation, snackbar is a service for displaying snack-bar notifications.
To use it you must install angular material
We hope Angular Team don´t deprecate it like they have done with the popular flex-layout library. which maybe will suppose a big problem for many projects where devs will have to spend weeks or months refactoring to CSS. I hope this is not the way Angular will take in the future.
Well, once we shared that disappointing news for the Angular community, we will focus on this helpful component, which is very useful to show messages to the user on the screen, in a very easy way.
We will take as example the Superheros app I am currently creating (yep, not the most original thing, I know 😛 ).
With snackbars I can notify the user when one hero has been successfully created; or when it has been deleted; or when an error occurs.
As you see, we have created a new hero with no problems and the user has been properly informed.
And, what if an error happens? We could also inform if, for example we try to create a new hero with no name. That is not allowed so in this case snackbar will throw an error and it will be red
Of course we could disable the “save” button until required conditions to create one hero are fulfilled, but this way I can show you the uses of snackbar for this purpose.
Well, we will show the code. If we go again to the documentation, all snackbars are white by default:
But if we go to the element´s API, we will find a property called “panelClass” which allows us to use one string or array of strings, corresponding to the names of the CSS classes that will help us to customize our snackbar.
So, ok, we will start writing our snackbar.
First of all, you will have to import the Module, like explained in the documentation:
That Module will contain everything you need to use your snackbars.
Where do you have to import it? Well, it is a Module, so we could import it on the main app.module.ts of the application, but, as when we use material components we end up having a lot of modules imported, I recommend creating another module only to declare the material components we will use.
For example: create a folder and inside a file with the name material-module.ts
Notice that here we have only used the exports array, while many times we also import the modules with the imports array, for example, in the app-module.ts
We can see why here:
- imports makes the exported declarations of other modules available in the current module
- exports makes the components, directives, and pipes available in modules that add this module to imports. exports can also be used to re-export modules such as CommonModule and FormsModule, which is often done in shared modules.
So, what we have in our material-module, is a module that will export the Angular Material modules, so they can be used on another module with the imports array. We need nothing more here.
Then, those exported Material Modules will be used, in my case, in another module called heroes-module.ts, just simply by importing the MaterialModule. That way, we will have with one sentence all the exported modules available for our heroes-module.ts. dependent components. Those dependent components are inside the “declarations” array.
IMPORTANT: Depending on the Code editor you use, maybe you will have to import manually the components or modules you specify in the @NgModule, inside declarations, imports… arrays. Visual Studio Code makes for you.
Well, now we are able to use our snackbar in our components. In my case for this example is the add-component.ts which I use to add new heros
Why can we use Snackbar inside this component now? Because remember: inside the heroes-module.ts, we declared the AddComponent, as well as we have imported also in heroes-module.ts our MaterialModule.
So, AppComponent “depends” on heroes-module.ts, which can “share” with its dependent components all the modules imported on heroes-module-ts, like for example our MaterialModule
If that sounded a bit confusing, maybe this scheme helps you:
Check the gists and I will stress the aforementioned stack overflow link where they talk about import, exports, declarations…
So, now, in the component where we need to show our snackbar, we will have to inject it in the constructor. Why? Because, as we have read at the beginning, from the doc:
MatSnackBar is a service for displaying snack-bar notifications.
And what means that it is a service? That we have to inject it (more about dependency injection here). So, in our add-component.ts:
Now in this same component, I will create a method to show our snack bar when needed, with the customizations we wish.
EDIT: Sorry for the mistyping, in the code I wrote “succesful” instead of “successful”
As we can see, our showSnackBar() method has 2 parameters:
- messageToShow. As it says, the message will be shown
- snackBarClass. In my case there will be just one string. Please notice, we have passed a default value to this parameter. It is a string with the name of the CSS class that will be used most of the times. Declaring a default value you can in this case simply call the method passing the first parameter and will show “successful” styles by default. We will see later.
Our inject service is called “snackbar”, and we will call its open() method.
Here you can find info about the method and the parameters it accepts, but basically is responsible for showing –opening- our snackbar.
- Our first parameter is the messageToShow, that will be taken from the first parameter of showSnackBar(). As you can see this method acts like a wrapper to make it easier later to call and open the snackbar.
- Second parameter “Close!” is just the text that will be shown, you can push on it, and the snackbar disappears before the set duration ends
- As third parameter we have an object where will use two properties:
- Duration. Simply the ms (milliseconds) you want the snackbar to stay on screen. 1000ms=1 second.
- panelClass. This is where we will pass our css customizations. In this case we will pass the snackBarClass which, if we remember, has the value “successful”. So in anyway, now it would be like writing:
So now, we only have to call our showSnackBar() where we need, with the appropriate message to be shown, For example, when we correctly add a new Hero.
And we will have:
Have you noticed how, as we call the method, we only passed the value to the message parameter? That saves us time and work, since we passed a default value for the second parameter and then we don´t need to specify a value if it is not different from the default value (we will see that later).
But ey! Where are the css classes we need? Where is our “successful” class?
Maybe you would place it on the add-component.css , but if you do that, it won’t work, as explained here:
Accepted answer tells us WE MUST DO IT IN OUR GLOBAL STYLES.CSS FILE
So we will go there and we will write 2 classes: our old friend “successful” and another to show different styles if there is an error
Now, if we want to show a different style when an error occurs, we will have to call the method this way:
Can you see this time we are passing also the value to the second parameter? This way, we overwrite the default value passed to the panelClass from “successful” to “fail”).
As we have already seen, this will allow us to show a proper error message with a red background:
And that’s it! Is not very difficult but having to declare the snackbar classes on the global styles.css can give you some headaches and I want to save them for you 🙂
Also we have reviewed some cool Angular concepts which maybe are useful to you.
Of course as I always say, please feel free to contact me if you find some kind of failure, or if you want to hire me as technical writer for your publication.
Cover image belongs to Lawrence Monk found in Pixabay