Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying angular material theme to a specific component only

Is it possible to apply the angular material built-in theme to a specific component only?

I did:

@import "~@angular/material/prebuilt-themes/indigo-pink.css";

In one of my component.scss file and referenced this as the styleUrl inside of component.ts file. But the styles did not apply to my angular material paginator.

Here is what the paginator looks like

enter image description here

As shown, the styles do not apply.

Is it something to do with the fact that I'm importing them in a component specific scss file instead of importing it in angular.json?

like image 365
Shaun Chua Avatar asked Oct 25 '18 00:10

Shaun Chua


People also ask

Can I change angular material theme?

it's very easy to create a custom angular material theme. This will allow you to specify primary, accent and warning colors that will be used on Angular Material components. Your custom theme will be a Sass file and in our case, we'll call it theme. scss and place it in our app's /src folder.

How would you include a Prebuild theme in angular application without using the angular CLI?

You can find the pre-built theme files in the "prebuilt-themes" directory of Angular Material's npm package ( @angular/material/prebuilt-themes ). To include the pre-built theme in your application, add your chosen CSS file to the styles array of your project's angular. json file.

How does angular material define theme?

Defining a theme in Angular Material is extremely simple, all we need to do is to create a theme file, select three color palettes for the main theme colors — primary, accent and warn — and we are all set! Feel free to explore all the palettes that are available out of the box.

How do I create a custom material theme?

To do so, open the Settings and navigate to Appearance & Behavior → Material Theme UI → Custom Theme. Once you're done customizing your colors, you'll need to select Custom Theme or Light Custom Theme from the Theme Switcher to see your colors in action. Enjoy!


1 Answers

You have to use the Mixins @angular/material provides instead of using a prebuilt theme.

@import '~@angular/material/theming';
@include mat-core();

// Use the desired palette
$palette:  mat-palette($mat-indigo);
// Create the theme
$theme: mat-light-theme($palette, $palette);

// Include component specific mixin
@include mat-dialog-theme($theme);

// Or wrap inside another selector to scope the styles to only one specific component
my-component {
   @include mat-dialog-theme($theme);
}

Edit: This code should be in your styles.scss (the global one, not the component specific scss)

like image 88
pascalpuetz Avatar answered Sep 28 '22 12:09

pascalpuetz