Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2/4 How to style angular material design snackbar

I am new to Angular2/4 and angular typescript. I want to style the angular material design snackbar for example change the background color from black and font color to something else.
How do i go about styling the "snackbar" ?

I have the material design snackbar in the service/core and to make it available i call it in every component as needed.

How can I style the Angular 2 material design "snackbar"in Angular 2/4? I have included the code snippet below:

service/core

import { Injectable, Inject } from '@angular/core';
import { Observable } from 'rxjs/Observable'
import { DOCUMENT } from'@angular/platform-browser'; 
import { MdDialog, MdDialogRef } from '@angular/material'; 
import { MdDialogConfig, ComponentType } from '@angular/material'; 
import {MdSnackBar} from '@angular/material';

constructor(
    public dialog: MdDialog,
    public snackBar: MdSnackBar,
    @Inject(DOCUMENT) public doc: any   ) {
      dialog.afterOpen.subscribe((ref: MdDialogRef<any>) => {
        if (!doc.body.classList.contains('no-scroll')) {
        doc.body.classList.add('no-scroll');
        }
      });
      dialog.afterAllClosed.subscribe(() => {
        doc.body.classList.remove('no-scroll');
      });        }

   openSnackBar(message: string, action?: string) {
    this.snackBar.open(message, action, {
      duration: 4000,
    });   }

wiz.components.ts ....

 saveData(): void {
    this.advisorClientModel.currentStep = this.currentStep;
    this.advisorClientModel.clientId = this.faService.getClientId();
    this.advisorClientModel.isMaxAmount = this.isMaximumAmount;
    this.advisorClientModel.desiredLoanAmount = this.loanAmount;
    this.advisorClientModel.maxLoanAmount = this.eligibleSelected.advanceAmount;
    this.advisorClientModel.pledgedAccounts = this.getPledgedAccountsArray();
    this.advisorClientModel.pledgedMarketValue = this.getPledgedMarkeValue();

    this.faService.updateAdvisorClient( this.advisorClientModel )
      .subscribe(
        successModel => {
          this.coreService.openSnackBar("Your Data Has been Saved");
          this.navigateTo("fa/wiz" + (this.currentStep + 1));
        },
        error => {
          this.onError(error);
        }
      );
  }
like image 415
user244394 Avatar asked Aug 01 '17 13:08

user244394


People also ask

What is snackbar in angular material?

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

How do I use snackbar in service?

Import the snackBarService and inject it inside the constructor of the component, in which you want to use the Snackbar. This will create an instance of the service say snackBService. Now call the openSnackBar function wherever it is required, with the help of snackBService.

What is a snackbar component?

Snackbars provide brief feedback about an operation through a message at the bottom of the screen. Snackbars contain a single line of text directly related to the operation performed. They may contain a text action, but no icons. Toasts (Android only) are primarily used for system messaging.

Why to use Angular Material snackbar service?

Using Angular material snackbar service to keep single code for the whole application, allow us to code reusabilities among different component. How to implement Angular Material Snackbar? How to apply Angular Material Snackbar color? How to apply to add a Material icon to the Angular Snackbar component?

What is @angular material?

Angular material is facilitating the process of development by using components of reusability. The style is nothing but the specification of the unified system which is visual, interaction design, and motion. We need to install the Angular CLI in our system. We can install the angular CLI by using npm command.

How to apply CSS classes to our Angular Material toast?

We can apply CSS classes to our Angular material toast, by using the CSS custom class name to panelClass attribute and we can add style and in our let change the background color. Let’s add the CSS class name to our snackbar using panelClass, and edit the app.component.ts file.

What is the angular module for snack bar notification popup?

The angular module for this component is as above. This section covers basic components with message, action, and duration This is an example of showing a simple notification offline message on clicking the button provided click event for showing simple snack bar notification popup


3 Answers

md-snackbar provides a service to provide custom config. One the properties of config is extraClasses that allows to add CSS classes to the snack bar container (doc).

extraClasses can be used with ::ng-deep to override the default CSS classes. Here's an example:

component.ts:

Requires following import in the component:

import {MdSnackBar, MdSnackBarConfig} from '@angular/material';

(providing custom configuration)

openSnackBar(message: string, action?: string) {
  let config = new MdSnackBarConfig();
  config.extraClasses = ['custom-class'];
  this.snackBar.open(message, action ? 'Action Label' : undefined, config);
}

component.css:

::ng-deep snack-bar-container.custom-class {
  background: yellow;
}

::ng-deep .custom-class .mat-simple-snackbar {
  color: green;
}

Here's a Plunker demo if you would like to try.

NOV 2018 UPDATE: Angular 6+

The syntax has changed a bit, with the md- prefix being replaced mat- and extraClasses was replaced with panelClass. The function is overall the same though:

const config = new MatSnackBarConfig();
config.panelClass = ['custom-class'];
...

and the imports too:

import { MatSnackBar, MatSnackBarConfig } from '@angular/material';
like image 74
Nehal Avatar answered Oct 07 '22 10:10

Nehal


I made the following code to work with Angular 6 and Angular Material 6.

The service that contain the snackBar calls:

@Injectable()
export class MessageService {
   constructor(private snackBar: MatSnackBar) {}

   showError(message: string) {
      const config = new MatSnackBarConfig();
      config.panelClass = ['background-red'];
      config.duration = 5000;
      this.snackBar.open(message, null, config);
   }
}

Add the css class in the styles.css file:

.background-red{
   background-color: rgb(153, 50, 50);
}
like image 28
Ângelo Polotto Avatar answered Oct 07 '22 10:10

Ângelo Polotto


From mat SnackBarConfig Class you can add

panelClass: string | string[]

"Extra CSS classes to be added to the snack bar container".

this.snackBar.open("Your custom Message", '', {
      panelClass:"custom_sneak_bar"
}
like image 8
Kshitij Avatar answered Oct 07 '22 08:10

Kshitij