|
| 1 | +# Avoiding duplicated theming styles |
| 2 | + |
| 3 | +As explained in the [theming guide](./theming.md), a theme in Angular Material consists of |
| 4 | +configurations for the `color`, `density` and `typography` systems. As some of these individual |
| 5 | +systems have default configurations, some usage patterns may cause duplication in the CSS output. |
| 6 | + |
| 7 | +Below are examples of patterns that generate duplicative theme styles: |
| 8 | + |
| 9 | +**Example #1** |
| 10 | + |
| 11 | +```scss |
| 12 | +@use '@angular/material' as mat; |
| 13 | + |
| 14 | +$light-theme: mat.define-light-theme((color: ...)); |
| 15 | +$dark-theme: mat.define-dark-theme((color: ...)); |
| 16 | + |
| 17 | +// Generates styles for all systems configured in the theme. In this case, color styles |
| 18 | +// and default density styles are generated. Density is in themes by default. |
| 19 | +@include mat.all-component-themes($light-theme); |
| 20 | + |
| 21 | +.dark-theme { |
| 22 | + // Generates styles for all systems configured in the theme. In this case, color styles |
| 23 | + // and the default density styles are generated. **Note** that this is a problem because it |
| 24 | + // means that density styles are generated *again*, even though only the color should change. |
| 25 | + @include mat.all-component-themes($dark-theme); |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +To fix this, you can use the dedicated mixin for color styles for the `.dark-theme` |
| 30 | +selector. Replace the `all-component-themes` mixin and include the dark theme using the |
| 31 | +`all-component-colors` mixin. For example: |
| 32 | + |
| 33 | +```scss |
| 34 | +@use '@angular/material' as mat; |
| 35 | + |
| 36 | +... |
| 37 | +@include mat.all-component-themes($light-theme); |
| 38 | + |
| 39 | +.dark-theme { |
| 40 | + // This mixin only generates the color styles now. |
| 41 | + @include mat.all-component-colors($dark-theme); |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +Typography can also be configured via Sass mixin; see `all-component-typographies`. |
| 46 | + |
| 47 | +**Example #2** |
| 48 | + |
| 49 | +Theme styles could also be duplicated if individual theme mixins are used. For example: |
| 50 | + |
| 51 | +```scss |
| 52 | +@use '@angular/material' as mat; |
| 53 | + |
| 54 | +@include mat.all-component-themes($my-theme); |
| 55 | + |
| 56 | +.my-custom-dark-button { |
| 57 | + // This will also generate the default density styles again. |
| 58 | + @include mat.button-theme($my-theme); |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +To avoid this duplication of styles, use the dedicated mixin for the color system and |
| 63 | +extract the configuration for the color system from the theme. |
| 64 | + |
| 65 | +```scss |
| 66 | +@use '@angular/material' as mat; |
| 67 | + |
| 68 | +.my-custom-dark-button { |
| 69 | + // This will only generate the color styles for `mat-button`. |
| 70 | + @include mat.button-color($my-theme); |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +## Disabling duplication warnings |
| 75 | + |
| 76 | +If your application intentionally duplicates styles, a global Sass variable can be |
| 77 | +set to disable duplication warnings from Angular Material. For example: |
| 78 | + |
| 79 | +```scss |
| 80 | +@use '@angular/material' as mat; |
| 81 | + |
| 82 | +mat.$theme-ignore-duplication-warnings: true; |
| 83 | + |
| 84 | +// Include themes as usual. |
| 85 | +@include mat.all-component-themes($light-theme); |
| 86 | + |
| 87 | +... |
| 88 | +``` |
0 commit comments