Skip to content

Commit e9625cc

Browse files
committed
refactor(material/sort): switch to token theming API (#27963)
Reworks the sort header to use the new tokens theming API. (cherry picked from commit f2ad334)
1 parent defd48d commit e9625cc

File tree

3 files changed

+87
-23
lines changed

3 files changed

+87
-23
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
@use 'sass:color';
2+
@use 'sass:meta';
3+
@use '../../token-utils';
4+
@use '../../../theming/inspection';
5+
@use '../../../style/sass-utils';
6+
7+
// The prefix used to generate the fully qualified name for tokens in this file.
8+
$prefix: (mat, sort);
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+
}
15+
16+
// Tokens that can be configured through Angular Material's color theming API.
17+
@function get-color-tokens($theme) {
18+
$table-background: inspection.get-theme-color($theme, background, card);
19+
$text-color: inspection.get-theme-color($theme, foreground, secondary-text);
20+
$arrow-color: null;
21+
22+
// Because the arrow is made up of multiple elements that are stacked on top of each other,
23+
// we can't use the semi-transparent color from the theme directly. If the value is a color
24+
// *type*, we convert it into a solid color by taking the opacity from the rgba value and
25+
// using the value to determine the percentage of the background to put into foreground
26+
// when mixing the colors together. Otherwise, if it resolves to something different
27+
// (e.g. it resolves to a CSS variable), we use the color directly.
28+
@if (meta.type-of($table-background) == color and meta.type-of($text-color) == color) {
29+
$text-opacity: opacity($text-color);
30+
$arrow-color: color.mix($table-background, rgba($text-color, 1), (1 - $text-opacity) * 100%);
31+
}
32+
@else {
33+
$arrow-color: $text-color;
34+
}
35+
36+
@return (
37+
arrow-color: $arrow-color,
38+
);
39+
}
40+
41+
// Tokens that can be configured through Angular Material's typography theming API.
42+
@function get-typography-tokens($theme) {
43+
@return ();
44+
}
45+
46+
// Tokens that can be configured through Angular Material's density theming API.
47+
@function get-density-tokens($theme) {
48+
@return ();
49+
}
50+
51+
// Combines the tokens generated by the above functions into a single map with placeholder values.
52+
// This is used to create token slots.
53+
@function get-token-slots() {
54+
@return sass-utils.deep-merge-all(
55+
get-unthemable-tokens(),
56+
get-color-tokens(token-utils.$placeholder-color-config),
57+
get-typography-tokens(token-utils.$placeholder-typography-config),
58+
get-density-tokens(token-utils.$placeholder-density-config)
59+
);
60+
}

src/material/sort/_sort-theme.scss

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
1-
@use 'sass:color';
2-
@use 'sass:meta';
1+
@use '../core/tokens/m2/mat/sort' as tokens-mat-sort;
2+
@use '../core/style/sass-utils';
3+
@use '../core/typography/typography';
34
@use '../core/theming/theming';
45
@use '../core/theming/inspection';
6+
@use '../core/tokens/token-utils';
57

6-
@mixin base($theme) {
7-
// TODO(mmalerba): Move sort header base tokens here
8-
}
8+
@mixin base($theme) {}
99

1010
@mixin color($theme) {
11-
.mat-sort-header-arrow {
12-
$table-background: inspection.get-theme-color($theme, background, card);
13-
$text-color: inspection.get-theme-color($theme, foreground, secondary-text);
11+
@include sass-utils.current-selector-or-root() {
12+
@include token-utils.create-token-values(tokens-mat-sort.$prefix,
13+
tokens-mat-sort.get-color-tokens($theme));
14+
}
15+
}
1416

15-
// Because the arrow is made up of multiple elements that are stacked on top of each other,
16-
// we can't use the semi-transparent color from the theme directly. If the value is a color
17-
// *type*, we convert it into a solid color by taking the opacity from the rgba value and
18-
// using the value to determine the percentage of the background to put into foreground
19-
// when mixing the colors together. Otherwise, if it resolves to something different
20-
// (e.g. it resolves to a CSS variable), we use the color directly.
21-
@if (meta.type-of($table-background) == color and meta.type-of($text-color) == color) {
22-
$text-opacity: opacity($text-color);
23-
color: color.mix($table-background, rgba($text-color, 1), (1 - $text-opacity) * 100%);
24-
}
25-
@else {
26-
color: $text-color;
27-
}
17+
@mixin typography($theme) {
18+
@include sass-utils.current-selector-or-root() {
19+
@include token-utils.create-token-values(tokens-mat-sort.$prefix,
20+
tokens-mat-sort.get-typography-tokens($theme));
2821
}
2922
}
3023

31-
@mixin typography($theme) {}
24+
@mixin density($theme) {
25+
$density-scale: inspection.get-theme-density($theme);
3226

33-
@mixin density($theme) {}
27+
@include sass-utils.current-selector-or-root() {
28+
@include token-utils.create-token-values(tokens-mat-sort.$prefix,
29+
tokens-mat-sort.get-density-tokens($theme));
30+
}
31+
}
3432

3533
@mixin theme($theme) {
3634
@include theming.private-check-duplicate-theme-styles($theme, 'mat-sort') {

src/material/sort/sort-header.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
@use '@angular/cdk';
22

3+
@use '../core/tokens/m2/mat/sort' as tokens-mat-sort;
4+
@use '../core/tokens/token-utils';
35
@use '../core/focus-indicators/private';
46

57
$header-arrow-margin: 6px;
@@ -56,6 +58,10 @@ $header-arrow-hint-opacity: 0.38;
5658
position: relative;
5759
display: flex;
5860

61+
@include token-utils.use-tokens(tokens-mat-sort.$prefix, tokens-mat-sort.get-token-slots()) {
62+
@include token-utils.create-token-slot(color, arrow-color);
63+
}
64+
5965
// Start off at 0 since the arrow may become visible while parent are animating.
6066
// This will be overwritten when the arrow animations kick in. See #11819.
6167
opacity: 0;

0 commit comments

Comments
 (0)