Skip to content

refactor(material/core): simplify token utils #29430

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
Jul 16, 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
56 changes: 33 additions & 23 deletions src/material/core/tokens/_token-utils.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
@use 'sass:list';
@use 'sass:map';
@use 'sass:meta';
@use '@material/theme/custom-properties' as mdc-custom-properties;
@use '@material/theme/theme' as mdc-theme;
@use '@material/theme/keys' as mdc-keys;
@use 'sass:string';
@use '@material/tokens/v0_161' as mdc-tokens;
@use '../style/elevation';
@use '../style/sass-utils';
Expand Down Expand Up @@ -57,27 +55,33 @@ $placeholder-density-config: 0;
$_tokens: null;
$_component-prefix: null;

@mixin _configure-token-prefix($first, $rest...) {
$_component-prefix: '' !global;
@each $item in $rest {
$_component-prefix:
if($_component-prefix == '', $item, '#{$_component-prefix}-#{$item}') !global;
}
@include mdc-custom-properties.configure($varname-prefix: $first) {
@content;
}
$_component-prefix: null !global;
}

// Sets the token prefix and map to use when creating token slots.
@mixin use-tokens($prefix, $tokens) {
$_component-prefix: $prefix !global;
$_tokens: $tokens !global;
@include _configure-token-prefix($prefix...) {
@content;
}

@content;

$_component-prefix: null !global;
$_tokens: null !global;
}

// Combines a prefix and a string to generate a CSS variable name for a token.
@function _get-css-variable($prefix, $name) {
@if $prefix == null or $name == null {
@error 'Must specify both prefix and name when generating token';
}

$string-prefix: '';

// Prefixes are lists so we need to combine them.
@each $part in $prefix {
$string-prefix: if($string-prefix == '', $part, '#{$string-prefix}-#{$part}');
}

@return string.unquote('--#{$string-prefix}-#{$name}');
}

// Emits a slot for the given token, provided that it has a non-null value in the token map passed
// to `use-tokens`.
@mixin create-token-slot($property, $token, $emit-fallback: false) {
Expand All @@ -97,8 +101,9 @@ $_component-prefix: null;
$fallback: $emit-fallback;
}

$value: mdc-custom-properties.create('#{$_component-prefix}-#{$token}', $fallback: $fallback);
@include mdc-theme.property($property, $value);
$var-name: _get-css-variable($_component-prefix, $token);
$var-reference: if($fallback == null, var(#{$var-name}), var(#{$var-name}, #{$fallback}));
#{$property}: #{$var-reference};
}
}

Expand All @@ -112,7 +117,7 @@ $_component-prefix: null;
@error 'Token #{$token} does not exist. Configured tokens are: #{map.keys($_tokens)}';
}

@return mdc-custom-properties.create-varname('#{$_component-prefix}-#{$token}');
@return _get-css-variable($_component-prefix, $token);
}

// TODO(crisbeto): should be able to replace the usages of `get-token-variable` with this.
Expand All @@ -137,9 +142,14 @@ $_component-prefix: null;
}
}

// Outputs a map of tokens under a specific prefix.
@mixin create-token-values($prefix, $tokens) {
@include _configure-token-prefix($prefix...) {
@include mdc-keys.declare-custom-properties($tokens, $_component-prefix);
@if $tokens != null {
@each $key, $value in $tokens {
@if $value != null {
#{_get-css-variable($prefix, $key)}: #{$value};
}
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/material/core/tokens/m2/mdc/_switch.scss
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ $prefix: (mdc, switch);
disabled-unselected-icon-color: $icon-color,
// Color of track when disabled.
disabled-unselected-track-color: $on-surface,
// TODO(crisbeto): `handle-surface-color` was hardcoded to `var(--mdc-surface-color, #fff)`
// which made it basically hardcoded to #fff. Should it be based on the theme?
// Color of slide-toggle handle's surface.
handle-surface-color: surface,
handle-surface-color: #fff,
// Color of icon (ex. checkmark) when selected
selected-icon-color: $icon-color,
// Color of handle when unselected and focused.
Expand Down
Loading