Skip to content

Commit c345df7

Browse files
authored
feat(material-experimental/theming): add mixin for customizing checkbox tokens (#28759)
1 parent 457ce69 commit c345df7

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/material/checkbox/_checkbox-theme.scss

+12
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@
9292
}
9393
}
9494

95+
/// Outputs the CSS variable values for the given tokens.
96+
/// @param {Map} $tokens The token values to emit.
97+
@mixin overrides($tokens: ()) {
98+
@include token-utils.batch-create-token-values(
99+
$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(),
104+
);
105+
}
106+
95107
/// Outputs all (base, color, typography, and density) theme styles for the mat-checkbox.
96108
/// @param {Map} $theme The theme to generate styles for.
97109
/// @param {ArgList} Additional optional arguments (only supported for M3 themes):

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

+56
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,59 @@ $_component-prefix: null;
209209
}
210210
@return if($emit-overrides-only, $overrides, map.merge($values, $overrides));
211211
}
212+
213+
/// Emits new token values for the given tokens.
214+
/// Verifies that the tokens passed in are valid tokens.
215+
/// New token values are emitted under the current selector or root.
216+
@mixin batch-create-token-values(
217+
$tokens: (),
218+
$mat-prefix: '',
219+
$mdc-prefix: '',
220+
$mat-tokens: (),
221+
$mdc-tokens: ()
222+
) {
223+
$custom-mat-tokens: ();
224+
$custom-mdc-tokens: ();
225+
226+
$mat-token-names: map.keys($mat-tokens);
227+
$mdc-token-names: map.keys($mdc-tokens);
228+
$valid-token-names: _get-valid-token-names($mat-tokens, $mdc-tokens);
229+
230+
@each $name, $value in $tokens {
231+
$is-mat-token: list.index($mat-token-names, $name) != null;
232+
$is-mdc-token: list.index($mdc-token-names, $name) != null;
233+
234+
@if ($is-mat-token) {
235+
$custom-mat-tokens: map.set($custom-mat-tokens, $name, $value);
236+
}
237+
238+
@if ($is-mdc-token) {
239+
$custom-mdc-tokens: map.set($custom-mdc-tokens, $name, $value);
240+
}
241+
242+
@if (list.index($valid-token-names, $name) == null) {
243+
@error (
244+
'Invalid token: "' + $name + '"'
245+
'Valid tokens include: ' $valid-token-names
246+
);
247+
}
248+
}
249+
250+
@include sass-utils.current-selector-or-root() {
251+
@include create-token-values($mat-prefix, $custom-mat-tokens);
252+
@include create-token-values($mdc-prefix, $custom-mdc-tokens);
253+
}
254+
}
255+
256+
/// Returns the union of token names whose values are not null.
257+
@function _get-valid-token-names($mat-tokens, $mdc-tokens) {
258+
$mat-and-mdc-tokens: map.merge($mat-tokens, $mdc-tokens);
259+
260+
@each $name, $value in $mat-and-mdc-tokens {
261+
@if ($value == null) {
262+
$mat-and-mdc-tokens: map.remove($mat-and-mdc-tokens, $name);
263+
}
264+
}
265+
266+
@return map.keys($mat-and-mdc-tokens);
267+
}

0 commit comments

Comments
 (0)