I am currently on the process of redoing out clubs app from Angular 9 to 18, since it has not been well maintained. I am trying to keep as much of the frontend as possible and therefore also wanted to keep the design more or less the same.
I have found the following code defining the themes of our app within this code:
@import '~@angular/material/theming';
@include mat-core();
$my-app-primary: mat-palette($material-primary);
$my-app-accent: mat-palette($mat-pink, 500, 900, A100);
$my-app-warn: mat-palette($mat-red);
$my-app-theme: mat-light-theme($my-app-primary, $my-app-accent, $my-app-warn);
@include angular-material-theme($my-app-theme);
This can be found in the colours.scss, which is referenced in the styles.scss of our old app. I wanted to keep all of it intact, but since Angular 17 the use of @import
ist no longer possible, and I am supposed to use @use
(like in this ticket: https://github.com/angular/components/issues/28204)
I have tried to change the code myself and with the help of ChatGPT came up with this:
@use '@angular/material' as mat;
@include mat.core();
$my-app-primary: mat.define-palette(mat.$indigo-palette);
$my-app-accent: mat.define-palette(mat.$pink-palette, 500, 900, 'A100');
$my-app-warn: mat.define-palette(mat.$red-palette);
$my-app-theme: mat.define-light-theme((
color: (
primary: $my-app-primary,
accent: $my-app-accent,
warn: $my-app-warn,
)
));
@include mat.all-component-themes($my-app-theme);
Unfortunatelly it does not work and I get the following error: "Unknown Function: ‘define-palete’", etc.
Does anyone have a clue, what the right syntax is? Even the tutorials provided by Angular still reference the old system: https://v7.material.angular.io/guide/theming
2
Answers
Your Angular 9 app is using Material 2, which is no longer the default. Here is the right syntax for Angular Material 18 using Material 3:
This may help,
should work with Angular Material 18:
Key changes and explanations:
We use
@use '@angular/material' as mat;
to import the Material styles and assign them the namespacemat
.The palette definitions use
mat.define-palette()
and access the predefined palettes viamat.$color-palette
.For the accent palette, I’ve used the standard Material Design recommendation for accent colors (A200, A100, A400). You can adjust these if needed.
The theme is defined using
mat.define-light-theme()
with an object structure for better readability and future extensibility.Finally, we include all component themes with
mat.all-component-themes($my-app-theme)
.Additional considerations:
If you’re using custom colors, you may need to define custom palettes. For example: