3

I'm trying to use a specific (BACKGROUND) color from my theme to make other elements that color

What is the designed way to do that? Is there a designed way to do this?

For example, Is there a way to say this custom element should use the current theme's -> background -> disabled color? etc

I would love to be able to say, for example, this specific element should be whatever color the current theme's -> background palette's -> disabled-list-option.

Thanks!!

enter image description here

JBoothUA
  • 3,040
  • 3
  • 17
  • 46
  • 2
    welcome to stackoverflow, a q/a site which you have to know the answer of your question. Not needed but, it is better you to share the scss part where you define your material theme. It may be the reason of down votes. – Okan Aslankan Aug 25 '18 at 20:00
  • Have you read this: https://material.angular.io/guide/theming-your-components – Robin Dijkhof Aug 30 '18 at 13:44
  • i have... i see it mentions primary, accent, and warn palettes though, and I was hoping to use a specific color from my background palette, do you have an example of that?? @RobinDijkhof – JBoothUA Aug 30 '18 at 14:25

4 Answers4

2

I think you want a .scss file where you put all your custom color variables and use them to generic CSSclass to apply all over your apps.

Then you need to do this things.

  1. Uses a theme.scss file which holds current material theme/ dynamic themes scss

@import '~@angular/material/theming';
@import './mytheme-sidemenu.scss';

// Primary theme
@include mat-core();
$mytheme-app-primary: mat-palette($mat-deep-purple, 700, 600);
$mytheme-app-accent: mat-palette($mat-red, A200, 900, A100);
$mytheme-app-warn: mat-palette($mat-deep-orange);
$mytheme-app-theme: mat-light-theme($mytheme-app-primary, $mytheme-app-accent, $mytheme-app-warn);
@include angular-material-theme($mytheme-app-theme);
// Secondary Theme
.mytheme-alt-theme {
    $mytheme-alt-primary: mat-palette($mat-teal, 500);
    $mytheme-alt-accent: mat-palette($mat-orange, 500);
    $mytheme-alt-warn: mat-palette($mat-red);
    $mytheme-alt-theme: mat-light-theme($mytheme-alt-primary, $mytheme-alt-accent, $mytheme-alt-warn);
    @include angular-material-theme($mytheme-alt-theme);
}

.mytheme-another-alt-theme{
   $mytheme-another-alt-primary: mat-palette($mat-blue, 500);
    $mytheme-another-alt-accent: mat-palette($mat-yellow, 500);
    $mytheme-another-alt-warn: mat-palette($mat-green);
    $mytheme-another-alt-theme: mat-light-theme($mytheme-another-alt-primary, $mytheme-another-alt-accent, $mytheme-another-alt-warn);
    @include angular-material-theme($mytheme-another-alt-theme);
}
// Using the $theme variable from the pre-built theme you can call the theming function
@include mytheme-sidemenu($mytheme-app-theme);
  1. Uses separate scss file to hold your custom css class

// Import all the tools needed to customize the theme and extract parts of it
@import "~@angular/material/theming";
// Define a mixin that accepts a theme and outputs the color styles for the component.
@mixin mytheme-sidemenu($theme) {
  // Extract whichever individual palettes you need from the theme.
  $primary: map-get($theme, primary);
  $accent: map-get(
    $theme,
    accent
  ); // Use mat-color to extract individual colors from a palette as necessary.

  .col-primary {
    color: mat-color($primary, 500) !important;
  }
  .col-accent {
    color: mat-color($accent, 300) !important;
  }
}
  1. Now you can use your custom css classes over the app and don't forget to put the scss file link within your angular.json/angular-cli.json

You can also use this classes too:
.col-primary {
    & .hue {
      &-50 {
        color: mat-color($primary, 50) !important;
      }
      &-100 {
        color: mat-color($primary, 100) !important;
      }
      .
      .
      .
     &-800 {
        color: mat-color($primary, 800) !important;
      }
    }
 }

Also, you can see the running Example of this code here

