Skip to content

Commit f9cc932

Browse files
authored
refactor(material/core): simplify token utils (angular#29430)
Reworks the logic for generating tokens so it doesn't have external dependencies.
1 parent 5655661 commit f9cc932

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

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

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
@use 'sass:list';
22
@use 'sass:map';
33
@use 'sass:meta';
4-
@use '@material/theme/custom-properties' as mdc-custom-properties;
5-
@use '@material/theme/theme' as mdc-theme;
6-
@use '@material/theme/keys' as mdc-keys;
4+
@use 'sass:string';
75
@use '@material/tokens/v0_161' as mdc-tokens;
86
@use '../style/elevation';
97
@use '../style/sass-utils';
@@ -57,27 +55,33 @@ $placeholder-density-config: 0;
5755
$_tokens: null;
5856
$_component-prefix: null;
5957

60-
@mixin _configure-token-prefix($first, $rest...) {
61-
$_component-prefix: '' !global;
62-
@each $item in $rest {
63-
$_component-prefix:
64-
if($_component-prefix == '', $item, '#{$_component-prefix}-#{$item}') !global;
65-
}
66-
@include mdc-custom-properties.configure($varname-prefix: $first) {
67-
@content;
68-
}
69-
$_component-prefix: null !global;
70-
}
71-
7258
// Sets the token prefix and map to use when creating token slots.
7359
@mixin use-tokens($prefix, $tokens) {
60+
$_component-prefix: $prefix !global;
7461
$_tokens: $tokens !global;
75-
@include _configure-token-prefix($prefix...) {
76-
@content;
77-
}
62+
63+
@content;
64+
65+
$_component-prefix: null !global;
7866
$_tokens: null !global;
7967
}
8068

69+
// Combines a prefix and a string to generate a CSS variable name for a token.
70+
@function _get-css-variable($prefix, $name) {
71+
@if $prefix == null or $name == null {
72+
@error 'Must specify both prefix and name when generating token';
73+
}
74+
75+
$string-prefix: '';
76+
77+
// Prefixes are lists so we need to combine them.
78+
@each $part in $prefix {
79+
$string-prefix: if($string-prefix == '', $part, '#{$string-prefix}-#{$part}');
80+
}
81+
82+
@return string.unquote('--#{$string-prefix}-#{$name}');
83+
}
84+
8185
// Emits a slot for the given token, provided that it has a non-null value in the token map passed
8286
// to `use-tokens`.
8387
@mixin create-token-slot($property, $token, $emit-fallback: false) {
@@ -97,8 +101,9 @@ $_component-prefix: null;
97101
$fallback: $emit-fallback;
98102
}
99103

100-
$value: mdc-custom-properties.create('#{$_component-prefix}-#{$token}', $fallback: $fallback);
101-
@include mdc-theme.property($property, $value);
104+
$var-name: _get-css-variable($_component-prefix, $token);
105+
$var-reference: if($fallback == null, var(#{$var-name}), var(#{$var-name}, #{$fallback}));
106+
#{$property}: #{$var-reference};
102107
}
103108
}
104109

@@ -112,7 +117,7 @@ $_component-prefix: null;
112117
@error 'Token #{$token} does not exist. Configured tokens are: #{map.keys($_tokens)}';
113118
}
114119

115-
@return mdc-custom-properties.create-varname('#{$_component-prefix}-#{$token}');
120+
@return _get-css-variable($_component-prefix, $token);
116121
}
117122

118123
// TODO(crisbeto): should be able to replace the usages of `get-token-variable` with this.
@@ -137,9 +142,14 @@ $_component-prefix: null;
137142
}
138143
}
139144

145+
// Outputs a map of tokens under a specific prefix.
140146
@mixin create-token-values($prefix, $tokens) {
141-
@include _configure-token-prefix($prefix...) {
142-
@include mdc-keys.declare-custom-properties($tokens, $_component-prefix);
147+
@if $tokens != null {
148+
@each $key, $value in $tokens {
149+
@if $value != null {
150+
#{_get-css-variable($prefix, $key)}: #{$value};
151+
}
152+
}
143153
}
144154
}
145155

src/material/core/tokens/m2/mdc/_switch.scss

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ $prefix: (mdc, switch);
8989
disabled-unselected-icon-color: $icon-color,
9090
// Color of track when disabled.
9191
disabled-unselected-track-color: $on-surface,
92+
// TODO(crisbeto): `handle-surface-color` was hardcoded to `var(--mdc-surface-color, #fff)`
93+
// which made it basically hardcoded to #fff. Should it be based on the theme?
9294
// Color of slide-toggle handle's surface.
93-
handle-surface-color: surface,
95+
handle-surface-color: #fff,
9496
// Color of icon (ex. checkmark) when selected
9597
selected-icon-color: $icon-color,
9698
// Color of handle when unselected and focused.

0 commit comments

Comments
 (0)