Skip to content

Commit b6ce8cc

Browse files
authored
refactor(material/expansion): switch to tokens API (#27390)
Reworks the expansion panel to use the tokens theming API.
1 parent 7c8a796 commit b6ce8cc

9 files changed

+203
-101
lines changed

src/material/core/tokens/_token-utils.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ $placeholder-typography-config: (
3939
caption: $_placeholder-typography-level-config,
4040
button: $_placeholder-typography-level-config,
4141
overline: $_placeholder-typography-level-config,
42+
subheading-1: $_placeholder-typography-level-config,
4243
);
4344

4445
// Placeholder density config that can be passed to token getter functions when generating token
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
@use 'sass:map';
2+
@use '../../token-utils';
3+
@use '../../../theming/theming';
4+
@use '../../../style/sass-utils';
5+
@use '../../../typography/typography-utils';
6+
7+
// The prefix used to generate the fully qualified name for tokens in this file.
8+
$prefix: (mat, expansion);
9+
10+
// Tokens that can't be configured through Angular Material's current theming API,
11+
// but may be in a future version of the theming API.
12+
@function get-unthemable-tokens() {
13+
@return (
14+
container-shape: 4px,
15+
);
16+
}
17+
18+
// Tokens that can be configured through Angular Material's color theming API.
19+
@function get-color-tokens($config) {
20+
$foreground: map.get($config, foreground);
21+
$background: map.get($config, background);
22+
23+
@return (
24+
container-background-color: theming.get-color-from-palette($background, card),
25+
container-text-color: theming.get-color-from-palette($foreground, text),
26+
actions-divider-color: theming.get-color-from-palette($foreground, divider),
27+
header-hover-state-layer-color: theming.get-color-from-palette($background, hover),
28+
header-focus-state-layer-color: theming.get-color-from-palette($background, hover),
29+
header-disabled-state-text-color: theming.get-color-from-palette($foreground, disabled-button),
30+
header-text-color: theming.get-color-from-palette($foreground, text),
31+
header-description-color: theming.get-color-from-palette($foreground, secondary-text),
32+
header-indicator-color: theming.get-color-from-palette($foreground, secondary-text),
33+
);
34+
}
35+
36+
// Tokens that can be configured through Angular Material's typography theming API.
37+
@function get-typography-tokens($config) {
38+
$fallback-font-family: typography-utils.font-family($config);
39+
40+
@return (
41+
header-text-font: typography-utils.font-family($config, subheading-1) or $fallback-font-family,
42+
header-text-size: typography-utils.font-size($config, subheading-1),
43+
header-text-weight: typography-utils.font-weight($config, subheading-1),
44+
45+
// TODO(crisbeto): these two properties weren't set at all before the introduction of tokens,
46+
// but it's inconsistent not to provide them since the container sets all of them. Eventually
47+
// we should change the values to use come from `subheading-1`.
48+
header-text-line-height: inherit,
49+
header-text-tracking: inherit,
50+
51+
container-text-font: typography-utils.font-family($config, body-1) or $fallback-font-family,
52+
container-text-line-height: typography-utils.line-height($config, body-1),
53+
container-text-size: typography-utils.font-size($config, body-1),
54+
container-text-tracking: typography-utils.letter-spacing($config, body-1),
55+
container-text-weight: typography-utils.font-weight($config, body-1),
56+
);
57+
}
58+
59+
// Tokens that can be configured through Angular Material's density theming API.
60+
@function get-density-tokens($config) {
61+
$scale: theming.clamp-density($config, -3);
62+
$collapsed-scale: (
63+
0: 48px,
64+
-1: 44px,
65+
-2: 40px,
66+
-3: 36px,
67+
);
68+
$expanded-scale: (
69+
0: 64px,
70+
-1: 60px,
71+
-2: 56px,
72+
-3: 48px,
73+
);
74+
75+
@return (
76+
header-collapsed-state-height: map.get($collapsed-scale, $scale),
77+
header-expanded-state-height: map.get($expanded-scale, $scale),
78+
);
79+
}
80+
81+
// Combines the tokens generated by the above functions into a single map with placeholder values.
82+
// This is used to create token slots.
83+
@function get-token-slots() {
84+
@return sass-utils.deep-merge-all(
85+
get-unthemable-tokens(),
86+
get-color-tokens(token-utils.$placeholder-color-config),
87+
get-typography-tokens(token-utils.$placeholder-typography-config),
88+
get-density-tokens(token-utils.$placeholder-density-config)
89+
);
90+
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
@forward 'expansion-variables' as mat-expansion-panel-*;
2-
@forward 'expansion-mixins' as mat-*;
32
@forward 'expansion-theme' as mat-expansion-panel-*;

src/material/expansion/_expansion-mixins.import.scss

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/material/expansion/_expansion-mixins.scss

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/material/expansion/_expansion-theme.import.scss

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
@forward '../core/style/private.import';
33
@forward 'expansion-variables' as mat-expansion-panel-*;
44
@forward '../core/typography/typography-utils.import';
5-
@forward 'expansion-mixins' as mat-*;
65
@forward 'expansion-theme' as mat-expansion-panel-*;
76

87
@import '../core/density/private/compatibility';
@@ -11,4 +10,3 @@
1110
@import '../core/style/private';
1211
@import '../core/typography/typography-utils';
1312
@import './expansion-variables';
14-
@import './expansion-mixins';

src/material/expansion/_expansion-theme.scss

Lines changed: 12 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,34 @@
1-
@use 'sass:map';
2-
@use '../core/density/private/compatibility';
31
@use '../core/theming/theming';
4-
@use '../core/style/private';
52
@use '../core/typography/typography';
6-
@use '../core/typography/typography-utils';
7-
@use './expansion-variables';
8-
@use './expansion-mixins';
3+
@use '../core/style/sass-utils';
4+
@use '../core/tokens/token-utils';
5+
@use '../core/tokens/m2/mat/expansion' as tokens-mat-expansion;
96

107
@mixin color($config-or-theme) {
118
$config: theming.get-color-config($config-or-theme);
12-
$background: map.get($config, background);
13-
$foreground: map.get($config, foreground);
149

15-
.mat-expansion-panel {
16-
@include private.private-theme-overridable-elevation(2, $config);
17-
background: theming.get-color-from-palette($background, card);
18-
color: theming.get-color-from-palette($foreground, text);
19-
}
20-
21-
.mat-action-row {
22-
border-top-color: theming.get-color-from-palette($foreground, divider);
23-
}
24-
25-
@include expansion-mixins.private-expansion-focus {
26-
background: theming.get-color-from-palette($background, hover);
27-
}
28-
29-
// Disable the hover on touch devices since it can appear like it is stuck. We can't use
30-
// `@media (hover)` above, because the desktop support browser support isn't great.
31-
@media (hover: none) {
32-
.mat-expansion-panel:not(.mat-expanded):not([aria-disabled='true'])
33-
.mat-expansion-panel-header:hover {
34-
background: theming.get-color-from-palette($background, card);
35-
}
36-
}
37-
38-
.mat-expansion-panel-header-title {
39-
color: theming.get-color-from-palette($foreground, text);
40-
}
41-
42-
.mat-expansion-panel-header-description,
43-
.mat-expansion-indicator::after {
44-
color: theming.get-color-from-palette($foreground, secondary-text);
45-
}
46-
47-
.mat-expansion-panel-header[aria-disabled='true'] {
48-
color: theming.get-color-from-palette($foreground, disabled-button);
49-
50-
.mat-expansion-panel-header-title,
51-
.mat-expansion-panel-header-description {
52-
color: inherit;
53-
}
10+
@include sass-utils.current-selector-or-root() {
11+
@include token-utils.create-token-values(tokens-mat-expansion.$prefix,
12+
tokens-mat-expansion.get-color-tokens($config));
5413
}
5514
}
5615

5716
@mixin typography($config-or-theme) {
5817
$config: typography.private-typography-to-2014-config(
5918
theming.get-typography-config($config-or-theme));
60-
.mat-expansion-panel-header {
61-
font: {
62-
family: typography-utils.font-family($config, subheading-1);
63-
size: typography-utils.font-size($config, subheading-1);
64-
weight: typography-utils.font-weight($config, subheading-1);
65-
}
66-
}
6719

68-
.mat-expansion-panel-content {
69-
@include typography-utils.typography-level($config, body-1);
20+
@include sass-utils.current-selector-or-root() {
21+
@include token-utils.create-token-values(tokens-mat-expansion.$prefix,
22+
tokens-mat-expansion.get-typography-tokens($config));
7023
}
7124
}
7225

7326
@mixin density($config-or-theme) {
7427
$density-scale: theming.get-density-config($config-or-theme);
75-
$expanded-height: compatibility.private-density-prop-value(
76-
expansion-variables.$header-density-config, $density-scale, expanded-height);
77-
$collapsed-height: compatibility.private-density-prop-value(
78-
expansion-variables.$header-density-config, $density-scale, collapsed-height);
7928

80-
@include compatibility.private-density-legacy-compatibility() {
81-
.mat-expansion-panel-header {
82-
height: $collapsed-height;
83-
84-
&.mat-expanded {
85-
height: $expanded-height;
86-
}
87-
}
29+
@include sass-utils.current-selector-or-root() {
30+
@include token-utils.create-token-values(tokens-mat-expansion.$prefix,
31+
tokens-mat-expansion.get-density-tokens($density-scale));
8832
}
8933
}
9034

src/material/expansion/expansion-panel-header.scss

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
@use '@angular/cdk';
2+
@use '../core/tokens/m2/mat/expansion' as tokens-mat-expansion;
3+
@use '../core/tokens/token-utils';
24
@use './expansion-variables';
35

46
.mat-expansion-panel-header {
@@ -9,6 +11,44 @@
911
border-radius: inherit;
1012
transition: height expansion-variables.$header-transition;
1113

14+
@include token-utils.use-tokens(
15+
tokens-mat-expansion.$prefix, tokens-mat-expansion.get-token-slots()) {
16+
@include token-utils.create-token-slot(height, header-collapsed-state-height);
17+
@include token-utils.create-token-slot(font-family, header-text-font);
18+
@include token-utils.create-token-slot(font-size, header-text-size);
19+
@include token-utils.create-token-slot(font-weight, header-text-weight);
20+
@include token-utils.create-token-slot(line-height, header-text-line-height);
21+
@include token-utils.create-token-slot(letter-spacing, header-text-tracking);
22+
23+
&.mat-expanded {
24+
@include token-utils.create-token-slot(height, header-expanded-state-height);
25+
}
26+
27+
&[aria-disabled='true'] {
28+
@include token-utils.create-token-slot(color, header-disabled-state-text-color);
29+
}
30+
31+
&:not([aria-disabled='true']) {
32+
cursor: pointer;
33+
34+
.mat-expansion-panel:not(.mat-expanded) &:hover {
35+
@include token-utils.create-token-slot(background, header-hover-state-layer-color);
36+
37+
// Disable the hover on touch devices since it can appear like it is stuck. We can't use
38+
// `@media (hover)` above, because the desktop support browser support isn't great.
39+
@media (hover: none) {
40+
@include token-utils.create-token-slot(background, container-background-color);
41+
}
42+
}
43+
44+
// The `.mat-expansion-panel` here is redundant, but we need the additional specificity.
45+
.mat-expansion-panel &.cdk-keyboard-focused,
46+
.mat-expansion-panel &.cdk-program-focused {
47+
@include token-utils.create-token-slot(background, header-focus-state-layer-color);
48+
}
49+
}
50+
}
51+
1252
// If the `NoopAnimationsModule` is used, disable the height transition.
1353
&._mat-animation-noopable {
1454
transition: none;
@@ -24,10 +64,6 @@
2464
background: inherit;
2565
}
2666

27-
&:not([aria-disabled='true']) {
28-
cursor: pointer;
29-
}
30-
3167
&.mat-expansion-toggle-indicator-before {
3268
flex-direction: row-reverse;
3369

@@ -68,6 +104,13 @@
68104
}
69105
}
70106

107+
.mat-expansion-panel-header-title {
108+
@include token-utils.use-tokens(
109+
tokens-mat-expansion.$prefix, tokens-mat-expansion.get-token-slots()) {
110+
@include token-utils.create-token-slot(color, header-text-color);
111+
}
112+
}
113+
71114
.mat-expansion-panel-header-title,
72115
.mat-expansion-panel-header-description {
73116
display: flex;
@@ -80,10 +123,19 @@
80123
margin-right: 0;
81124
margin-left: 16px;
82125
}
126+
127+
.mat-expansion-panel-header[aria-disabled='true'] & {
128+
color: inherit;
129+
}
83130
}
84131

85132
.mat-expansion-panel-header-description {
86133
flex-grow: 2;
134+
135+
@include token-utils.use-tokens(
136+
tokens-mat-expansion.$prefix, tokens-mat-expansion.get-token-slots()) {
137+
@include token-utils.create-token-slot(color, header-description-color);
138+
}
87139
}
88140

89141
// Creates the expansion indicator arrow. Done using ::after
@@ -96,6 +148,11 @@
96148
padding: 3px;
97149
transform: rotate(45deg);
98150
vertical-align: middle;
151+
152+
@include token-utils.use-tokens(
153+
tokens-mat-expansion.$prefix, tokens-mat-expansion.get-token-slots()) {
154+
@include token-utils.create-token-slot(color, header-indicator-color);
155+
}
99156
}
100157

101158
@include cdk.high-contrast(active, off) {

0 commit comments

Comments
 (0)