Skip to content

Commit 2111972

Browse files
committed
feat(material-experimental/mdc-tabs): switch to new theming API
Switches the MDC-based tabs to the new theming API. Note that while this uses the new mixins which clean up some of our overrides, it doesn't get us there completely in regards to the new API. Currently MDC's tabs mixins output the token values directly, rather than generating CSS variables for them. That being said, this still makes it easier to switch to CSS variables once they're available. These changes also introduce a new `structural-styles` mixin in `tabs-common` which centralizes the various structural styles we include from MDC. Previously they were scattered across multiple files.
1 parent c649f7d commit 2111972

File tree

6 files changed

+90
-54
lines changed

6 files changed

+90
-54
lines changed

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
@use '@material/ripple' as mdc-ripple;
22
@use '@material/tab' as mdc-tab;
3+
@use '@material/tab-indicator' as mdc-tab-indicator;
34
@use 'sass:map';
45
@use '../../material/core/style/vendor-prefixes';
56
@use '../../cdk/a11y';
67
@use '../mdc-helpers/mdc-helpers';
78

89
$mat-tab-animation-duration: 500ms !default;
910

11+
// Combines the various structural styles we need for the tab group and tab nav bar.
12+
@mixin structural-styles {
13+
@include mdc-tab.without-ripple($query: mdc-helpers.$mat-base-styles-query);
14+
@include mdc-tab-indicator.core-styles($query: mdc-helpers.$mat-base-styles-query);
15+
@include _paginated-tab-header;
16+
17+
.mat-mdc-tab-ripple {
18+
position: absolute;
19+
top: 0;
20+
left: 0;
21+
bottom: 0;
22+
right: 0;
23+
pointer-events: none;
24+
}
25+
}
26+
1027
@mixin tab {
1128
&.mdc-tab {
1229
// This is usually included by MDC's tab bar, however we don't
@@ -17,6 +34,10 @@ $mat-tab-animation-duration: 500ms !default;
1734
// is an opt-in behavior. Also technically we don't need to combine the two classes, but
1835
// we need the extra specificity to avoid issues with CSS insertion order.
1936
flex-grow: 0;
37+
38+
// MDC seems to include a background color on tabs which only stands
39+
// out on dark themes. Disable for now for backwards compatibility.
40+
background-color: transparent;
2041
}
2142

2243
// Used to render out the background tint when hovered/focused. Usually this is done by
@@ -58,20 +79,9 @@ $mat-tab-animation-duration: 500ms !default;
5879
}
5980
}
6081

