1
1
@use ' sass:list' ;
2
2
@use ' sass:map' ;
3
3
@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' ;
7
5
@use ' @material/tokens/v0_161' as mdc-tokens ;
8
6
@use ' ../style/elevation' ;
9
7
@use ' ../style/sass-utils' ;
@@ -57,27 +55,33 @@ $placeholder-density-config: 0;
57
55
$_tokens : null;
58
56
$_component-prefix : null;
59
57
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
-
72
58
// Sets the token prefix and map to use when creating token slots.
73
59
@mixin use-tokens ($prefix , $tokens ) {
60
+ $_component-prefix : $prefix !global;
74
61
$_tokens : $tokens !global;
75
- @include _configure-token-prefix ($prefix ...) {
76
- @content ;
77
- }
62
+
63
+ @content ;
64
+
65
+ $_component-prefix : null !global;
78
66
$_tokens : null !global;
79
67
}
80
68
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
+
81
85
// Emits a slot for the given token, provided that it has a non-null value in the token map passed
82
86
// to `use-tokens`.
83
87
@mixin create-token-slot ($property , $token , $emit-fallback : false) {
@@ -97,8 +101,9 @@ $_component-prefix: null;
97
101
$fallback : $emit-fallback ;
98
102
}
99
103
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 } ;
102
107
}
103
108
}
104
109
@@ -112,7 +117,7 @@ $_component-prefix: null;
112
117
@error ' Token #{$token } does not exist. Configured tokens are: #{map .keys ($_tokens )} ' ;
113
118
}
114
119
115
- @return mdc-custom-properties . create-varname ( ' #{ $_component-prefix } - #{ $token } ' );
120
+ @return _get-css-variable ( $_component-prefix , $token );
116
121
}
117
122
118
123
// TODO(crisbeto): should be able to replace the usages of `get-token-variable` with this.
@@ -137,9 +142,14 @@ $_component-prefix: null;
137
142
}
138
143
}
139
144
145
+ // Outputs a map of tokens under a specific prefix.
140
146
@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
+ }
143
153
}
144
154
}
145
155
0 commit comments