|
11 | 11 |
|
12 | 12 | - **CSS variables** included by `mat.theme()`. These are prefixed with `--mat-sys` and can be used |
13 | 13 | in your component stylesheets to match the theme and design for your application. |
14 | | -- **CSS classes** included by `mat.system-classes()`. These utility classes wrap the CSS variables |
| 14 | +- **Utility classes** included by `mat.system-classes()`. These utility classes wrap the CSS variables |
15 | 15 | and allow you to apply theme styles directly from your component templates. |
16 | 16 |
|
17 | 17 | Both approaches are valid ways to ensure your application is consistently designed alongside Angular |
@@ -733,3 +733,55 @@ html { |
733 | 733 | @include mat.system-classes(); |
734 | 734 | } |
735 | 735 | ``` |
| 736 | + |
| 737 | +By using CSS variables and utility classes, you can avoid creating component-specific theme files |
| 738 | +and mixins that extract values from the `$theme` Sass map. For example, consider the following |
| 739 | +example of how styles used to be applied in custom components: |
| 740 | + |
| 741 | +```scss |
| 742 | +@use '@angular/material' as mat; |
| 743 | +@use 'sass:map'; |
| 744 | + |
| 745 | +@mixin my-component-theme($theme) { |
| 746 | + $foreground: map.get(mat.m2-get-color-config($theme), foreground); |
| 747 | + $background: map.get(mat.m2-get-color-config($theme), background); |
| 748 | + $primary: map.get(mat.m2-get-color-config($theme), primary); |
| 749 | + $typography: mat.m2-get-typography-config($config-or-theme); |
| 750 | + |
| 751 | + .widget-a { |
| 752 | + background-color: mat.m2-get-color-from-palette($background, card); |
| 753 | + color: mat.m2-get-color-from-palette($foreground, text); |
| 754 | + @include mat.m2-typography-level($config, body-1); |
| 755 | + } |
| 756 | + |
| 757 | + .widget-b { |
| 758 | + background-color: mat.m2-get-color-from-palette($primary, default); |
| 759 | + color: mat.m2-get-color-from-palette($primary, default-contrast); |
| 760 | + } |
| 761 | +} |
| 762 | +``` |
| 763 | + |
| 764 | +By using the CSS variables, you can define these theme styles in your component's |
| 765 | +stylesheet and avoid creating a separate theme file or mixin: |
| 766 | + |
| 767 | +```scss |
| 768 | +.widget-a { |
| 769 | + background-color: var(--mat-sys-surface); |
| 770 | + color: var(--mat-sys-on-surface); |
| 771 | + font: var(--mat-sys-body-medium); |
| 772 | +} |
| 773 | + |
| 774 | +.widget-b { |
| 775 | + background-color: var(--mat-sys-primary); |
| 776 | + color: var(--mat-sys-on-primary); |
| 777 | +} |
| 778 | +``` |
| 779 | + |
| 780 | +Taking it one step further, you can alternatively use the utility classes to achieve the same styles |
| 781 | +in the component template: |
| 782 | + |
| 783 | +```html |
| 784 | +<widget-a class="mat-bg-surface mat-font-body-medium"/> |
| 785 | + |
| 786 | +<widget-b class="mat-bg-primary"/> |
| 787 | +``` |
0 commit comments