Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 Material Snackbar panelClass config

I am trying to add a panelClass config to the Angular Material Snackbar.

I wrote the following code, by following documentations from the official websites.

import { Component, OnInit } from '@angular/core';
import { MatSnackBar, MatSnackBarConfig } from "@angular/material";
import { Location } from '@angular/common';

@Component({
  selector: 'snack-bar-component-example',
  templateUrl: './snack-bar-component-example.html',
  styleUrls: ['./snack-bar-component-example.css']
})
export class SnackBarComponentExample implements OnInit {

  constructor(public snackBar: MatSnackBar) { }

  ngOnInit() {
  }

  saveButtonClick = () =>{
    this.snackBar.open("This is a message!", "ACTION", {
      duration: 3000,
      panelClass: ["font-family:'Open Sans', sans-serif;"]
    });
  }
}

I have already binded the event to the HTML Button.When I am removing the panelClass config, then the duration config is working fine. I am importing a Google Font (Open Sans) and trying to apply the font to the Snackbar. However, I am receiving an error:

ERROR DOMException: Failed to execute 'add' on 'DOMTokenList': The token provided ('font-family:'Open Sans', sans-serif;') contains HTML space characters, which are not valid in tokens.

Maybe, I am not able to understand how to use panelClass. Even, when I am trying to add this,

panelClass: ["color:white;"];

It is still showing the error:

ERROR DOMException: Failed to execute 'add' on 'DOMTokenList': The token provided ('color: white;') contains HTML space characters, which are not valid in tokens.

How can I fix this error and get things working? Please help.

PS: I am aware with the extraClasses config. But, I don't want to use it as it is written in the documentation that it will soon be deprecated.

PPS:: It is working fine with the duration config.

like image 988
abhijeetps Avatar asked Dec 20 '17 07:12

abhijeetps


People also ask

Why snackbar is not working in angular?

The issue is that the snackbar is being ran outside of angular's zone. A quick fix can be placed in your SnackbarService or where the error method is being called. Waaaaaaaaaah that's it!

How to implement snackbar in angular?

Opening a snackbarlet snackBarRef = snackBar. open('Message archived'); // Simple message with an action. let snackBarRef = snackBar. open('Message archived', 'Undo'); // Load the given component into the snackbar.

What is Panelclass?

The class Panel is the simplest container class. It provides space in which an application can attach any other component, including other panels. It uses FlowLayout as default layout manager.

How to close snackbar in angular?

Using the snackbar close() method, you can close the component in the code.


2 Answers

In your component SnackBarComponentExample try:

saveButtonClick = () =>{
  let config = new MatSnackBarConfig();
  config.duration = 5000;
  config.panelClass = ['red-snackbar']
  this.snackBar.open("This is a message!", "ACTION", config);
}

Where 'red-snackbar' is a class in your apps main styles.css file. Strangely I was unable to get the config.panelClass to work when I was referencing my components associated css file, but once I put the class into the main styles.css file my styles were applied correctly to the snackBar.

like image 170
Narm Avatar answered Oct 17 '22 23:10

Narm


In angular 7 using ::ng-deep in front of class worked for me.

::ng-deep  .snackBar-fail {
    color: #ffffff !important;
    background-color: #cc3333 !important;
}
like image 25
Lucky Avatar answered Oct 18 '22 00:10

Lucky