Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get primary or accent color of currently applied theme in angular material 2

I'm building an app with multiple theme with angular material design 2. I created multiple theme and it's working really great. Using this guideline : Angular Material design theme

But the problem is that if user select "green theme" for example. Then I want to display his/her name in green and so. But how can I get the currently selected theme in this case "green" in my component style and then use that primary variable in my user name class to change its color

like image 624
Rahul Avatar asked Jul 13 '17 19:07

Rahul


People also ask

How do I change the theme of angular materials?

add("custom-theme"); With adding class="mat-app-background" to the <body> tag, the correct background color will take effect. As you call @include angular-material-theme($custom-theme); within your custom custom-them-css-class mixin, the mat-app-background css-selector will become a nested selector.

What is theming in angular?

Angular Material's theming system lets you customize color and typography styles for components in your application. The theming system is based on Google's Material Design specification. This document describes the concepts and APIs for customizing colors. For typography customization, see Angular Material Typography.

How do I import angular materials prebuilt themes indigo pink css?

In src/styles. css add: @import "~@angular/material/prebuilt-themes/indigo-pink.


1 Answers

I'm not sure if this is the "correct" way to do it, but it works, so I'm running with it for now. I'll adapt if there's a better way. My goal was to be able to style non-Material elements (such as standard DIVs, SPANs, etc) with different colors depending on which Material theme was currently applied. It took a combination of Material 2 and Angular 2 elements to make it all work.

Here is what I did: My custom theme file looks like this:

@import '~@angular/material/_theming.scss';  @include mat-core();  // default theme: $primary: mat-palette($mat-blue,800); $accent: mat-palette($mat-teal); $theme: mat-light-theme($primary, $accent);  @include angular-material-theme($theme);  // "dark" theme $dark-p: mat-palette($mat-blue-grey, 500); $dark-a: mat-palette($mat-blue-grey,900); $dark-t: mat-dark-theme($dark-p, $dark-a);  .darkTheme {   @include angular-material-theme($dark-t); } 

A snippet from my application scss file:

@import '../../themes/main-theme';  //  <-- the theme file shown above  //default palette forground/background: $light-foreground-palette: map-get($theme, foreground); $light-background-palette: map-get($theme, background);  //dark palette forground/background: $dark-foreground-palette: map-get($dark-t, foreground); $dark-background-palette: map-get($dark-t, background);  .light-colors{     background-color : mat-color($primary, default);     color: mat-color($light-foreground-palette, text); } .dark-colors{     background-color : mat-color($dark-p, default);     color: mat-color($dark-foreground-palette, text); } 

In my "theme" service (although you could do it in any service, as long as it's available globally, or at least anywhere you need it), I defined a simple boolean variable isDarkTheme. I use that to control display depending on whether the user has selected the "dark" theme.

Then wherever I need to, I use ngClass to apply classes dynamically, depending on the value of the global isDarkTheme variable:

<div [ngClass]="{'light-colors' : !svc.isDarkTheme,'dark-colors' : svc.isDarkTheme}"> ...my content... </div> 

I have a div wrapping my entire application using the same ngClass approach to either apply the darkTheme class or not depending on the value of the isDarkTheme variable. This take care of all Material-aware elements in my entire application in one shot, and I just use the light-colors and dark-colors on the specific non-Material elements where i need to. I could probably combine these, but for now I'm leaving things as-is.

For completeness, here are the lists of the elements you can get from the different palettes: From the "primary" palette ($primary and $dark-p in my code above):

  • default
  • lighter
  • darker

You can also get these same three color values for the $accent and $warn palettes.

From the "foreground" palette ($light-foreground-palette and $dark-foreground-palette in my code above):

  • base
  • divider
  • dividers
  • disabled
  • disabled-button
  • disabled-text
  • hint-text
  • secondary-text
  • icon
  • icons
  • text
  • slider-off
  • slider-off-active

From the "background" palette ($light-background-palette and $dark-background-palette in my code above):

  • status-bar
  • app-bar
  • background
  • hover
  • card
  • dialog
  • disabled-button
  • raised-button
  • focused-button
  • selected-button
  • selected-disabled-button
  • disabled-button-toggle

Here are the sources I used to put this together:

  • https://medium.com/@tomastrajan/the-complete-guide-to-angular-material-themes-4d165a9d24d1
  • How to change font color of primary palette in Angular Material2?
  • https://material.angular.io/guide/theming-your-components
  • The _theming.scss file from Material: https://github.com/angular/material2/blob/master/src/lib/core/theming/_theming.scss

I'll freely admit to only understanding about 80% of what's going on here, so if there's a better way, please let me know...

like image 99
TimTheEnchanter Avatar answered Sep 19 '22 06:09

TimTheEnchanter