Skip to content

Commit db8f7b7

Browse files
committed
docs: add description for individual theme mixins
1 parent 6767e01 commit db8f7b7

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

guides/duplicate-theming-styles.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ As explained in the [theming guide](./theming.md), a theme in Angular Material c
44
configurations for the `color`, `density` and `typography` systems. As some of these individual
55
systems have default configurations, some usage patterns may cause duplication in the CSS output.
66

7-
Below is an example of a pattern that generates duplicative theme styles:
7+
Below are examples of patterns that generate duplicative theme styles:
8+
9+
**Example #1**
810

911
```scss
1012
$light-theme: mat-light-theme((color: ...));
@@ -36,7 +38,44 @@ selector. Replace the `angular-material-theme` mixin and include the dark theme
3638
}
3739
```
3840

39-
Typography can also be configured via Sass mixin; see `angular-material-typography`.
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(map_get($my-theme, color));
63+
}
64+
```
65+
66+
Alternatively, a new theme object can be constructed that explicitly disables
67+
styles from other systems such as `density` or `typography`:
68+
69+
```scss
70+
.my-custom-dark-button {
71+
$only-color-theme: map_merge($my-theme, (
72+
density: null,
73+
typography: null
74+
));
75+
// This will only generate the color styles for `mat-button`.
76+
@include mat-button-theme($only-color-theme);
77+
}
78+
```
4079

4180
#### Disabling duplication warnings
4281

guides/theming.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ styles for all configured theming system parts. For example, typography styles w
192192
multiple times, even though the configuration did not change. Instead, use fine-grained mixins such
193193
as `angular-material-color` that only result in styles being generated for the [color system][2].
194194

195+
Read more about duplicated theme styles in the [dedicated guide](./duplicate-theming-styles.md).
196+
195197
##### Multiple themes and overlay-based components
196198
Since certain components (e.g. menu, select, dialog, etc.) are inside of a global overlay container,
197199
an additional step is required for those components to be affected by the theme's css class selector
@@ -247,12 +249,20 @@ $candy-app-theme: mat-light-theme((
247249
#### Changing styles at run-time
248250

249251
##### Toggling classes
250-
You can use the mixins described above to define styles to customize any part of your application
251-
with standard CSS selectors. For example, let's say you want to toggle alternate colors on a button.
252+
You can use the theming mixins to customize any part of your application with standard
253+
CSS selectors. For example, let's say you want to toggle alternate colors on a button.
252254
You would first define a CSS class with the alternate colors.
255+
256+
Note that `mat-button-color` should be used instead of `mat-button-theme` as we only
257+
want to have alternate colors for the button. Using the theme mixin could result in
258+
duplicative theme styles if the `mat-button-theme` has been included before. Read more about
259+
this in the [dedicated guide](./duplicate-theming-styles.md).
260+
253261
```scss
254262
.alternate-button {
255-
@include mat-button-theme($alternate-theme);
263+
// Extract the color configuration from the theme and generate
264+
// the color theme styles for `mat-button`.
265+
@include mat-button-color(map_get($alternate-theme, color));
256266
}
257267
```
258268

0 commit comments

Comments
 (0)