AngularMaterialCssVars
You want to style your angular material dynamically with all the colors in the rainbow and don't really care about IE11 (support for css variables is required)? Look no further!
Check out the Demo!
Setup
- Install:
npm i angular-material-css-vars -S
- If @angular/material is already configured remove
@import '~@angular/material/theming';
from your main stylesheet file if present.
- Add this to your main stylesheet instead:
@import '~angular-material-css-vars/main';
$mat-css-dark-theme-selector: '.isDarkTheme';
$mat-css-light-theme-selector: '.isLightTheme';
@include initMaterialCssVars();
- Add to your main module:
import {MaterialCssVarsModule} from 'angular-material-css-vars';
@NgModule({
imports: [
MaterialCssVarsModule.forRoot({
isAutoContrast: true,
darkThemeClass: 'isDarkTheme',
lightThemeClass: 'isLightTheme',
}),
],
})
export class AppModule {
}
Additional Features
- Auto or manually set contrast color via
setAutoContrastEnabled(bool)
changeContrastColorThreshold(hueVal: HueValue)
- Helper to set all variables
setVariable(cssVarName: MaterialCssVariables, value: string)
- You can use the
MaterialCssVariables
enum from here to make sure you get the variable name right.
- Rudimentary dark theme support via body class
setDarkTheme(isDark: boolean)
Utility
There are also several utility functions and mixins.
@import '~angular-material-css-vars/public-util';
.with-color {
border-color: mat-css-color-primary(300);
}
.color-and-contrast {
@include mat-css-color-and-contrast(300);
}
.with-bg {
@include mat-css-bg(300);
}
Initialization Options
You can provide different options before initialization to change the body class used for the dark theme and to provide different default styles:
$mat-css-dark-theme-selector: '.isDarkTheme';
$mat-css-light-theme-selector: '.isLightTheme';
@include initMaterialCssVars();
To make those variables take effect with your mixins, you need to make sure that they are also defined before using them. E.g.:
$mat-css-dark-theme-selector: '.isDarkThemeCUSTOM';
@import '~angular-material-css-vars/public-util';
.my-component {
@include mat-css-dark-theme {
}
}
A full list of the theme map can be found here.
Credit...
...goes to @zbirizdo project on which parts of this are based which is in turn supposedly based on this gist.
Development server
Run ng serve
for a dev server. Navigate to http://localhost:4200/
. The app will automatically reload if you change any of the source files.