Skip to content

Commit

Permalink
fix(material/slide-toggle): change slide-toggle to use MDC's token API (
Browse files Browse the repository at this point in the history
  • Loading branch information
amysorto authored Jun 20, 2023
1 parent df30433 commit 860aa42
Show file tree
Hide file tree
Showing 5 changed files with 315 additions and 85 deletions.
52 changes: 52 additions & 0 deletions src/material/core/tokens/m2/mat/_slide-toggle.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@use '../../token-utils';
@use '../../../style/sass-utils';
@use '../../../typography/typography-utils';

// The prefix used to generate the fully qualified name for tokens in this file.
$prefix: (mat, slide-toggle);

// Tokens that can't be configured through Angular Material's current theming API,
// but may be in a future version of the theming API.
@function get-unthemable-tokens() {
@return ();
}

// Tokens that can be configured through Angular Material's color theming API.
@function get-color-tokens($config) {
@return ();
}

// Tokens that can be configured through Angular Material's typography theming API.
@function get-typography-tokens($config) {
// TODO(amysorto): The earlier implementation of the slide-toggle used MDC's APIs to create the
// typography tokens. As a result, we unintentionally allowed `null` typography configs to be
// passed in. Since there a lot of apps that now depend on this pattern, we need this temporary
// fallback.
@if ($config == null) {
$config: mdc-helpers.private-fallback-typography-from-mdc();
}

@return (
label-text-font: typography-utils.font-family($config),
label-text-size: typography-utils.font-size($config, body-2),
label-text-letter-spacing: typography-utils.letter-spacing($config, body-2),
label-text-line-height: typography-utils.line-height($config, body-2),
label-text-weight: typography-utils.font-weight($config, body-2),
);
}

// Tokens that can be configured through Angular Material's density theming API.
@function get-density-tokens($config) {
@return ();
}

// Combines the tokens generated by the above functions into a single map with placeholder values.
// This is used to create token slots.
@function get-token-slots() {
@return sass-utils.deep-merge-all(
get-unthemable-tokens(),
get-color-tokens(token-utils.$placeholder-color-config),
get-typography-tokens(token-utils.$placeholder-typography-config),
get-density-tokens(token-utils.$placeholder-density-config)
);
}
182 changes: 182 additions & 0 deletions src/material/core/tokens/m2/mdc/_switch.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
@use 'sass:map';
@use '../../../style/elevation';
@use '../../../style/sass-utils';
@use '../../../theming/theming';
@use '../../token-utils';

// The prefix used to generate the fully qualified name for tokens in this file.
$prefix: (mdc, switch);

// Tokens that can't be configured through Angular Material's current theming API,
// but may be in a future version of the theming API.
//
// Tokens that are available in MDC, but not used in Angular Material should be mapped to `null`.
// `null` indicates that we are intentionally choosing not to emit a slot or value for the token in
// our CSS.

@function get-unthemable-tokens() {
@return (
// Opacity of handle when disabled.
disabled-handle-opacity: 0.38,
// Opacity of icon when disabled and selected.
disabled-selected-icon-opacity: 0.38,
// Opacity of track when disabled.
disabled-track-opacity: 0.12,
// Opacity of icon when disabled and unselected.
disabled-unselected-icon-opacity: 0.38,
// Height of handle.
handle-height: 20px,
// Border radius of handle.
handle-shape: 10px,
// Width of handle.
handle-width: 20px,
// Width and height of icon when selected.
selected-icon-size: 18px,
// Height of track.
track-height: 14px,
// Border radius of track.
track-shape: 7px,
// Width of track.
track-width: 36px,
// Width and height of icon when unselected.
unselected-icon-size: 18px,
// The diameter of the handle ripple.
state-layer-size: 40px,
// Opacity of ripple when selected and focused.
selected-focus-state-layer-opacity: 0.12,
// Opacity of ripple when selected and on hover.
selected-hover-state-layer-opacity: 0.04,
// Opacity of ripple when selected and pressed.
selected-pressed-state-layer-opacity: 0.1,
// Opacity of ripple when unselected and focused.
unselected-focus-state-layer-opacity: 0.12,
// Opacity of ripple when unselected and on hover.
unselected-hover-state-layer-opacity: 0.04,
// Opacity of ripple when unselected and pressed.
unselected-pressed-state-layer-opacity: 0.1,
);
}