61-
@mixin tab-ripple {
62-
.mat-mdc-tab-ripple {
63-
position: absolute;
64-
top: 0;
65-
left: 0;
66-
bottom: 0;
67-
right: 0;
68-
pointer-events: none;
69-
}
70-
}
71-
7282
// Structural styles for a tab header. Used by both `mat-tab-header` and `mat-tab-nav-bar`.
7383
// We need this styles on top of MDC's, because MDC doesn't support pagination like ours.
74-
@mixin paginated-tab-header {
84+
@mixin _paginated-tab-header {
7585
.mat-mdc-tab-header {
7686
display: flex;
7787
overflow: hidden;

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

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,65 @@
1+
@use 'sass:map';
12
@use '@material/theme/theme-color' as mdc-theme-color;
23
@use '@material/theme/theme' as mdc-theme;
34
@use '@material/tab-indicator' as mdc-tab-indicator;
5+
@use '@material/tab-indicator/tab-indicator-theme' as mdc-tab-indicator-theme;
46
@use '@material/tab' as mdc-tab;
7+
@use '@material/tab/tab-theme' as mdc-tab-theme;
58
@use '@material/tab-bar' as mdc-tab-bar;
69
@use '../mdc-helpers/mdc-helpers';
710
@use '../../material/core/typography/typography';
811
@use '../../material/core/theming/theming';
912

1013
@mixin color($config-or-theme) {
1114
$config: theming.get-color-config($config-or-theme);
12-
// Save original values of MDC global variables. We need to save these so we can restore the
13-
// variables to their original values and prevent unintended side effects from using this mixin.
14-
$orig-text-label-color-active: mdc-tab.$text-label-color-active;
15-
$orig-icon-color-active: mdc-tab.$icon-color-active;
16-
$orig-text-label-color-default: mdc-tab.$text-label-color-default;
15+
$primary: theming.get-color-from-palette(map.get($config, primary));
16+
$accent: theming.get-color-from-palette(map.get($config, accent));
17+
$warn: theming.get-color-from-palette(map.get($config, warn));
1718

1819
@include mdc-helpers.mat-using-mdc-theme($config) {
19-
// This value is the same as MDC's default, but MDC defines it once inside
20-
// a variables file which means that we can't override it with our own palette.
21-
mdc-tab.$text-label-color-default:
22-
rgba(mdc-theme-color.prop-value(on-surface), mdc-tab.$text-label-opacity);
20+
.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.theme-styles(map.merge(mdc-tab-theme.$light-theme, (
29+
container-color: $surface,
30+
inactive-focus-state-layer-color: $on-surface,
31+
inactive-hover-state-layer-color: $on-surface,
32+
inactive-pressed-state-layer-color: $on-surface,
33+
with-icon-inactive-focus-icon-color: $on-surface,
34+
with-icon-inactive-hover-icon-color: $on-surface,
35+
with-icon-inactive-icon-color: $on-surface,
36+
with-icon-inactive-pressed-icon-color: $on-surface,
37+
with-label-text-inactive-focus-label-text-color: $on-surface,
38+
with-label-text-inactive-hover-label-text-color: $on-surface,
39+
with-label-text-inactive-label-text-color: $on-surface,
40+
with-label-text-inactive-pressed-label-text-color: $on-surface,
41+
42+
// TODO(crisbeto): MDC's styles are set up so that the icon size is set through a
43+
// `font-size` at the root of the tab while the text size of the tab is set on
44+
// `.mdc-tab__text-label` which overrides the one from the root. The problem is that the
45+
// `$light-theme` is looking for a `subhead2` level which doesn't exist in MDC's code which
46+
// in turn causes no text label styles to be emitted and for the icon size to be applied to
47+
// the entire tab. Since we don't support icons inside the tab anyway, we can temporarily
48+
// work around it by preventing MDC from emitting icon styles. The correct label typography
49+
// will be applied by our theme instead.
50+
with-icon-icon-size: null
51+
)));
52+
}
2353

24-
@include _palette-styles(mdc-tab.$text-label-color-active);
54+
@include _palette-styles($primary);
2555

2656
.mat-mdc-tab-group, .mat-mdc-tab-nav-bar {
2757
&.mat-accent {
28-
mdc-tab.$text-label-color-active: secondary;
29-
mdc-tab.$icon-color-active: secondary;
30-
@include _palette-styles(mdc-tab.$text-label-color-active);
58+
@include _palette-styles($accent);
3159
}
3260

3361
&.mat-warn {
34-
mdc-tab.$text-label-color-active: error;
35-
mdc-tab.$icon-color-active: error;
36-
@include _palette-styles(mdc-tab.$text-label-color-active);
62+
@include _palette-styles($warn);
3763
}
3864
}
3965

@@ -55,11 +81,6 @@
5581
@include mdc-theme.prop(border-color, on-surface);
5682
}
5783
}
58-
59-
// Restore original values of MDC global variables.
60-
mdc-tab.$text-label-color-active: $orig-text-label-color-active;
61-
mdc-tab.$icon-color-active: $orig-icon-color-active;
62-
mdc-tab.$text-label-color-default: $orig-text-label-color-default;
6384
}
6485

6586
@mixin _background($background-color, $foreground-color) {
@@ -73,7 +94,7 @@
7394

7495
> .mat-mdc-tab-header, > .mat-mdc-tab-link-container {
7596
// Set labels to contrast against background
76-
.mdc-tab__text-label, .mat-mdc-tab-link {
97+
.mat-mdc-tab .mdc-tab__text-label, .mat-mdc-tab-link .mdc-tab__text-label {
7798
@include mdc-theme.prop(color, $foreground-color);
7899
}
79100

@@ -96,9 +117,29 @@
96117
}
97118

98119
@mixin _palette-styles($color) {
99-
@include mdc-tab.without-ripple($query: mdc-helpers.$mat-theme-styles-query);
100-
@include mdc-tab-indicator.underline-color($color, $query: mdc-helpers.$mat-theme-styles-query);
101-
@include mdc-tab-indicator.icon-color($color, $query: mdc-helpers.$mat-theme-styles-query);
120+
.mat-mdc-tab, .mat-mdc-tab-link {
121+
// TODO(crisbeto): these styles should actually be set through the `theme` mixin while the
122+
// `theme-styles` are included in the `tab` mixin inside `_tabs-common.scss`. Currently
123+
// they are not, because `theme-styles` outputs the token values directly, rather than
124+
// generating CSS variables.
125+
@include mdc-tab-theme.theme-styles((
126+
active-focus-state-layer-color: $color,
127+
active-hover-state-layer-color: $color,
128+
active-pressed-state-layer-color: $color,
129+
with-icon-active-focus-icon-color: $color,
130+
with-icon-active-hover-icon-color: $color,
131+
with-icon-active-icon-color: $color,
132+
with-icon-active-pressed-icon-color: $color,
133+
with-label-text-active-focus-label-text-color: $color,
134+
with-label-text-active-hover-label-text-color: $color,
135+
with-label-text-active-label-text-color: $color,
136+
with-label-text-active-pressed-label-text-color: $color,
137+
));
138+
139+
@include mdc-tab-indicator-theme.theme-styles((
140+
active-indicator-color: $color
141+
));
142+
}
102143

103144
.mdc-tab__ripple::before,
104145
.mat-mdc-tab .mat-ripple-element,

src/material-experimental/mdc-tabs/tab-group.scss

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
@use '@material/tab' as mdc-tab;
21
@use '../../material/core/style/variables';
32
@use '../../material/core/style/private';
4-
@use '../mdc-helpers/mdc-helpers';
53
@use './tabs-common';
64

7-
@include mdc-tab.without-ripple($query: mdc-helpers.$mat-base-styles-query);
8-
@include tabs-common.tab-ripple;
5+
@include tabs-common.structural-styles;
96

107
.mat-mdc-tab {
118
@include tabs-common.tab;

src/material-experimental/mdc-tabs/tab-header.scss

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
@use '@material/tab-indicator' as mdc-tab-indicator;
2-
@use '../mdc-helpers/mdc-helpers';
31
@use './tabs-common';
42

5-
@include mdc-tab-indicator.core-styles($query: mdc-helpers.$mat-base-styles-query);
6-
@include tabs-common.paginated-tab-header;
7-
83
.mat-mdc-tab-label-container {
94
@include tabs-common.paginated-tab-header-container;
105
}

src/material-experimental/mdc-tabs/tab-nav-bar/tab-link.scss

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
@use '@material/tab' as mdc-tab;
2-
@use '@material/tab-indicator' as mdc-tab-indicator;
31
@use '../../../material/core/style/variables';
4-
@use '../../mdc-helpers/mdc-helpers';
52
@use '../tabs-common';
63

7-
@include mdc-tab.without-ripple($query: mdc-helpers.$mat-base-styles-query);
8-
@include mdc-tab-indicator.core-styles($query: mdc-helpers.$mat-base-styles-query);
9-
@include tabs-common.tab-ripple;
10-
114
// Wraps each link in the header
125
.mat-mdc-tab-link {
136
@include tabs-common.tab;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@use '../tabs-common';
22

3-
@include tabs-common.paginated-tab-header;
3+
@include tabs-common.structural-styles;
44

55
.mat-mdc-tab-links {
66
@include tabs-common.paginated-tab-header-item-wrapper('.mat-mdc-tab-link-container');

0 commit comments

Comments
 (0)