Modify the color of the matSnackbar

I'm having trouble changing the snackbar color in Angular using Angular Material. I tried using panelClass in the ts file and adding it to the global css, but the color remains unchanged. Any suggestions on how to resolve this? I am still new to this framework.

Here is my code snippet from app.component.ts:

import { Component } from '@angular/core';
import { MatSnackBar, MatSnackBarConfig } from '@angular/material/snack-bar';
import { MatSnackBarHorizontalPosition } from '@angular/material/snack-bar';
import { MatSnackBarVerticalPosition } from '@angular/material/snack-bar';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  horizontalPosition: MatSnackBarHorizontalPosition = 'center';
  verticalPosition: MatSnackBarVerticalPosition = 'top';
  constructor(private snackBar: MatSnackBar) { }
  openSnackBar(message: string, action: string) {
    this.snackBar.open(message, action, {
      horizontalPosition: this.horizontalPosition,
      verticalPosition: this.verticalPosition,
      panelClass: ['.snackbar-style'],
    });
  }
}

And here is the relevant part of my app.component.css:

.snackbar-style {
  background-color: black;
  color: hotpink;  
}
            

Answer №1

To update the global styles.scss file, include:

:root {
    --primary-color: #153d77;
    --secondary-color: #6c757d;
    --white-color: #fff;
    --success-color: #198754;
    --delete-color: #dc3545;
    --background-color: #f2f2f2;
  }
  
  .success-snackbar {
    color: white;
    --mdc-snackbar-container-color: var(--success-color);
    --mat-mdc-snack-bar-button-color: var(--white-color);
  }

.error-snackbar {
    max-width: max-content !important;
    color: white;
    --mdc-snackbar-container-color: var(--delete-color);
    border-color: #f5c6cb;
  
    --mat-mdc-snack-bar-button-color: var(--white-color);
  }
  
  .light-success-snackbar {
    color: #155724;
    --mdc-snackbar-container-color: #d4edda;
    --mat-mdc-snack-bar-button-color: darkgreen;
  }
  
  .light-error-snackbar {
    color: #721c24;
    --mdc-snackbar-container-color: #f8d7da;
    border-color: #f5c6cb;
    --mat-mdc-snack-bar-button-color: darkred;
  }

Then, to implement the .success-snackbar style, add it to the panelClass when calling the snackbar:

