Skip to content

Commit

Permalink
fixup! feat(material-experimental/theming): add mixin for customizing…
Browse files Browse the repository at this point in the history
… checkbox tokens
  • Loading branch information
wagnermaciel committed Mar 22, 2024
1 parent 3eec0f7 commit cb55db4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/material/checkbox/_checkbox-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@
}
}

@mixin customizations($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(),
);
}

/// Outputs all (base, color, typography, and density) theme styles for the mat-checkbox.
/// @param {Map} $theme The theme to generate styles for.
/// @param {ArgList} Additional optional arguments (only supported for M3 themes):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/// Emits new token values for the given tokens.
/// Verifies that the tokens passed in are valid tokens.
/// New token values are emitted under the current selector or root.
@mixin _customize-tokens(
@mixin create-token-values(
$tokens: (),
$mat-prefix: '',
$mdc-prefix: '',
Expand Down
62 changes: 59 additions & 3 deletions src/material/core/tokens/_token-utils.scss
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ $_component-prefix: null;
$elevation: map.get($tokens, $elevation-token);
$shadow-color: map.get($tokens, $shadow-color-token);
@return map.merge($tokens, (
$elevation-token: mdc-elevation-theme.elevation-box-shadow($elevation, $shadow-color),
$shadow-color-token: null,
));
$elevation-token: mdc-elevation-theme.elevation-box-shadow($elevation, $shadow-color),
$shadow-color-token: null,
));
}

/// Checks whether a list starts wih a given prefix
Expand Down Expand Up @@ -206,3 +206,59 @@ $_component-prefix: null;
}
@return map.merge($values, $overrides);
}

/// Emits new token values for the given tokens.
/// Verifies that the tokens 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: ();

$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 $name, $value in $tokens {
$is-mat-token: list.index($mat-token-names, $name) != null;
$is-mdc-token: list.index($mdc-token-names, $name) != null;

@if ($is-mat-token) {
$custom-mat-tokens: map.set($custom-mat-tokens, $name, $value);
}

@if ($is-mdc-token) {
$custom-mdc-tokens: map.set($custom-mdc-tokens, $name, $value);
}

@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);
}

0 comments on commit cb55db4

Please sign in to comment.