DEV Community

Cover image for How to change angular material MatSnackBar color
Rahul Raveendran
Rahul Raveendran

Posted on • Edited on

1

How to change angular material MatSnackBar color

Angular Material Snackbar is helpful for displaying information to users about API success or failure, among other things.

The general syntax for using the Snackbar is:

import { MatSnackBar } from '@angular/material/snack-bar';

constructor(private _snackBar: MatSnackBar) {}

this._snackBar.open(message, action, configuration)

If we want to change the color of the text, for example, to display an API failure message in red, we can pass a custom class through the configuration object using the panelClass property:

this._snackBar.open('Something went wrong!!!', '', { panelClass: "error-api", duration: 5000 })

An important point to note is that the panel class should be defined in the global stylesheet, such as Style.css, Style.scss or Style.sass.

Here's an example of defining the .error-api class in Style.css:

.error-api {
--mdc-snackbar-supporting-text-color: #f44336;
}

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (1)

Collapse
 
rahulbenzeen profile image
RahulBenzeen

thanks @orahul1

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay