In my angular project Im creating the css classes by my own based on the given Material Theme. Therefor you can use these classes all over your html styling - its a more decoupled way. In the first step I've created the initial css classes (im using scss) for the several theming modes like dark or light mode:
.default-theme {
&--light {
@include angular-material-theme($default--light);
@include create-css-class-colors($default--light, "light");
}
&--dark {
@include angular-material-theme($default--dark);
@include create-css-class-colors($default--dark, "dark");
}
}
This setup could be done in the style.scss file.
After you have done the initial setup you have to implement the mixin in the code example above for "create-css-class-colors($default--light, "light")" in an extra file or leave it all in one large file for testing:
@mixin create-css-class-colors($colors, $mode) {
.text-color,
.font-color {
@include text-color-list($colors, $mode);
}
.background-color {
@include background-color-list($colors, $mode);
}
}
The mixin above has always a css-class name and an additional function for creating the responsible css-classes like background-color--primary or font-color--primary (naming is up to you). The logic of these functions is like the following:
@mixin background-color-list($map, $mode) {
$color-list: get-colors($map, $mode);
@each $color in $color-list {
$color-name: map-get($color, name);
&--#{$color-name} {
background-color: map-get($color, color) !important;
}
}
}
One last step is needed to complete all the setup. Besides of Angular Material you could define further colors you wanna use in your project. To do so you need another function "get-colors($map, $mode)":
@function get-colors($map, $mode) {
$primary-color: (
name: "primary",
color: getSpecificThemeColor($map, primary),
);
$accent-color: (
name: "accent",
color: getSpecificThemeColor($map, accent),
);
$info: (
name: "info",
color: #00bcd4,
);
$color-list: $primary-color,
$accent-color,
$info;
@return $color-list;
}
To get the specific theme color of the angular theme you have to implement one last function "getSpecificThemeColor($theme, $colorType)":
@function getSpecificThemeColor($theme, $colorType) {
@if map-has-key($theme, color) {
$colors: map-get($theme, color);
$color: map-get($colors, $colorType);
@return mat-color($color);
}
@else {
$color: map-get($theme, $colorType);
@return mat-color($color);
}
}
Now you are able to use the css classes to style your none-angular-material-components. If you wanna set the background-color of a div you can now use the generated css class "background-color--primary" or if you wanna set the font-color to accent you can use font-color--accent etc. but with this setup it is possible to generate your own color css classes as well like info.
` text based on my app current theme. like if user choosed green theme then `h1` text color should change to green and so..
– Rahul Jul 17 '17 at 10:12