I’m using angular material 19.0.3 and the m3 theming mechanism
here’s my style.scss
html {
color-scheme: light dark;
@include mat.theme((color: (
primary: ft-green-theme.$primary-palette,
tertiary: ft-green-theme.$tertiary-palette,
),
typography: InterVariable,
density: 0));
&[dir='rtl'] {
@include mat.theme((typography: IranSans,
));
}
}
body.light {
color-scheme: light;
}
body.dark {
color-scheme: dark;
}
Now I have a button
<button mat-flat-button >click me</button>
I used to be able to do this
<button mat-flat-button color='danger'>click me</button>
and the button would be red.
But in the new version of angular material (19) they’ve changed things.
I tried to read the documentation but I don’t understand a thing.
In mat-button documentation it says:
Theme color of the button. This API is supported in M2 themes only, it has no effect in M3 themes.
For information on applying color variants in M3, see https://material.angular.io/guide/theming#using-component-color-variants.
And in the link provided there’s just an explanation about how to give a color pallette as a theme.
But suppose, I’m gonna have a confirmation dialog and the yes button will be green and the cancel button will be red.
How am I supposed to color individual mat-buttons in angular material 19??
2
Answers
When you use
@include mat.theme(...)
everything is taken care of internally, so there is no way to access thetheme
(to my knowledge) so you need to set the overrides manually using@include mat.button-overrides(( ... ))
.This method could be a problem, since you just want to theme the button based on whether it’s
primary
,secondary
, etc.As far as I know, the available options of types are
'primary', 'secondary', 'tertiary', 'error'
.We can define our own theme button by using
mat.button-color
, this mixin takes two arguments.The first is the
theme
, but when we use@include mat.theme(...)
we do not have this theme stored in a SCSS property, hence we can use the previous method of usingmat.define-theme
, Which provides the theme for use to customize.After this, it is simply a matter of inputting this theme to the function and setting the color variant.
Full Code:
SCSS:
TS:
HTML:
Stackblitz Demo
Easy way to create accent and other colored buttons in Angular Material v19 is simply to create another theme with your specific colors in a specific CSS selector. E.g.
And then just simply apply the class to your button when you want.
I discussed all of this in a recent video, if you’d like more details.
https://youtu.be/5NSH8VvJH5o