Skip to content

Commit c932512

Browse files
committed
fix(material-experimental/theming): avoid re-emitting the same tokens from the backwards-compatibility styles
Currently the color variant backwards-compatibility styles emit all tokens, even though they were likely emitted already by the theme. This ends up increasing the theme size by about 50kb. These changes add a `emit-overrides-only` parameter to the token utilities and use it to only emit the palette-specific styles from the backwards-compatibility mixin.
1 parent d38798a commit c932512

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,85 @@
11
@use '@angular/material' as mat;
22

3+
// We want to emit only the overrides, because the backwards compatibility styles are usually
4+
// emitted after all the tokens have been included once already. This allows us to save ~50kb
5+
// from the bundle.
6+
$_overrides-only: true;
7+
38
@mixin color-variant-styles($theme, $color-variant) {
9+
$primary-options: (color-variant: $color-variant, emit-overrides-only: $_overrides-only);
10+
411
// Some components use the secondary color rather than primary color for `.mat-primary`.
512
// Those components should use the $secondary-color-variant.
6-
$secondary-color-variant: if($color-variant == primary, secondary, $color-variant);
13+
$secondary-options: (
14+
color-variant: if($color-variant == primary, secondary, $color-variant),
15+
emit-overrides-only: $_overrides-only
16+
);
717

8-
@include mat.option-color($theme, $color-variant: $secondary-color-variant);
9-
@include mat.progress-spinner-color($theme, $color-variant: $color-variant);
10-
@include mat.pseudo-checkbox-color($theme, $color-variant: $color-variant);
11-
@include mat.stepper-color($theme, $color-variant: $color-variant);
18+
@include mat.option-color($theme, $secondary-options...);
19+
@include mat.progress-spinner-color($theme, $primary-options...);
20+
@include mat.pseudo-checkbox-color($theme, $primary-options...);
21+
@include mat.stepper-color($theme, $primary-options...);
1222

1323
&.mat-icon {
14-
@include mat.icon-color($theme, $color-variant: $color-variant);
24+
@include mat.icon-color($theme, $primary-options...);
1525
}
1626

1727
&.mat-mdc-checkbox {
18-
@include mat.checkbox-color($theme, $color-variant: $color-variant);
28+
@include mat.checkbox-color($theme, $primary-options...);
1929
}
2030

2131
&.mat-mdc-slider {
22-
@include mat.slider-color($theme, $color-variant: $color-variant);
32+
@include mat.slider-color($theme, $primary-options...);
2333
}
2434

2535
&.mat-mdc-tab-group,
2636
&.mat-mdc-tab-nav-bar {
27-
@include mat.tabs-color($theme, $color-variant: $color-variant);
37+
@include mat.tabs-color($theme, $primary-options...);
2838
}
2939

3040
&.mat-mdc-slide-toggle {
31-
@include mat.slide-toggle-color($theme, $color-variant: $color-variant);
41+
@include mat.slide-toggle-color($theme, $primary-options...);
3242
}
3343

3444
&.mat-mdc-form-field {
35-
@include mat.select-color($theme, $color-variant: $color-variant);
45+
@include mat.select-color($theme, $primary-options...);
3646
}
3747

3848
&.mat-mdc-radio-button {
39-
@include mat.radio-color($theme, $color-variant: $color-variant);
49+
@include mat.radio-color($theme, $primary-options...);
4050
}
4151

4252
&.mat-mdc-progress-bar {
43-
@include mat.progress-bar-color($theme, $color-variant: $color-variant);
53+
@include mat.progress-bar-color($theme, $primary-options...);
4454
}
4555

4656
&.mat-mdc-form-field {
47-
@include mat.form-field-color($theme, $color-variant: $color-variant);
57+
@include mat.form-field-color($theme, $primary-options...);
4858
}
4959

5060
&.mat-datepicker-content {
51-
@include mat.datepicker-color($theme, $color-variant: $color-variant);
61+
@include mat.datepicker-color($theme, $primary-options...);
5262
}
5363

5464
&.mat-mdc-button-base {
55-
@include mat.button-color($theme, $color-variant: $color-variant);
65+
@include mat.button-color($theme, $primary-options...);
5666
}
5767

5868
&.mat-mdc-standard-chip {
59-
@include mat.chips-color($theme, $color-variant: $secondary-color-variant);
69+
@include mat.chips-color($theme, $secondary-options...);
6070
}
6171

6272
.mdc-list-item__start,
6373
.mdc-list-item__end {
64-
@include mat.checkbox-color($theme, $color-variant: $color-variant);
65-
@include mat.radio-color($theme, $color-variant: $color-variant);
74+
@include mat.checkbox-color($theme, $primary-options...);
75+
@include mat.radio-color($theme, $primary-options...);
6676
}
6777

6878
// M3 dropped support for warn/error color FABs.
6979
@if $color-variant != error {
7080
&.mat-mdc-fab,
7181
&.mat-mdc-mini-fab {
72-
@include mat.fab-color($theme, $color-variant: $color-variant);
82+
@include mat.fab-color($theme, $primary-options...);
7383
}
7484
}
7585
}
@@ -79,20 +89,23 @@
7989
@include color-variant-styles($theme, primary);
8090
}
8191
.mat-badge {
82-
@include mat.badge-color($theme, $color-variant: primary);
92+
@include mat.badge-color($theme, $color-variant: primary,
93+
$emit-overrides-only: $_overrides-only);
8394
}
8495

8596
.mat-accent {
8697
@include color-variant-styles($theme, tertiary);
8798
}
8899
.mat-badge-accent {
89-
@include mat.badge-color($theme, $color-variant: tertiary);
100+
@include mat.badge-color($theme, $color-variant: tertiary,
101+
$emit-overrides-only: $_overrides-only);
90102
}
91103

92104
.mat-warn {
93105
@include color-variant-styles($theme, error);
94106
}
95107
.mat-badge-warn {
96-
@include mat.badge-color($theme, $color-variant: error);
108+
@include mat.badge-color($theme, $color-variant: error,
109+
$emit-overrides-only: $_overrides-only);
97110
}
98111
}

src/material/core/tokens/_token-utils.scss

+6-3
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,19 @@ $_component-prefix: null;
185185
/// @param {List} $prefix The component prefix to get the token values for.
186186
/// @param {ArgList} Any additional options
187187
/// Currently the additional supported options are:
188-
// - $color-variant (The color variant to use for the component)
188+
// - $color-variant - The color variant to use for the component
189+
// - $emit-overrides-only - Whether to emit *only* the overrides for the
190+
// specific color variant, or all color styles. Defaults to false.
189191
/// @throws If given options are invalid
190192
/// @return {Map} The token values for the requested component.
191193
@function get-tokens-for($tokens, $prefix, $options...) {
192-
$options: sass-utils.validate-keyword-args($options, (color-variant));
194+
$options: sass-utils.validate-keyword-args($options, (color-variant, emit-overrides-only));
193195
@if $tokens == () {
194196
@return ();
195197
}
196198
$values: map.get($tokens, $prefix);
197199
$color-variant: map.get($options, color-variant);
200+
$emit-overrides-only: map.get($options, emit-overrides-only);
198201
@if $color-variant == null {
199202
@return $values;
200203
}
@@ -204,5 +207,5 @@ $_component-prefix: null;
204207
_supported-color-variants($tokens, $prefix)
205208
}#{'.'};
206209
}
207-
@return map.merge($values, $overrides);
210+
@return if($emit-overrides-only, $overrides, map.merge($values, $overrides));
208211
}

0 commit comments

Comments
 (0)