5

I am shocked about how difficult it is to actually start using Angular Material pre-installed themes and how little tutorials there are on it. I am trying to use values from my theme using the function as explained in the official documentation:

https://material.angular.io/guide/theming-your-components

However, when I try to run this code

@use 'sass:map'
@use '@angular/material' as mat

@import './styles/normalize.css'
@import 'ag-grid-community/styles/ag-grid.css'
@import 'ag-grid-community/styles/ag-theme-material.css'

$my-palette: mat.define-palette(mat.$primary-palette)

I get a SASS error that the $theme is undefined. Where is the error?

1
  • could you able to provides some block of code Commented Dec 10, 2022 at 16:30

1 Answer 1

5

Oh yes. It's very tricky if you wanna style your material app. First of all, to better understanding what is to do, look at the material designer: Link. With it you can create your custom style.

Second: Here is a good video tutorial from Academind. Here you can learn what is todo and check all with the documation you linked. All you need to do is in this docu. But it's very crazy written. So this video will help!

But here is a quick guide:

  1. First of all create a new file (my-theme.scss as example)
  2. Inside the theme file add:
@use '@angular/material' as mat;

@include mat.core();

$my-primary: mat.define-palette(mat.$indigo-palette, 500);
$my-accent: mat.define-palette(mat.$pink-palette, A200, A100, A400);

$my-theme: mat.define-light-theme((
 color: (
   primary: $my-primary,
   accent: $my-accent,
 ),
 density: 0,
));

// Emit theme-dependent styles for common features used across multiple components.
@include mat.core-theme($my-theme);

// Emit styles for MatButton based on `$my-theme`. Because the configuration
// passed to `define-light-theme` omits typography, `button-theme` will not
// emit any typography styles.
@include mat.button-theme($my-theme);

// Include the theme mixins for other components you use here.

Ok, now open the angular.json file and go to the "styles" part. You wanna find anything like node_modules/@angular/material/prebuilt-themes/....... inside the "input" property. enter image description here

Replace this with your theme:

enter image description here

Then restart your app (call ng serve again because we changed a angular.json). And it will work as well!

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your time and this perfect explanation! I love using Angular but unfortunately I feel like some parts of documentation are poorly written. Thanks!
To be able to access the $theme do we have to create our own theme? It doesn't seem right (not saying your answer is wrong). It just feels... wrong.
Yes, it looks wrong and crazy. But under the hood Materials designs are pregenerated as css. So yes, you need to use your own theme because css does not have any functions and so on.
Is it though? I mean, I think that Material Design uses Sass, so the final css is generated during the build time, so I don't really understand why the MDA doesn't export $theme as a global variable. But we will see what's gonna happen with Material Design v3 Web once it will be out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.