openSnackBar(message: string, action: string) {
    this.snackBar.open(message, action, {
      horizontalPosition: this.horizontalPosition,
      verticalPosition: this.verticalPosition,
      panelClass: ['success-snackbar'],
    });
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Attempting to output numerical values using Jquery, however instead of integer values, I am met with [Object object]

I am struggling to figure out how to display the value contained in my object after attempting to create a Calendar using Jquery. I attempted to use JSON.toString() on my table data, but it didn't solve the issue. Perhaps I am not placing the toString ...

How can one element be made to rely on the presence of another element?

I'm currently working on my portfolio website and encountering an issue. Whenever I hover over an image of a project, the image expands in size, becomes less transparent, and a description of the project pops up. However, I would like to include a lin ...

Issue with random pages where global header.php is not showing a specific image

I'm currently revamping a website that was originally developed using codeigniter. The backend is quite chaotic with no clear identifiers for anything. I recently updated the banner in the header.php file, adjusting the style to suit the requirements. ...

Theme customization in Material UI includes the addition of a custom color. However, this custom color is missing from the control values in Story

Currently in my project, I am utilizing a stack that includes React 18, TypeScript, MUI 5, and Storybook 6.5. I have been attempting to incorporate custom colors into my MUI Theme and have them reflect in Storybook's dropdown options for the color p ...

Searching for an active Angular component in Selenium WebDriver - the complete guide!

I am facing an issue with XPath in Selenium webdriver. I can successfully locate an element using XPath in DevTool, but when I try the same XPath in selenium-webdriver, it throws an error saying "no such element: Unable to locate element". I have checked ...

typescript locate within the union type in the mapping expression

Consider the following: type X = { label: 'Xlabel', X_id: 12 }; type Y = { label: 'Ylabel', Y_id: 24 }; type Z = { label: 'Zlabel', Z_id: 36 }; type CharSet = X | Y | Z; I am looking for type CharSetByLabel = Map<CharSet& ...

The Link Breaks the Overlay Hover Effect

Currently, the code functions as intended when you hover over or touch the thumbnail, an overlay will appear. The issue lies in the fact that to navigate to a specific URL, you have to click directly on the text. The overlay itself is not clickable and ca ...

How to eliminate parameters from the URL in Ionic 2 with Angular 2

Basically, in my application, when a user receives an email notification with a hyperlink to open a specific record based on an ID number, the URL looks like this: https://myappserver.com/?aid=12343551 So, after the user opens the record, I'm wonder ...

What could be causing my jQuery script to not load properly on the first attempt?

I have a jQuery script set up on this particular webpage just before the ending body tag. Here is the script: <script> $(document).ready(function(){ var demomain = $(".demo-bg section"); var demomainW = demomain.height()+ 150 ...

MUI Chips serving as selectible tags with checkbox-like functionality

I have retrieved data from a JSON file containing information about different types of chips: [ { "id": "4", "name": "Caucasian" }, { "id": "5", "name": "Asian" }, ...

Customizing the font style on a CSS changing table

Below is the CSS code snippet I am using: table.Table12 { border:1px solid blue; } table.Table12 td { border:1px solid blue; width:200px;} table.Table12 a:link { font-weight: bold;} Additionally, here is the HTML code: <table class="Table12" align="r ...

Is there a way to convert a literal type from the type level to the term level in TypeScript?

Consider this scenario: I have created a type that can only hold one specific value: export type IfEqual<T, U> = (<G>() => G extends T ? 1 : 2) extends ...

Eliminating Redundant Code in Angular by Utilizing RxJS Subscriptions in a Service with Proper Typing and Unsubscribing

I've noticed some repetitive code in a few of my components... After simplifying it, the code basically looks like this: // code within my components phoneNumberChanges$: any; ngOnInit(): void { // keep an eye on the phone number this.subscri ...

The rendering process in ag-grid is resulting in the service component initialized from an event to become null

Currently, I am utilizing ag-grid and need help understanding a specific issue. In my method for preparing GridOptions, I have set up an onCellValueChanged event that triggers a service component to access the database and populate the data. However, when ...

What is the correct way to handle Vue props that include a dash in their name?

I am currently working on a project using Vue. Following the guidelines of eslint, I am restricted from naming props in camel case. If I try to do so, it triggers a warning saying Attribute ':clientId' must be hyphenated. eslint vue/attribute-hyp ...

Embellishing Your Website with Unique Cursor Styles

Looking to add personalized cursors to my website using asp.net 4.0. On my master page, I aim to set a custom cursor as the default for the entire site and have different custom cursors for all links. Is this achievable? ...

Loading Animation for Pages and Tables

I'm experiencing a choppy and sudden transition when switching between two pages that contain tables populated using ng-repeat. Upon loading, the footer is positioned halfway up the page below the table heading until the table loads and the layout adj ...

VScode has identified potential problems with modules that utilize Angular Ivy, however, this should not cause any major issues

Encountering a problem with using Angular in VSCode, where there seems to be no ngModules due to AngularIvy. The error message indicates an issue with 'CommonModule' not being recognized as an NgModule class: [{ "resource": "[...]src/app/com ...

Guide on inserting a dropdown menu following a specific count of <li> elements using Javascript or Jquery

I am currently working on an HTML project <div class="ktmsg"> <ul> <li class="a1"> <a title="Link Tools" href="#"> … </a> </li> <li class="a2"> <a title="Link Tools" href="#"> ...

Having trouble getting Angular 2 QuickStart to run with npm start?

When attempting to start my project with npm start, I encountered the following error: C:\angular2-quickstart>npm start > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="47262920322b2635756a36322e242c343326353307766 ...