Skip to content

Commit 8719700

Browse files
authored
perf(material/tabs): reduce amount of CSS generated for background color (#26186)
We were generating more than 15kb of selectors to support the background colors for tabs. These changes move the selectors into the base styles and use variables to set the colors.
1 parent 93818e5 commit 8719700

File tree

7 files changed

+61
-44
lines changed

7 files changed

+61
-44
lines changed

src/components-examples/material/tabs/tab-group-theme/tab-group-theme-example.html

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,26 @@
22
<mat-button-toggle-group #colorToggle="matButtonToggleGroup"
33
value="primary"
44
aria-label="Change color">
5-
<mat-button-toggle value="primary"> Primary </mat-button-toggle>
6-
<mat-button-toggle value="accent"> Accent </mat-button-toggle>
5+
<mat-button-toggle value="primary">Primary</mat-button-toggle>
6+
<mat-button-toggle value="accent">Accent</mat-button-toggle>
7+
<mat-button-toggle value="warn">Warn</mat-button-toggle>
78
</mat-button-toggle-group>
8-
<span class="example-button-toggle-label"> Color </span>
9+
<span class="example-button-toggle-label">Color</span>
910
</div>
1011

1112
<div>
1213
<mat-button-toggle-group #backgroundColorToggle="matButtonToggleGroup"
1314
value="primary"
1415
aria-label="Change color">
15-
<mat-button-toggle value="primary"> Primary </mat-button-toggle>
16-
<mat-button-toggle value="accent"> Accent </mat-button-toggle>
16+
<mat-button-toggle value="primary">Primary</mat-button-toggle>
17+
<mat-button-toggle value="accent">Accent</mat-button-toggle>
18+
<mat-button-toggle value="warn">Warn</mat-button-toggle>
1719
</mat-button-toggle-group>
18-
<span class="example-button-toggle-label"> Background Color </span>
20+
<span class="example-button-toggle-label">Background Color</span>
1921
</div>
2022

2123
<mat-tab-group [color]="colorToggle.value" [backgroundColor]="backgroundColorToggle.value">
22-
<mat-tab label="First"> Content 1 </mat-tab>
23-
<mat-tab label="Second"> Content 2 </mat-tab>
24-
<mat-tab label="Third"> Content 3 </mat-tab>
24+
<mat-tab label="First">Content 1</mat-tab>
25+
<mat-tab label="Second">Content 2</mat-tab>
26+
<mat-tab label="Third">Content 3</mat-tab>
2527
</mat-tab-group>

src/material/tabs/_tabs-common.scss

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,39 @@ $mat-tab-animation-duration: 500ms !default;
196196
overflow: hidden;
197197
z-index: 1;
198198
}
199+
200+
@mixin paginated-tab-header-with-background($header-selector) {
201+
// TODO(crisbeto): the class is repeated here to ensure the specificity is high enough.
202+
// We should be able to remove it once the tabs are switched to the new theming API.
203+
&.mat-tabs-with-background.mat-tabs-with-background {
204+
// Note that these selectors target direct descendants so
205+
// that the styles don't apply to any nested tab groups.
206+
> #{$header-selector}, > .mat-mdc-tab-header-pagination {
207+
// Set background color for the tab group
208+
background-color: var(--mat-mdc-tab-header-with-background-background-color, transparent);
209+
}
210+
211+
> #{$header-selector} {
212+
// Set labels to contrast against background
213+
.mat-mdc-tab .mdc-tab__text-label, .mat-mdc-tab-link .mdc-tab__text-label {
214+
color: var(--mat-mdc-tab-header-with-background-foreground-color, inherit);
215+
}
216+
217+
.mdc-tab-indicator__content--underline,
218+
.mat-mdc-tab-header-pagination-chevron,
219+
.mat-mdc-focus-indicator::before {
220+
border-color: var(--mat-mdc-tab-header-with-background-foreground-color, inherit);
221+
}
222+
}
223+
224+
> #{$header-selector}, > .mat-mdc-tab-header-pagination {
225+
.mat-ripple-element, .mdc-tab__ripple::before {
226+
background-color: var(--mat-mdc-tab-header-with-background-foreground-color, inherit);
227+
}
228+
229+
.mat-mdc-tab-header-pagination-chevron {
230+
border-color: var(--mat-mdc-tab-header-with-background-foreground-color, inherit);
231+
}
232+
}
233+
}
234+
}