AlokeT
  • 1,086
  • 7
  • 21
  • I think this is close to where I want to go.. I think this is how I have everything setup, minus the mixin, which I don't fully understand.. also i'm trying to set parts of a custom component, not a standard button or anything like that..? How would I do that? Or even override a color in certain cases, we have a specific theme we're trying to stick too that is different from the material theme so i'd like an easy way to set custom colors from my theme. – JBoothUA Aug 28 '18 at 13:46
  • think where i might be getting lost, is it looks like you can only use the primary,accent,warn colors that way?? I would like to use like a specific color from my custom 'background' palette etc.. is that possible? – JBoothUA Aug 29 '18 at 16:03
  • 1
    See if you use the material part, I think you need to follow their guidelines where you can follow. Also you use your own CSS in material component by overriding Angualar styles with encapsulation: ViewEncapsulation.None. if you can reproduce a demo using stackblitz it help me to understand your requirement. – AlokeT Aug 29 '18 at 17:33
  • i can post anything that will help, but I think you're understanding correctly. we just have a very specific theme I'm trying to stick with and I need to do things differently than the material guidelines. i would like to override the material theme where necessary in the most "designed" way – JBoothUA Aug 29 '18 at 20:23
  • i was thinking a mixin was what i needed maybe? but i guess not? – JBoothUA Aug 29 '18 at 20:42
  • 1
    See this is web technology and you can change anything that you want. But here @ mixin provided by material theme so you can't use outside of material themes data, I think you need to override them everywhere to apply your own specific theme. – AlokeT Aug 30 '18 at 03:01
2

I searched for reuse same color scss and came across this post, which I highly recommend so you can understand reusing values throughout your SCSS files, in order to make them more maintainable.

To sum it up, you can define SCSS variables by defining something like this:

$important-color: rgb(1, 2, 3);

And then reuse it like so:

class-1 {
    background-color: $important-color;
}

...

class-2 > span {
    color: $important-color;
}

So what you should probably do is to define one of these constants in a SCSS file higher up in the hierarchy (such as app.scss, for instance, if this is an application-wide variable) and use this variable in your components. Please, make sure to read Aloke's answer as well for a more in-depth analysis.

Always use the search, and be sure to include your code when asking code-related questions (use websites such as JSFiddle to make things easier).

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
ogabrielp
  • 89
  • 7
2

The answer to THIS: question is actually what I was looking for: It was much easier than I thought:

.mat-option {
  color : mat-color($foreground, text);
}

Angular Material - change color of clicked mat-list-option

JBoothUA
  • 3,040
  • 3
  • 17
  • 46
1

Why don't use Angular Material Theming built-in functionnalities ?

That way, you only have to create a custom Color Palette and affect it to either primary/accent/warn color (or use a pre-defined theme) with a bit of scss (to customize base colors)

Then the rest is done automatically by selecting the good color while using angular material components :

<button mat-raised-button color="primary">Primary</button>
<button mat-raised-button color="accent">Accent</button>
<button mat-raised-button color="warn">Warn</button>

In your components you can also use theses palettes quite easily if needed :

button {
    background-color: mat-color($accent);
}
Jscti
  • 14,096
  • 4
  • 62
  • 87
  • i'm trying to set parts of a custom component, not a standard button or anything like that..? How would I do that? Or even override a color in certain cases, we have a specific theme we're trying to stick too that is different from the material theme so i'd like an easy way to set custom colors from my theme. – JBoothUA Aug 28 '18 at 13:43
  • 1
    The link I provided contains a reference to what you need : https://material.angular.io/guide/theming-your-components – Jscti Aug 28 '18 at 13:49
  • again, I'm very new to this.. so i checked out the link, and that does look very helpful. i think that made things a little clearer in my head and I will try to checkout these mixins further.. i think where i might be getting lost, is it looks like you can only use the primary,accent,warn colors that way?? I would like to use like a specific color from my custom 'background' palette etc.. is that possible? – JBoothUA Aug 28 '18 at 15:51