Skip to content

Commit 92c701f

Browse files
authored
refactor(material/tree): switch to tokens theming API (#27980)
Reworks the tree to use the new tokens theming API.
1 parent 3430c4d commit 92c701f

File tree

5 files changed

+101
-40
lines changed

5 files changed

+101
-40
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
@use 'sass:map';
2+
@use '../../token-utils';
3+
@use '../../../theming/theming';
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, tree);
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+
$foreground-base: inspection.get-theme-color($theme, foreground, base);
19+
20+
@return (
21+
container-background-color: inspection.get-theme-color($theme, background, card),
22+
node-text-color: inspection.get-theme-color($theme, foreground, text),
23+
);
24+
}
25+
26+
// Tokens that can be configured through Angular Material's typography theming API.
27+
@function get-typography-tokens($theme) {
28+
@return (
29+
node-text-font: inspection.get-theme-typography($theme, body-2, font-family),
30+
node-text-size: inspection.get-theme-typography($theme, body-2, font-size),
31+
node-text-weight: inspection.get-theme-typography($theme, body-2, font-weight),
32+
33+
// TODO(crisbeto): provide tokens for line height and letter spacing to match other components.
34+
);
35+
}
36+
37+
// Tokens that can be configured through Angular Material's density theming API.
38+
@function get-density-tokens($theme) {
39+
$density-scale: theming.clamp-density(inspection.get-theme-density($theme), -4);
40+
$min-height-scale: (
41+
0: 48px,
42+
-1: 44px,
43+
-2: 40px,
44+
-3: 36px,
45+
-4: 28px,
46+
);
47+
48+
@return (
49+
node-min-height: map.get($min-height-scale, $density-scale)
50+
);
51+
}
52+
53+
// Combines the tokens generated by the above functions into a single map with placeholder values.
54+
// This is used to create token slots.
55+
@function get-token-slots() {
56+
@return sass-utils.deep-merge-all(
57+
get-unthemable-tokens(),
58+
get-color-tokens(token-utils.$placeholder-color-config),
59+
get-typography-tokens(token-utils.$placeholder-typography-config),
60+
get-density-tokens(token-utils.$placeholder-density-config)
61+
);
62+
}

src/material/tree/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ sass_library(
3737
sass_binary(
3838
name = "tree_scss",
3939
src = "tree.scss",
40+
deps = [
41+
":tree_scss_lib",
42+
],
4043
)
4144

4245
ng_test_library(

src/material/tree/_tree-theme.scss

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,35 @@
1-
@use '../core/density/private/compatibility';
21
@use '../core/theming/theming';
32
@use '../core/theming/inspection';
43
@use '../core/typography/typography';
5-
@use './tree-variables';
4+
@use '../core/style/sass-utils';
5+
@use '../core/tokens/token-utils';
6+
@use '../core/tokens/m2/mat/tree' as tokens-mat-tree;
67

7-
@mixin base($theme) {
8-
// TODO(mmalerba): Move tree base tokens here
9-
}
8+
@mixin base($theme) {}
109

1110
@mixin color($theme) {
12-
.mat-tree {
13-
background: inspection.get-theme-color($theme, background, card);
14-
}
15-
16-
.mat-tree-node,
17-
.mat-nested-tree-node {
18-
color: inspection.get-theme-color($theme, foreground, text);
11+
@include sass-utils.current-selector-or-root() {
12+
@include token-utils.create-token-values(tokens-mat-tree.$prefix,
13+
tokens-mat-tree.get-color-tokens($theme));
1914
}
2015
}
2116

2217
@mixin typography($theme) {
2318
// TODO(mmalerba): Stop calling this and resolve resulting screen diffs.
2419
$theme: inspection.private-get-typography-back-compat-theme($theme);
2520

26-
.mat-tree {
27-
font-family: inspection.get-theme-typography($theme, body-2, font-family);
28-
}
29-
30-
.mat-tree-node,
31-
.mat-nested-tree-node {
32-
font-weight: inspection.get-theme-typography($theme, body-2, font-weight);
33-
font-size: inspection.get-theme-typography($theme, body-2, font-size);
21+
@include sass-utils.current-selector-or-root() {
22+
@include token-utils.create-token-values(tokens-mat-tree.$prefix,
23+
tokens-mat-tree.get-typography-tokens($theme));
3424
}
3525
}
3626

3727
@mixin density($theme) {
3828
$density-scale: inspection.get-theme-density($theme);
39-
$height: compatibility.private-density-prop-value(tree-variables.$density-config,
40-
$density-scale, height);
4129

42-
@include compatibility.private-density-legacy-compatibility() {
43-
.mat-tree-node {
44-
min-height: $height;
45-
}
30+
@include sass-utils.current-selector-or-root() {
31+
@include token-utils.create-token-values(tokens-mat-tree.$prefix,
32+
tokens-mat-tree.get-density-tokens($theme));
4633
}
4734
}
4835

src/material/tree/_tree-variables.scss

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

src/material/tree/tree.scss

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
1+
@use '../core/tokens/m2/mat/tree' as tokens-mat-tree;
2+
@use '../core/tokens/token-utils';
3+
14
.mat-tree {
25
display: block;
6+
7+
@include token-utils.use-tokens(tokens-mat-tree.$prefix, tokens-mat-tree.get-token-slots()) {
8+
@include token-utils.create-token-slot(background-color, container-background-color);
9+
}
10+
}
11+
12+
.mat-tree-node,
13+
.mat-nested-tree-node {
14+
@include token-utils.use-tokens(tokens-mat-tree.$prefix, tokens-mat-tree.get-token-slots()) {
15+
@include token-utils.create-token-slot(color, node-text-color);
16+
@include token-utils.create-token-slot(font-family, node-text-font);
17+
@include token-utils.create-token-slot(font-size, node-text-size);
18+
@include token-utils.create-token-slot(font-weight, node-text-weight);
19+
}
320
}
421

522
.mat-tree-node {
623
display: flex;
724
align-items: center;
825
flex: 1;
926
word-wrap: break-word;
27+
28+
@include token-utils.use-tokens(tokens-mat-tree.$prefix, tokens-mat-tree.get-token-slots()) {
29+
// TODO: before tokens were introduced, the `min-height` only applied to the
30+
// non-nested tree node. Should it apply to the nested one as well?
31+
@include token-utils.create-token-slot(min-height, node-min-height);
32+
}
1033
}
1134

1235
.mat-nested-tree-node {

0 commit comments

Comments
 (0)