Skip to content

Commit 2bd36cf

Browse files
zarendcrisbeto
andauthored
fix(material-experimental/mdc-tabs): switch to new theming API (#23726)
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. Co-authored-by: Kristiyan Kostadinov <crisbeto@abv.bg>
1 parent a259f22 commit 2bd36cf

File tree

6 files changed

+89
-50
lines changed

6 files changed

+89
-50
lines changed

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
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+
16+
.mat-mdc-tab-ripple {
17+
position: absolute;
18+
top: 0;
19+
left: 0;
20+
bottom: 0;
21+
right: 0;
22+
pointer-events: none;
23+
}
24+
}
25+
1026
@mixin tab {
1127
&.mdc-tab {
1228
// This is usually included by MDC's tab bar, however we don't
@@ -58,17 +74,6 @@ $mat-tab-animation-duration: 500ms !default;
5874
}
5975
}
6076

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-
7277
// Structural styles for a tab header. Used by both `mat-tab-header` and `mat-tab-nav-bar`.
7378
// We need this styles on top of MDC's, because MDC doesn't support pagination like ours.
7479
@mixin paginated-tab-header {

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

Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,70 @@
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+
53+
// MDC seems to include a background color on tabs which only stands out on dark themes.
54+
// Disable for now for backwards compatibility. These styles are inside the theme in order
55+
// to avoid CSS specificity issues.
56+
background-color: transparent;
57+
}
2358

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

2661
.mat-mdc-tab-group, .mat-mdc-tab-nav-bar {
2762
&.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);
63+
@include _palette-styles($accent);
3164
}
3265

3366
&.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);
67+
@include _palette-styles($warn);
3768
}
3869
}
3970

@@ -55,11 +86,6 @@
5586
@include mdc-theme.prop(border-color, on-surface);
5687
}
5788
}
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;
6389
}
6490

6591
@mixin _background($background-color, $foreground-color) {
@@ -73,7 +99,7 @@
7399

74100
> .mat-mdc-tab-header, > .mat-mdc-tab-link-container {
75101
// Set labels to contrast against background
76-
.mdc-tab__text-label, .mat-mdc-tab-link {
102+
.mat-mdc-tab .mdc-tab__text-label, .mat-mdc-tab-link .mdc-tab__text-label {
77103
@include mdc-theme.prop(color, $foreground-color);
78104
}
79105

@@ -96,9 +122,29 @@
96122
}
97123

98124
@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);
125+
.mat-mdc-tab, .mat-mdc-tab-link {
126+
// TODO(crisbeto): these styles should actually be set through the `theme` mixin while the
127+
// `theme-styles` are included in the `tab` mixin inside `_tabs-common.scss`. Currently
128+
// they are not, because `theme-styles` outputs the token values directly, rather than
129+
// generating CSS variables.
130+
@include mdc-tab-theme.theme-styles((
131+
active-focus-state-layer-color: $color,
132+
active-hover-state-layer-color: $color,
133+
active-pressed-state-layer-color: $color,
134+
with-icon-active-focus-icon-color: $color,
135+
with-icon-active-hover-icon-color: $color,
136+
with-icon-active-icon-color: $color,
137+
with-icon-active-pressed-icon-color: $color,
138+
with-label-text-active-focus-label-text-color: $color,
139+
with-label-text-active-hover-label-text-color: $color,
140+
with-label-text-active-label-text-color: $color,
141+
with-label-text-active-pressed-label-text-color: $color,
142+
));
143+
144+
@include mdc-tab-indicator-theme.theme-styles((
145+
active-indicator-color: $color
146+
));
147+
}
102148

103149
.mdc-tab__ripple::before,
104150
.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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +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);
63
@include tabs-common.paginated-tab-header;
74

85
.mat-mdc-tab-label-container {

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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@use '../tabs-common';
22

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

56
.mat-mdc-tab-links {

0 commit comments

Comments
 (0)