Skip to content

Commit 635bc6c

Browse files
committed
refactor(material/core): add utility to generate a variable reference
Adds a utility that generates a `var()` reference to a token. This is necessary for some more complex declarations like `padding` where `create-token-slot` isn't enough.
1 parent 6c49372 commit 635bc6c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,28 @@ $_component-prefix: null;
110110
@return mdc-custom-properties.create-varname('#{$_component-prefix}-#{$token}');
111111
}
112112

113+
// TODO(crisbeto): should be able to replace the usages of `get-token-variable` with this.
114+
// Returns a `var()` reference to a specific token. Intended for declarations
115+
// where the token has to be referenced as a part of a larger expression.
116+
@function get-token-variable-reference($token, $emit-fallback: false) {
117+
@if $_component-prefix == null or $_tokens == null {
118+
@error '`get-token-variable-reference` must be used within `use-tokens`';
119+
}
120+
@if not map.has-key($_tokens, $token) {
121+
@error 'Token #{$token} does not exist. Configured tokens are: #{map.keys($_tokens)}';
122+
}
123+
124+
$var: get-token-variable($token);
125+
$fallback: if($emit-fallback, map.get($_tokens, $token), null);
126+
127+
@if ($fallback != null) {
128+
@return var($var, $fallback);
129+
}
130+
@else {
131+
@return var($var);
132+
}
133+
}
134+
113135
@mixin create-token-values($prefix, $tokens) {
114136
@include _configure-token-prefix($prefix...) {
115137
@include mdc-keys.declare-custom-properties($tokens, $_component-prefix);

0 commit comments

Comments
 (0)