Skip to content

Commit 9b2e9b7

Browse files
authored
refactor(material/theming): make batch-create-token-values more flexible (#28897)
* Refactor token-utils.batch-create-token-values to allow us to pass in as many token maps as we want. This is change is relevant for components like the button which would want to pass in token maps for each button variant.
1 parent 4648f6b commit 9b2e9b7

File tree

2 files changed

+27
-43
lines changed

2 files changed

+27
-43
lines changed

src/material/checkbox/_checkbox-theme.scss

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,8 @@
9797
@mixin overrides($tokens: ()) {
9898
@include token-utils.batch-create-token-values(
9999
$tokens,
100-
$mat-prefix: tokens-mat-checkbox.$prefix,
101-
$mdc-prefix: tokens-mdc-checkbox.$prefix,
102-
$mat-tokens: tokens-mat-checkbox.get-token-slots(),
103-
$mdc-tokens: tokens-mdc-checkbox.get-token-slots(),
100+
(prefix: tokens-mat-checkbox.$prefix, tokens: tokens-mat-checkbox.get-token-slots()),
101+
(prefix: tokens-mdc-checkbox.$prefix, tokens: tokens-mdc-checkbox.get-token-slots()),
104102
);
105103
}
106104

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

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -214,58 +214,44 @@ $_component-prefix: null;
214214
@return if($emit-overrides-only, $overrides, map.merge($values, $overrides));
215215
}
216216

217-
/// Emits new token values for the given tokens.
218-
/// Verifies that the tokens passed in are valid tokens.
217+
/// Emits new token values for the given token overrides.
218+
/// Verifies that the overrides passed in are valid tokens.
219219
/// New token values are emitted under the current selector or root.
220-
@mixin batch-create-token-values(
221-
$tokens: (),
222-
$mat-prefix: '',
223-
$mdc-prefix: '',
224-
$mat-tokens: (),
225-
$mdc-tokens: ()
226-
) {
227-
$custom-mat-tokens: ();
228-
$custom-mdc-tokens: ();
220+
@mixin batch-create-token-values($overrides: (), $token-maps...) {
221+
@include _validate-token-overrides($overrides, $token-maps);
229222

230-
$mat-token-names: map.keys($mat-tokens);
231-
$mdc-token-names: map.keys($mdc-tokens);
232-
$valid-token-names: _get-valid-token-names($mat-tokens, $mdc-tokens);
223+
@each $token-map in $token-maps {
224+
$prefix: map.get($token-map, prefix);
225+
$tokens: map.get($token-map, tokens);
233226

234-
@each $name, $value in $tokens {
235-
$is-mat-token: list.index($mat-token-names, $name) != null;
236-
$is-mdc-token: list.index($mdc-token-names, $name) != null;
227+
@each $name, $value in $tokens {
228+
$tokens: map.set($tokens, $name, map.get($overrides, $name));
229+
}
237230

238-
@if ($is-mat-token) {
239-
$custom-mat-tokens: map.set($custom-mat-tokens, $name, $value);
231+
@include sass-utils.current-selector-or-root() {
232+
@include create-token-values($prefix, $tokens);
240233
}
234+
}
235+
}
236+
237+
/// Verifies that the token overrides exist and are used in one of the given token maps.
238+
@mixin _validate-token-overrides($overrides: (), $token-maps) {
239+
$valid-token-names: ();
241240

242-
@if ($is-mdc-token) {
243-
$custom-mdc-tokens: map.set($custom-mdc-tokens, $name, $value);
241+
@each $token-map in $token-maps {
242+
@each $name, $value in map.get($token-map, tokens) {
243+
@if ($value != null and list.index($valid-token-names, $name) == null) {
244+
$valid-token-names: list.append($valid-token-names, $name);
245+
}
244246
}
247+
}
245248

249+
@each $name in map.keys($overrides) {
246250
@if (list.index($valid-token-names, $name) == null) {
247251
@error (
248252
'Invalid token: "' + $name + '"'
249253
'Valid tokens include: ' $valid-token-names
250254
);
251255
}
252256
}
253-
254-
@include sass-utils.current-selector-or-root() {
255-
@include create-token-values($mat-prefix, $custom-mat-tokens);
256-
@include create-token-values($mdc-prefix, $custom-mdc-tokens);
257-
}
258-
}
259-
260-
/// Returns the union of token names whose values are not null.
261-
@function _get-valid-token-names($mat-tokens, $mdc-tokens) {
262-
$mat-and-mdc-tokens: map.merge($mat-tokens, $mdc-tokens);
263-
264-
@each $name, $value in $mat-and-mdc-tokens {
265-
@if ($value == null) {
266-
$mat-and-mdc-tokens: map.remove($mat-and-mdc-tokens, $name);
267-
}
268-
}
269-
270-
@return map.keys($mat-and-mdc-tokens);
271257
}

0 commit comments

Comments
 (0)