How to use colors of the prebuilt angular-material themes?

Viewed 2914

In my angular app that I'm building for a demo, I've some kind of toolbar that I need to distinguish a bit from the background. I was willing to use a shade of the prebuilt-theme I'm using(purple-green). My understanding is that google's material guide describe shades of the primary color: https://material.io/resources/color/#!/?view.left=0&view.right=0 that would be ideal for my case

I'm strugling to find how to do such a simple thing. In ionic, you could use color="primary", but here, I'm using one of the prebuilt theme in my styles.scss:

@import '@angular/material/prebuilt-themes/purple-green.css';

I tried several things

@import '~@angular/material/theming';
.chat-header {
  color: mat-color($primary);
}

My understading is that since this prebuilt theme is just pure CSS, it doesn't have a SCSS variable. But how am I supposed to use their colors in one of my component then?

Is there some predefined CSS classes that I could use? I've searched but didn't found any?

1 Answers

I guess you will have to resort to grabbing the scss version of the theme-definition and extract the adjusted values to your liking. The scss sources don't seem to be bundled with the material distribution, so go to https://github.com/angular/components/blob/master/src/material/core/theming/prebuilt/purple-green.scss to see its source. Then:

my-custom-theme-definition.scss

@import '~@angular/material/theming';

// taken from https://github.com/angular/components/blob/master/src/material/core/theming/prebuilt/purple-green.scss

// Define a theme.
$my-custom-primary: mat-palette($mat-purple, 700, 500, 800);
$my-custom-accent:  mat-palette($mat-green, A200, A100, A400);

$my-custom-theme: mat-dark-theme((
  color: (
    primary: $my-custom-primary,
    accent: $my-custom-accent
  )
));

my-custom-theme.scss (import once in your global styles.scss)

@import 'my-custom-theme-definition';

// IMPORT ONLY ONCE, AS ALL STYLES ARE BEING OUTPUT

// Include non-theme styles for core.
@include mat-core();

// Include all theme styles for the components.
@include angular-material-theme($my-custom-theme);

other-component.scss

@import 'my-custom-theme-definiton';


.chat-header {
  color: mat-color(map-get($my-custom-theme, primary), 'lighter'); // or 'darker' or some value defined in https://github.com/angular/components/blob/master/src/material/core/theming/_palette.scss#L105
}
Related