Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 and material - How to change the background color from SnackBar component

I have to change the background from the snackbar component. I'm using it to alert or inform the user about some either error or completed action the user did.

The material version from the project. "@angular/material": "^5.0.0-rc.1",

The documentation https://material.angular.io/components/snack-bar/api say about an api to change the class.

panelClass: string | string[] Extra CSS classes to be added to the snack bar container.

This is what I add in the css file.

.mycsssnackbartest {   background-color: blue; } 

This is the code to open the snackbar

this.snackBar.open('Informing the user about sth', 'User Message' , {     panelClass: ['mycsssnackbartest '] } ); 

What am I doing wrong?

like image 935
andre luis borges de paula Avatar asked Nov 29 '17 19:11

andre luis borges de paula


People also ask

What is snackbar in angular?

Advertisements. The <MatSnackBar>, an Angular Directive, is used to show a notification bar to show on mobile devices as an alternative of dialogs/popups.


2 Answers

You have to use the panelClass option (since v6) to apply classes on a snackbar like this:

this.snackBar.open(message, action, {   duration: 2000,   panelClass: ['blue-snackbar'] }); 

CSS (in global styles.scss):

.blue-snackbar {   background: #2196F3; } 

See the Stackblitz example

like image 59
Philipp Kief Avatar answered Sep 20 '22 18:09

Philipp Kief


Using themes:

Primary:

this.snackBar.open('Some message','Some action', {     duration: 2000,     panelClass: ['mat-toolbar', 'mat-primary'] }); 

Switch: 'mat-primary' to 'mat-accent' or 'mat-warn'

like image 42
FranSanchis Avatar answered Sep 22 '22 18:09

FranSanchis