src/material/tabs/_tabs-theme.scss

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -62,36 +62,10 @@
6262
}
6363

6464
@mixin _background($background-color, $foreground-color) {
65-
// Note that these selectors target direct descendants so
66-
// that the styles don't apply to any nested tab groups.
67-
68-
> .mat-mdc-tab-header, > .mat-mdc-tab-link-container, > .mat-mdc-tab-header-pagination {
69-
// Set background color for the tab group
70-
@include mdc-theme.prop(background-color, $background-color);
71-
}
72-
73-
> .mat-mdc-tab-header, > .mat-mdc-tab-link-container {
74-
// Set labels to contrast against background
75-
.mat-mdc-tab .mdc-tab__text-label, .mat-mdc-tab-link .mdc-tab__text-label {
76-
@include mdc-theme.prop(color, $foreground-color);
77-
}
78-
79-
.mdc-tab-indicator__content--underline,
80-
.mat-mdc-tab-header-pagination-chevron,
81-
.mat-mdc-focus-indicator::before {
82-
@include mdc-theme.prop(border-color, $foreground-color);
83-
}
84-
}
85-
86-
> .mat-mdc-tab-header, > .mat-mdc-tab-link-container, > .mat-mdc-tab-header-pagination {
87-
.mat-ripple-element, .mdc-tab__ripple::before {
88-
@include mdc-theme.prop(background-color, $foreground-color);
89-
}
90-
91-
.mat-mdc-tab-header-pagination-chevron {
92-
@include mdc-theme.prop(border-color, $foreground-color);
93-
}
94-
}
65+
$background-value: mdc-theme-color.prop-value($background-color);
66+
$foreground-value: mdc-theme-color.prop-value($foreground-color);
67+
--mat-mdc-tab-header-with-background-background-color: #{$background-value};
68+
--mat-mdc-tab-header-with-background-foreground-color: #{$foreground-value};
9569
}
9670

9771
@mixin _palette-styles($color) {

src/material/tabs/tab-group.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
}
2121

2222
.mat-mdc-tab-group {
23+
@include tabs-common.paginated-tab-header-with-background('.mat-mdc-tab-header');
2324
display: flex;
2425
flex-direction: column;
2526

src/material/tabs/tab-group.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,12 @@ export abstract class _MatTabGroupBase
199199
}
200200

201201
set backgroundColor(value: ThemePalette) {
202-
const nativeElement: HTMLElement = this._elementRef.nativeElement;
202+
const classList: DOMTokenList = this._elementRef.nativeElement.classList;
203203

204-
nativeElement.classList.remove(`mat-background-${this.backgroundColor}`);
204+
classList.remove('mat-tabs-with-background', `mat-background-${this.backgroundColor}`);
205205

206206
if (value) {
207-
nativeElement.classList.add(`mat-background-${value}`);
207+
classList.add('mat-tabs-with-background', `mat-background-${value}`);
208208
}
209209

210210
this._backgroundColor = value;

src/material/tabs/tab-nav-bar/tab-nav-bar.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@
1010
.mat-mdc-tab-link-container {
1111
@include tabs-common.paginated-tab-header-container;
1212
}
13+
14+
.mat-mdc-tab-nav-bar {
15+
@include tabs-common.paginated-tab-header-with-background('.mat-mdc-tab-link-container');
16+
}

src/material/tabs/tab-nav-bar/tab-nav-bar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ export abstract class _MatTabNavBase
7575

7676
set backgroundColor(value: ThemePalette) {
7777
const classList = this._elementRef.nativeElement.classList;
78-
classList.remove(`mat-background-${this.backgroundColor}`);
78+
classList.remove('mat-tabs-with-background', `mat-background-${this.backgroundColor}`);
7979

8080
if (value) {
81-
classList.add(`mat-background-${value}`);
81+
classList.add('mat-tabs-with-background', `mat-background-${value}`);
8282
}
8383

8484
this._backgroundColor = value;

0 commit comments

Comments
 (0)