|
| 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 | +$light-theme: mat-light-theme((color: ...)); |
| 13 | +$dark-theme: mat-dark-theme((color: ...)); |
| 14 | + |
| 15 | +// Generates styles for all systems configured in the theme. In this case, color styles |
| 16 | +// and default density styles are generated. Density is in themes by default. |
| 17 | +@include angular-material-theme($light-theme); |
| 18 | + |
| 19 | +.dark-theme { |
| 20 | + // Generates styles for all systems configured in the theme. In this case, color styles |
| 21 | + // and the default density styles are generated. **Note** that this is a problem because it |
| 22 | + // means that density styles are generated *again*, even though only the color should change. |
| 23 | + @include angular-material-theme($dark-theme); |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +To fix this, you can use the dedicated mixin for color styles for the `.dark-theme` |
| 28 | +selector. Replace the `angular-material-theme` mixin and include the dark theme using the |
| 29 | +`angular-material-color` mixin. For example: |
| 30 | + |
| 31 | +```scss |
| 32 | +... |
| 33 | +@include angular-material-theme($light-theme); |
| 34 | + |
| 35 | +.dark-theme { |
| 36 | + // This mixin only generates the color styles now. |
| 37 | + @include angular-material-color($dark-theme); |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +Typography can also be configured via Sass mixin; see `angular-material-typography`. |
| 42 | + |
| 43 | +**Example #2** |
| 44 | + |
| 45 | +Theme styles could also be duplicated if individual theme mixins are used. For example: |
| 46 | + |
| 47 | +```scss |
| 48 | +@include angular-material-theme($my-theme); |
| 49 | + |
| 50 | +.my-custom-dark-button { |
| 51 | + // This will also generate the default density styles again. |
| 52 | + @include mat-button-theme($my-theme); |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +To avoid this duplication of styles, use the dedicated mixin for the color system and |
| 57 | +extract the configuration for the color system from the theme. |
| 58 | + |
| 59 | +```scss |
| 60 | +.my-custom-dark-button { |
| 61 | + // This will only generate the color styles for `mat-button`. |
| 62 | + @include mat-button-color($my-theme); |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +#### Disabling duplication warnings |
| 67 | + |
| 68 | +If your application intentionally duplicates styles, a global Sass variable can be |
| 69 | +set to disable duplication warnings from Angular Material. For example: |
| 70 | + |
| 71 | +```scss |
| 72 | +$mat-theme-ignore-duplication-warnings: true; |
| 73 | + |
| 74 | +// Include themes as usual. |
| 75 | +@include angular-material-theme($light-theme); |
| 76 | + |
| 77 | +... |
| 78 | +``` |
0 commit comments