// Tokens that can be configured through Angular Material's color theming API.
@function get-color-tokens($config) {
$foreground: map.get($config, foreground);
$elevation: theming.get-color-from-palette($foreground, elevation);
$is-dark: map.get($config, is-dark);
$on-surface: if($is-dark, #f5f5f5, #424242);
$hairline: if($is-dark, #616161, #e0e0e0);
$on-surface-variant: if($is-dark, #9e9e9e, #616161);
$on-surface-state-content: if($is-dark, #fafafa, #212121);
$disabled-handle-color: if($is-dark, #000, #424242);
$icon-color: if($is-dark, #212121, #fff);

// The default for tokens that rely on the theme will use the primary palette
$theme-color-tokens: private-get-color-palette-color-tokens(
map.get($config, primary),
$is-dark
);

@return map.merge(
$theme-color-tokens,
(
// Color of handle when selected while disabled.
disabled-selected-handle-color: $disabled-handle-color,
// Color of handle when selected while disabled.
disabled-selected-icon-color: $icon-color,
// Color of track when selected while disabled.
disabled-selected-track-color: $on-surface,
// Color of handle when unselected while disabled.
disabled-unselected-handle-color: $disabled-handle-color,
// Color of icon when unselected while disabled.
disabled-unselected-icon-color: $icon-color,
// Color of track when disabled.
disabled-unselected-track-color: $on-surface,
// Color of slide-toggle handle's surface.
handle-surface-color: surface,
// Elevation level for handle.
handle-elevation: 1,
// Color of handle's shadow
handle-shadow-color: if($elevation != null, $elevation, elevation.$color),
// Elevation level for handle when disabled.
disabled-handle-elevation: 0,
// Color of icon (ex. checkmark) when selected
selected-icon-color: $icon-color,
// Color of handle when unselected and focused.
unselected-focus-handle-color: $on-surface-state-content,
// Color of ripple when unselected and focused.
unselected-focus-state-layer-color: $on-surface,
// Color of track when unselected and focused.
unselected-focus-track-color: $hairline,
// Color of handle when unselected.
unselected-handle-color: $on-surface-variant,
// Color of handle when unselected and on hover.
unselected-hover-handle-color: $on-surface-state-content,
// Color of ripple when unselected and on hover.
unselected-hover-state-layer-color: $on-surface,
// Color of track when unselected and on hover.
unselected-hover-track-color: $hairline,
// Color of icon color when unselected.
unselected-icon-color: $icon-color,
// Color of handle when unselected and pressed.
unselected-pressed-handle-color: $on-surface-state-content,
// Color of ripple when unselected and pressed.
unselected-pressed-state-layer-color: $on-surface,
// Color of track when unselected and pressed.
unselected-pressed-track-color: $hairline,
// Color of track when selected.
unselected-track-color: $hairline,
)
);
}

// Generates the mapping for the properties that change based on the slide toggle color.
@function private-get-color-palette-color-tokens($color-palette, $is-dark) {
$primary: theming.get-color-from-palette($color-palette, if($is-dark, 300, 600));
$state-content: theming.get-color-from-palette($color-palette, if($is-dark, 200, 900));
$inverse: theming.get-color-from-palette($color-palette, if($is-dark, 600, 300));

@return (
// Color of ripple when selected and focused.
selected-focus-state-layer-color: $primary,
// Color of handle when selected
selected-handle-color: $primary,
// Color of ripple when selected and on hover.
selected-hover-state-layer-color: $primary,
// Color of ripple when selected and pressed.
selected-pressed-state-layer-color: $primary,
// Color of handle when selected and focused.
selected-focus-handle-color: $state-content,
// Color of handle when selected and on hover.
selected-hover-handle-color: $state-content,
// Color of handle when selected and pressed.
selected-pressed-handle-color: $state-content,
// Color of track when selected and focused.
selected-focus-track-color: $inverse,
// Color of track when selected and on hover.
selected-hover-track-color: $inverse,
/// Color of track when selected and pressed.
selected-pressed-track-color: $inverse,
// Color of track when selected.
selected-track-color: $inverse,
);
}

// Tokens that can be configured through Angular Material's typography theming API.
@function get-typography-tokens($config) {
@return ();
}

// Tokens that can be configured through Angular Material's density theming API.
@function get-density-tokens($config) {
@return ();
}

// Combines the tokens generated by the above functions into a single map with placeholder values.
// This is used to create token slots.
@function get-token-slots() {
@return sass-utils.deep-merge-all(
get-unthemable-tokens(),
get-color-tokens(token-utils.$placeholder-color-config),
get-typography-tokens(token-utils.$placeholder-typography-config),
get-density-tokens(token-utils.$placeholder-density-config)
);
}
7 changes: 7 additions & 0 deletions src/material/core/tokens/tests/test-validate-tokens.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@use '@material/list/list-theme' as mdc-list-theme;
@use '@material/tooltip/plain-tooltip-theme' as mdc-plaintip-tooltip-theme;
@use '@material/radio/radio-theme' as mdc-radio-theme;
@use '@material/switch/switch-theme' as mdc-switch-theme;
@use '@material/tab-indicator/tab-indicator-theme' as mdc-tab-indicator-theme;
@use '@material/tab/tab-theme' as mdc-tab-theme;
@use '@material/snackbar/snackbar-theme' as mdc-snackbar-theme;
Expand All @@ -25,6 +26,7 @@
@use '../m2/mdc/outlined-card' as tokens-mdc-outlined-card;
@use '../m2/mdc/plain-tooltip' as tokens-mdc-plain-tooltip;
@use '../m2/mdc/radio' as tokens-mdc-radio;
@use '../m2/mdc/switch' as tokens-mdc-switch;
@use '../m2/mdc/tab-indicator' as tokens-mdc-tab-indicator;
@use '../m2/mdc/tab' as tokens-mdc-tab;
@use '../m2/mdc/snack-bar' as tokens-mdc-snack-bar;
Expand Down Expand Up @@ -87,6 +89,11 @@
$slots: tokens-mdc-radio.get-token-slots(),
$reference: mdc-radio-theme.$light-theme
);
@include validate-slots(
$component: 'm2.mdc.switch',
$slots: tokens-mdc-switch.get-token-slots(),
$reference: mdc-switch-theme.$light-theme,
);
@include validate-slots(
$component: 'm2.mdc.tab-indicator',
$slots: tokens-mdc-tab-indicator.get-token-slots(),
Expand Down
Loading

0 comments on commit 860aa42

Please sign in to comment.