Skip to content

refactor(material/theming): make batch-create-token-values more flexible #28897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/material/checkbox/_checkbox-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@
@mixin overrides($tokens: ()) {
@include token-utils.batch-create-token-values(
$tokens,
$mat-prefix: tokens-mat-checkbox.$prefix,
$mdc-prefix: tokens-mdc-checkbox.$prefix,
$mat-tokens: tokens-mat-checkbox.get-token-slots(),
$mdc-tokens: tokens-mdc-checkbox.get-token-slots(),
(prefix: tokens-mat-checkbox.$prefix, tokens: tokens-mat-checkbox.get-token-slots()),
(prefix: tokens-mdc-checkbox.$prefix, tokens: tokens-mdc-checkbox.get-token-slots()),
);
}

Expand Down
64 changes: 25 additions & 39 deletions src/material/core/tokens/_token-utils.scss
Original file line number Diff line number Diff line change
Expand Up @@ -210,58 +210,44 @@ $_component-prefix: null;
@return if($emit-overrides-only, $overrides, map.merge($values, $overrides));
}

/// Emits new token values for the given tokens.
/// Verifies that the tokens passed in are valid tokens.
/// Emits new token values for the given token overrides.
/// Verifies that the overrides passed in are valid tokens.
/// New token values are emitted under the current selector or root.
@mixin batch-create-token-values(
$tokens: (),
$mat-prefix: '',
$mdc-prefix: '',
$mat-tokens: (),
$mdc-tokens: ()
) {
$custom-mat-tokens: ();
$custom-mdc-tokens: ();
@mixin batch-create-token-values($overrides: (), $token-maps...) {
@include _validate-token-overrides($overrides, $token-maps);

$mat-token-names: map.keys($mat-tokens);
$mdc-token-names: map.keys($mdc-tokens);
$valid-token-names: _get-valid-token-names($mat-tokens, $mdc-tokens);
@each $token-map in $token-maps {
$prefix: map.get($token-map, prefix);
$tokens: map.get($token-map, tokens);

@each $name, $value in $tokens {
$is-mat-token: list.index($mat-token-names, $name) != null;
$is-mdc-token: list.index($mdc-token-names, $name) != null;
@each $name, $value in $tokens {
$tokens: map.set($tokens, $name, map.get($overrides, $name));
}

@if ($is-mat-token) {
$custom-mat-tokens: map.set($custom-mat-tokens, $name, $value);
@include sass-utils.current-selector-or-root() {
@include create-token-values($prefix, $tokens);
}
}
}

/// Verifies that the token overrides exist and are used in one of the given token maps.
@mixin _validate-token-overrides($overrides: (), $token-maps) {
$valid-token-names: ();

@if ($is-mdc-token) {
$custom-mdc-tokens: map.set($custom-mdc-tokens, $name, $value);
@each $token-map in $token-maps {
@each $name, $value in map.get($token-map, tokens) {
@if ($value != null and list.index($valid-token-names, $name) == null) {
$valid-token-names: list.append($valid-token-names, $name);
}
}
}

@each $name in map.keys($overrides) {
@if (list.index($valid-token-names, $name) == null) {
@error (
'Invalid token: "' + $name + '"'
'Valid tokens include: ' $valid-token-names
);
}
}

@include sass-utils.current-selector-or-root() {
@include create-token-values($mat-prefix, $custom-mat-tokens);
@include create-token-values($mdc-prefix, $custom-mdc-tokens);
}
}

/// Returns the union of token names whose values are not null.
@function _get-valid-token-names($mat-tokens, $mdc-tokens) {
$mat-and-mdc-tokens: map.merge($mat-tokens, $mdc-tokens);

@each $name, $value in $mat-and-mdc-tokens {
@if ($value == null) {
$mat-and-mdc-tokens: map.remove($mat-and-mdc-tokens, $name);
}
}

@return map.keys($mat-and-mdc-tokens);
}