Skip to content

Commit 02c8f2a

Browse files
committedJan 26, 2022
perf(material-experimental/mdc-tabs): reduce bundle size (#24262)
Reworks the MDC-based tabs to reduce their bundle size. In total, the JS bundle has decreased by ~10kb and the dev app theme by ~50kb. The changes include: * Not using MDC's tab theme since we were basically only styling the tab label color. I've switched us over to the mixins for styling the label instead. * Using the `static-styles` mixin instead of `without-ripple`. (cherry picked from commit e7b7576)
1 parent 0477022 commit 02c8f2a

File tree

2 files changed

+16
-56
lines changed

2 files changed

+16
-56
lines changed
 

‎src/material-experimental/mdc-tabs/_tabs-common.scss

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ $mat-tab-animation-duration: 500ms !default;
1111
// Combines the various structural styles we need for the tab group and tab nav bar.
1212
@mixin structural-styles {
1313
@include mdc-helpers.disable-fallback-declarations {
14-
@include mdc-tab.without-ripple($query: mdc-helpers.$mat-base-styles-query);
14+
@include mdc-tab.static-styles($query: mdc-helpers.$mat-base-styles-query);
1515
@include mdc-tab-indicator.core-styles($query: mdc-helpers.$mat-base-styles-query);
1616
}
1717

@@ -51,6 +51,13 @@ $mat-tab-animation-duration: 500ms !default;
5151
pointer-events: none;
5252
}
5353

54+
// Required for `fitInkBarToContent` to work. This used to be included with MDC's `without-ripple`
55+
// mixin, but that no longer appears to be the case with `static-styles`. Since the latter is
56+
// ~10kb smaller, we include this one extra style ourselves.
57+
.mdc-tab__content {
58+
@include mdc-tab-indicator.surface;
59+
}
60+
5461
// We need to handle the hover and focus indication ourselves, because we don't use MDC's ripple.
5562
&:hover .mdc-tab__ripple::before {
5663
opacity: map.get(mdc-ripple.$dark-ink-opacities, hover);

‎src/material-experimental/mdc-tabs/_tabs-theme.scss

+8-55
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
@use '@material/tab-indicator' as mdc-tab-indicator;
55
@use '@material/tab-indicator/tab-indicator-theme' as mdc-tab-indicator-theme;
66
@use '@material/tab' as mdc-tab;
7-
@use '@material/tab/tab-theme' as mdc-tab-theme;
7+
@use '@material/tab/mixins' as mdc-tab-mixins;
88
@use '@material/tab-bar' as mdc-tab-bar;
99
@use '../mdc-helpers/mdc-helpers';
1010
@use '../../material/core/typography/typography';
@@ -18,39 +18,9 @@
1818

1919
@include mdc-helpers.mat-using-mdc-theme($config) {
2020
.mat-mdc-tab, .mat-mdc-tab-link {
21-
$surface: mdc-theme-color.$surface;
22-
$on-surface: rgba(mdc-theme-color.$on-surface, 0.6);
23-
24-
// TODO(crisbeto): these styles should actually be set through the `theme` mixin while the
25-
// `theme-styles` are included in the `tab` mixin inside `_tabs-common.scss`. Currently
26-
// they are not, because `theme-styles` outputs the token values directly, rather than
27-
// generating CSS variables.
28-
@include mdc-tab-theme.primary-navigation-tab-theme-styles(map.merge(
29-
mdc-tab-theme.$primary-light-theme,
30-
(
31-
container-color: $surface,
32-
inactive-focus-state-layer-color: $on-surface,
33-
inactive-hover-state-layer-color: $on-surface,
34-
inactive-pressed-state-layer-color: $on-surface,
35-
with-icon-inactive-focus-icon-color: $on-surface,
36-
with-icon-inactive-hover-icon-color: $on-surface,
37-
with-icon-inactive-icon-color: $on-surface,
38-
with-icon-inactive-pressed-icon-color: $on-surface,
39-
with-label-text-inactive-focus-label-text-color: $on-surface,
40-
with-label-text-inactive-hover-label-text-color: $on-surface,
41-
with-label-text-inactive-label-text-color: $on-surface,
42-
with-label-text-inactive-pressed-label-text-color: $on-surface,
43-
44-
// TODO(crisbeto): MDC's styles are set up so that the icon size is set through a
45-
// `font-size` at the root of the tab while the text size of the tab is set on
46-
// `.mdc-tab__text-label` which overrides the one from the root. The problem is that
47-
// the `$light-theme` is looking for a `subhead2` level which doesn't exist in MDC's
48-
// code which in turn causes no text label styles to be emitted and for the icon size
49-
// to be applied to the entire tab. Since we don't support icons inside the tab
50-
// anyway, we can temporarily work around it by preventing MDC from emitting icon
51-
// styles. The correct label typography will be applied by our theme instead.
52-
with-icon-icon-size: null
53-
)));
21+
&:not(.mat-mdc-tab-disabled) {
22+
@include mdc-tab-mixins.text-label-color(rgba(mdc-theme-color.$on-surface, 0.6));
23+
}
5424

5525
// MDC seems to include a background color on tabs which only stands out on dark themes.
5626
// Disable for now for backwards compatibility. These styles are inside the theme in order
@@ -125,27 +95,10 @@
12595

12696
@mixin _palette-styles($color) {
12797
.mat-mdc-tab, .mat-mdc-tab-link {
128-
// TODO(crisbeto): these styles should actually be set through the `theme` mixin while the
129-
// `theme-styles` are included in the `tab` mixin inside `_tabs-common.scss`. Currently
130-
// they are not, because `theme-styles` outputs the token values directly, rather than
131-
// generating CSS variables.
132-
@include mdc-tab-theme.primary-navigation-tab-theme-styles((
133-
active-focus-state-layer-color: $color,
134-
active-hover-state-layer-color: $color,
135-
active-pressed-state-layer-color: $color,
136-
with-icon-active-focus-icon-color: $color,
137-
with-icon-active-hover-icon-color: $color,
138-
with-icon-active-icon-color: $color,
139-
with-icon-active-pressed-icon-color: $color,
140-
with-label-text-active-focus-label-text-color: $color,
141-
with-label-text-active-hover-label-text-color: $color,
142-
with-label-text-active-label-text-color: $color,
143-
with-label-text-active-pressed-label-text-color: $color,
144-
));
145-
146-
@include mdc-tab-indicator-theme.theme-styles((
147-
active-indicator-color: $color
148-
));
98+
&:not(.mat-mdc-tab-disabled) {
99+
@include mdc-tab-mixins.active-text-label-color($color);
100+
@include mdc-tab-indicator-theme.theme-styles((active-indicator-color: $color));
101+
}
149102
}
150103

151104
.mdc-tab__ripple::before,

0 commit comments

Comments
 (0)
Please sign in to comment.