Skip to content

Commit 1e8a4f4

Browse files
authored
refactor(material/tabs): introduce new tokens (#26901)
Adds new tokens to the tabs to make it easier to implement the gmat variant.
1 parent cb29697 commit 1e8a4f4

File tree

4 files changed

+71
-29
lines changed

4 files changed

+71
-29
lines changed

src/material/core/tokens/m2/mat/_tab-header.scss

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,27 @@ $prefix: (mat, tab-header);
1717
$is-dark: map.get($config, is-dark);
1818
$foreground: map.get($config, foreground);
1919
$primary: map.get($config, primary);
20+
$inactive-label-text-color: rgba(if($is-dark, #fff, #000), 0.6);
21+
$active-label-text-color: theming.get-color-from-palette($primary, default);
22+
$ripple-color: theming.get-color-from-palette($primary, default);
2023

2124
@return (
22-
ripple-color: theming.get-color-from-palette($primary, default),
2325
disabled-ripple-color: theming.get-color-from-palette($foreground, disabled),
2426
pagination-icon-color: if($is-dark, #fff, #000),
2527

2628
// Note: MDC has equivalents of these tokens, but they lead to much higher selector specificity.
27-
inactive-label-text-color: rgba(if($is-dark, #fff, #000), 0.6),
28-
active-label-text-color: theming.get-color-from-palette($primary, default),
29+
inactive-label-text-color: $inactive-label-text-color,
30+
active-label-text-color: $active-label-text-color,
31+
32+
// Tokens needed to implement the gmat styles. Externally they don't change.
33+
active-ripple-color: $ripple-color,
34+
inactive-ripple-color: $ripple-color,
35+
inactive-focus-label-text-color: $inactive-label-text-color,
36+
inactive-hover-label-text-color: $inactive-label-text-color,
37+
active-focus-label-text-color: $active-label-text-color,
38+
active-hover-label-text-color: $active-label-text-color,
39+
active-focus-indicator-color: $active-label-text-color,
40+
active-hover-indicator-color: $active-label-text-color,
2941
);
3042
}
3143

src/material/core/tokens/m2/mdc/_tab-indicator.scss

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ $prefix: (mdc, tab-indicator);
1515
@return (
1616
active-indicator-height: 2px,
1717

18-
// =============================================================================================
19-
// = TOKENS NOT USED IN ANGULAR MATERIAL =
20-
// =============================================================================================
21-
active-indicator-shape: null,
18+
// Currently set to zero, but used by the gmat styles to make the indicator rounded.
19+
active-indicator-shape: 0,
2220
);
2321
}
2422

src/material/tabs/_tabs-common.scss

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,17 @@ $mat-tab-animation-duration: 500ms !default;
4646
}
4747

4848
-webkit-tap-highlight-color: transparent;
49+
-webkit-font-smoothing: antialiased;
50+
-moz-osx-font-smoothing: grayscale;
4951
text-decoration: none;
5052

53+
&.mdc-tab {
54+
// MDC's tabs stretch to fit the header by default, whereas stretching on our current ones
55+
// is an opt-in behavior. Also technically we don't need to combine the two classes, but
56+
// we need the extra specificity to avoid issues with CSS insertion order.
57+
flex-grow: 0;
58+
}
59+
5160
@include token-utils.use-tokens(
5261
tokens-mat-tab-header.$prefix,
5362
tokens-mat-tab-header.get-token-slots()
@@ -57,28 +66,59 @@ $mat-tab-animation-duration: 500ms !default;
5766
@include token-utils.create-token-slot(letter-spacing, label-text-letter-spacing);
5867
@include token-utils.create-token-slot(line-height, label-text-line-height);
5968
@include token-utils.create-token-slot(font-weight, label-text-weight);
60-
}
6169

62-
&.mdc-tab {
63-
// MDC's tabs stretch to fit the header by default, whereas stretching on our current ones
64-
// is an opt-in behavior. Also technically we don't need to combine the two classes, but
65-
// we need the extra specificity to avoid issues with CSS insertion order.
66-
flex-grow: 0;
70+
&:hover .mdc-tab__text-label {
71+
@include token-utils.create-token-slot(color, inactive-hover-label-text-color);
72+
}
6773

68-
&:not(.mat-mdc-tab-disabled).mdc-tab--active .mdc-tab__text-label {
69-
@include token-utils.use-tokens(
70-
tokens-mat-tab-header.$prefix,
71-
tokens-mat-tab-header.get-token-slots()
72-
) {
74+
&:focus .mdc-tab__text-label {
75+
@include token-utils.create-token-slot(color, inactive-focus-label-text-color);
76+
}
77+
78+
&.mdc-tab--active {
79+
.mdc-tab__text-label {
7380
@include token-utils.create-token-slot(color, active-label-text-color);
7481
}
82+
83+
.mdc-tab__ripple::before,
84+
.mat-ripple-element {
85+
@include token-utils.create-token-slot(background-color, active-ripple-color);
86+
}
87+
88+
&:hover {
89+
.mdc-tab__text-label {
90+
@include token-utils.create-token-slot(color, active-hover-label-text-color);
91+
}
92+
93+
.mdc-tab-indicator__content--underline {
94+
@include token-utils.create-token-slot(border-color, active-hover-indicator-color);
95+
}
96+
}
97+
98+
&:focus {
99+
.mdc-tab__text-label {
100+
@include token-utils.create-token-slot(color, active-focus-label-text-color);
101+
}
102+
103+
.mdc-tab-indicator__content--underline {
104+
@include token-utils.create-token-slot(border-color, active-focus-indicator-color);
105+
}
106+
}
75107
}
76108
}
77109

78110
&.mat-mdc-tab-disabled {
79111
// MDC doesn't support disabled tabs so we need to improvise.
80112
opacity: 0.4;
81113

114+
// We use `pointer-events` to make the element unclickable when it's disabled, rather than
115+
// preventing the default action through JS, because we can't prevent the action reliably
116+
// due to other directives potentially registering their events earlier. This shouldn't cause
117+
// the user to click through, because we always have a header behind the tab. Furthermore, this
118+
// saves us some CSS, because we don't have to add `:not(.mat-mdc-tab-disabled)` to all the
119+
// hover and focus selectors.
120+
pointer-events: none;
121+
82122
@include token-utils.use-tokens(
83123
tokens-mat-tab-header.$prefix,
84124
tokens-mat-tab-header.get-token-slots()
@@ -107,7 +147,7 @@ $mat-tab-animation-duration: 500ms !default;
107147
tokens-mat-tab-header.$prefix,
108148
tokens-mat-tab-header.get-token-slots()
109149
) {
110-
@include token-utils.create-token-slot(background-color, ripple-color);
150+
@include token-utils.create-token-slot(background-color, inactive-ripple-color);
111151
}
112152
}
113153

@@ -154,7 +194,7 @@ $mat-tab-animation-duration: 500ms !default;
154194
tokens-mat-tab-header.$prefix,
155195
tokens-mat-tab-header.get-token-slots()
156196
) {
157-
@include token-utils.create-token-slot(background-color, ripple-color);
197+
@include token-utils.create-token-slot(background-color, inactive-ripple-color);
158198
}
159199
}
160200
}
@@ -202,7 +242,7 @@ $mat-tab-animation-duration: 500ms !default;
202242
tokens-mat-tab-header.$prefix,
203243
tokens-mat-tab-header.get-token-slots()
204244
) {
205-
@include token-utils.create-token-slot(background-color, ripple-color);
245+
@include token-utils.create-token-slot(background-color, inactive-ripple-color);
206246
}
207247
}
208248

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@
55
.mat-mdc-tab-link {
66
@include tabs-common.tab;
77

8-
&.mat-mdc-tab-disabled {
9-
// We use `pointer-events` to make the element unclickable when it's disabled, rather than
10-
// preventing the default action through JS, because we can't prevent the action reliably
11-
// due to other directives potentially registering their events earlier. This shouldn't cause
12-
// the user to click through, because we always have a `.mat-tab-links` behind the link.
13-
pointer-events: none;
14-
}
15-
168
// Note that we only want to target direct descendant tabs.
179
.mat-mdc-tab-header.mat-mdc-tab-nav-bar-stretch-tabs & {
1810
flex-grow: 1;

0 commit comments

Comments
 (0)