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
You can use
class="mat-primary"
andclass="mat-accent"
on HTML elements to get the primary and accent colours of your theme.I know this is super late to the party, but didn't want to pull a DenverCoder9 after finally figuring out a clean way to do this.
First, go to this link on the angular material2 github and figure out what color palettes your theme is using. (That link points to version 6, so make sure you change the tag to whatever version you're using.)
Then create a
variables.scss
file in your project somewhere to store the palette references for your theme (the example below uses the palettes for the indigo-pink theme):You can then include the
variables.scss
file into your style sheets and usemat-color(<palette>)
to get the color for you classes.Using this method, you can still use the pre-built themes. It would probably be even cleaner just to re-create the theme using their published documentation, but for now I'm happy with this.
Hopefully that saves the next guy a lot of pain and suffering down the road.
UPDATE: Redefine Default Theme to Prevent OhGodTheyChangedColorsThisRelease
So who knows when angular material might change their theme colors, so its probably a good idea to just re-create the default them.
So building on the previous part of the post, add the
mat-light-theme
/mat-dark-theme
mixins to re-define the new theme.Then in your master root
styles.scss
for your app,Make sure you drop the
<link href=>
in yourindex.html
for the default stylesheet.Great! Now if you update angular material and they changed the colors, your good!, And you can update the colors across the app with just mucking with the palettes in
variables.scss
.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:
A snippet from my application scss file:
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:I have a div wrapping my entire application using the same
ngClass
approach to either apply thedarkTheme
class or not depending on the value of theisDarkTheme
variable. This take care of all Material-aware elements in my entire application in one shot, and I just use thelight-colors
anddark-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):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):From the "background" palette (
$light-background-palette
and$dark-background-palette
in my code above):Here are the sources I used to put this together:
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...