@nikojpapa's answer here is the recommended method of changing Angular Material component styles. I'll add a bit more details here.
The Slider Toggle component has a property named color
that expects a ThemePalette
value. Possible ThemePalette values are 'primary', 'accent', and 'warn'. These relate to the primary, accent and warn color palette variants of the theme. The color property of this component defaults to 'accent'.
So, to customise the colors, we need to provide a color palette as the accent of the theme.
Let's assume we want to change only the slider component's styles.
To do this:
- We need to define a new color palette that fits our needs.
- We need to create a new theme, passing our color palette as the variant being used by the slider toggle component (as in if the slider toggle color is 'primary', we need to create the theme with our color palette as the primary variant).
- We need to change the slider toggle's theme to our newly defined theme.
We do all of these steps in the styles.scss
file of the Angular app.
Example:
/* styles.scss */
/* required set-up */
@import '~@angular/material/theming';
@include mat-core();
/** 1. Define new color palette. */
$md-custom-toggle-colors: (
50 : #e8f6e9,
100 : #c5e9c8,
200 : #9fdaa4,
300 : #79cb7f,
400 : #5cc063,
500 : #3fb548,
600 : #39ae41,
700 : #31a538,
800 : #299d30,
900 : #1b8d21,
A100 : #c6ffc9,
A200 : #93ff98,
A400 : #60ff67,
A700 : #47ff4f,
contrast: (
50 : #000000,
100 : #000000,
200 : #000000,
300 : #000000,
400 : #000000,
500 : #000000,
600 : #ffffff,
700 : #ffffff,
800 : #ffffff,
900 : #ffffff,
A100 : #000000,
A200 : #000000,
A400 : #000000,
A700 : #000000,
)
);
/**
2. Create new theme, passing above palette as the required variant.
Since the default variant of the Slider Toggle is 'accent', we will pass the above palette as such.
*/
$custom-toggle-theme: mat-light-theme(mat-palette($mat-red), mat-palette($md-custom-toggle-colors));
/** 3. Set new theme as slider toggle theme. */
@include mat-slide-toggle-theme($custom-toggle-theme);
Ensure that you include this styles.scss
file in the styles array within your angular.json
file. Note that the order of the style files is important here - later files replace the styles defined in preceding files.
You probably already have a pre-built Angular Material style file included in the array. To apply our newly defined styles, the styles array should be something like below:
{
"projects" : {
"architect": {
"build": {
"styles" : [
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
"./src/styles.scss"
]
}
}
}
}