From c17164a57583fdc76bce75f3c99bac77dd9a8eb6 Mon Sep 17 00:00:00 2001 From: Miles Malerba Date: Fri, 17 Nov 2023 21:36:29 +0000 Subject: [PATCH] feat(material-experimental/theming): add M3 option & optgroup support --- src/dev-app/theme-m3.scss | 6 ++ .../theming/_custom-tokens.scss | 36 +++++++++ .../theming/_m3-density.scss | 2 + .../theming/_m3-tokens.scss | 10 +++ src/material/core/option/_optgroup-theme.scss | 63 +++++++++++---- src/material/core/option/_option-theme.scss | 77 +++++++++++++------ src/material/core/tokens/m2/_index.scss | 4 + 7 files changed, 159 insertions(+), 39 deletions(-) diff --git a/src/dev-app/theme-m3.scss b/src/dev-app/theme-m3.scss index e7b59ff52cfa..db5f48009ccd 100644 --- a/src/dev-app/theme-m3.scss +++ b/src/dev-app/theme-m3.scss @@ -45,6 +45,8 @@ html { @include mat.input-theme($light-theme); @include mat.list-theme($light-theme); @include mat.menu-theme($light-theme); + @include mat.optgroup-theme($light-theme); + @include mat.option-theme($light-theme); @include mat.progress-bar-theme($light-theme); @include mat.progress-spinner-theme($light-theme); @include mat.radio-theme($light-theme); @@ -78,6 +80,8 @@ html { @include mat.input-color($dark-theme); @include mat.list-color($dark-theme); @include mat.menu-color($dark-theme); + @include mat.optgroup-color($dark-theme); + @include mat.option-color($dark-theme); @include mat.progress-bar-color($dark-theme); @include mat.progress-spinner-color($dark-theme); @include mat.radio-color($dark-theme); @@ -110,6 +114,8 @@ html { @include mat.input-density($scale-theme); @include mat.list-density($scale-theme); @include mat.menu-density($scale-theme); + @include mat.optgroup-density($scale-theme); + @include mat.option-density($scale-theme); @include mat.progress-bar-density($scale-theme); @include mat.progress-spinner-density($scale-theme); @include mat.radio-density($scale-theme); diff --git a/src/material-experimental/theming/_custom-tokens.scss b/src/material-experimental/theming/_custom-tokens.scss index 1d61a7b86164..247552c5c37c 100644 --- a/src/material-experimental/theming/_custom-tokens.scss +++ b/src/material-experimental/theming/_custom-tokens.scss @@ -120,6 +120,42 @@ ); } +/// Generates custom tokens for the mat-optgroup. +/// @param {Map} $systems The MDC system tokens +/// @param {Boolean} $exclude-hardcoded Whether to exclude hardcoded token values +/// @return {Map} A set of custom tokens for the mat-optgroup +@function optgroup($systems, $exclude-hardcoded) { + @return mat.private-merge-all( + _generate-typography-tokens($systems, label-text, title-small), + ( + label-text-color: map.get($systems, md-sys-color, on-surface-variant), + ) + ); +} + +/// Generates custom tokens for the mat-option. +/// @param {Map} $systems The MDC system tokens +/// @param {Boolean} $exclude-hardcoded Whether to exclude hardcoded token values +/// @return {Map} A set of custom tokens for the mat-option +@function option($systems, $exclude-hardcoded) { + @return mat.private-merge-all( + _generate-typography-tokens($systems, label-text, label-large), + ( + selected-state-label-text-color: map.get($systems, md-sys-color, on-secondary-container), + label-text-color: map.get($systems, md-sys-color, on-surface), + hover-state-layer-color: mat.private-safe-color-change( + map.get($systems, md-sys-color, on-surface), + $alpha: map.get($systems, md-sys-state, hover-state-layer-opacity) + ), + focus-state-layer-color: mat.private-safe-color-change( + map.get($systems, md-sys-color, on-surface), + $alpha: map.get($systems, md-sys-state, focus-state-layer-opacity) + ), + selected-state-layer-color: map.get($systems, md-sys-color, secondary-container), + ), + ); +} + /// Generates custom tokens for the mat-radio. /// @param {Map} $systems The MDC system tokens /// @param {Boolean} $exclude-hardcoded Whether to exclude hardcoded token values diff --git a/src/material-experimental/theming/_m3-density.scss b/src/material-experimental/theming/_m3-density.scss index 4e5654981849..5ba148483a38 100644 --- a/src/material-experimental/theming/_m3-density.scss +++ b/src/material-experimental/theming/_m3-density.scss @@ -49,6 +49,8 @@ $_density-tokens: ( (mat, grid-list): (), (mat, icon): (), (mat, menu): (), + (mat, optgroup): (), + (mat, option): (), (mat, radio): (), (mat, ripple): (), (mat, select): (), diff --git a/src/material-experimental/theming/_m3-tokens.scss b/src/material-experimental/theming/_m3-tokens.scss index ea78779c66f6..114293f88529 100644 --- a/src/material-experimental/theming/_m3-tokens.scss +++ b/src/material-experimental/theming/_m3-tokens.scss @@ -280,6 +280,16 @@ custom-tokens.menu($systems, $exclude-hardcoded), $token-slots ), + _namespace-tokens( + (mat, optgroup), + custom-tokens.optgroup($systems, $exclude-hardcoded), + $token-slots + ), + _namespace-tokens( + (mat, option), + custom-tokens.option($systems, $exclude-hardcoded), + $token-slots + ), _namespace-tokens( (mat, radio), custom-tokens.radio($systems, $exclude-hardcoded), diff --git a/src/material/core/option/_optgroup-theme.scss b/src/material/core/option/_optgroup-theme.scss index 275906bb0ab7..5cd02dad20b3 100644 --- a/src/material/core/option/_optgroup-theme.scss +++ b/src/material/core/option/_optgroup-theme.scss @@ -1,41 +1,72 @@ +@use 'sass:map'; @use '../tokens/m2/mat/optgroup' as tokens-mat-optgroup; @use '../tokens/token-utils'; @use '../style/sass-utils'; - @use '../theming/theming'; @use '../theming/inspection'; @use '../typography/typography'; -@mixin base($theme) {} +@mixin base($theme) { + @if inspection.get-theme-version($theme) == 1 { + @include _theme-from-tokens(inspection.get-theme-tokens($theme, base)); + } + @else {} +} @mixin color($theme) { - @include sass-utils.current-selector-or-root() { - @include token-utils.create-token-values(tokens-mat-optgroup.$prefix, - tokens-mat-optgroup.get-color-tokens($theme)); + @if inspection.get-theme-version($theme) == 1 { + @include _theme-from-tokens(inspection.get-theme-tokens($theme, color)); + } + @else { + @include sass-utils.current-selector-or-root() { + @include token-utils.create-token-values(tokens-mat-optgroup.$prefix, + tokens-mat-optgroup.get-color-tokens($theme)); + } } } @mixin typography($theme) { - @include sass-utils.current-selector-or-root() { - @include token-utils.create-token-values(tokens-mat-optgroup.$prefix, - tokens-mat-optgroup.get-typography-tokens($theme)); + @if inspection.get-theme-version($theme) == 1 { + @include _theme-from-tokens(inspection.get-theme-tokens($theme, typography)); + } + @else { + @include sass-utils.current-selector-or-root() { + @include token-utils.create-token-values(tokens-mat-optgroup.$prefix, + tokens-mat-optgroup.get-typography-tokens($theme)); + } } } @mixin density($theme) { + @if inspection.get-theme-version($theme) == 1 { + @include _theme-from-tokens(inspection.get-theme-tokens($theme, density)); + } + @else {} } @mixin theme($theme) { @include theming.private-check-duplicate-theme-styles($theme, 'mat-optgroup') { - @include base($theme); - @if inspection.theme-has($theme, color) { - @include color($theme); - } - @if inspection.theme-has($theme, density) { - @include density($theme); + @if inspection.get-theme-version($theme) == 1 { + @include _theme-from-tokens(inspection.get-theme-tokens($theme)); } - @if inspection.theme-has($theme, typography) { - @include typography($theme); + @else { + @include base($theme); + @if inspection.theme-has($theme, color) { + @include color($theme); + } + @if inspection.theme-has($theme, density) { + @include density($theme); + } + @if inspection.theme-has($theme, typography) { + @include typography($theme); + } } } } + +@mixin _theme-from-tokens($tokens) { + @if ($tokens != ()) { + @include token-utils.create-token-values( + tokens-mat-optgroup.$prefix, map.get($tokens, tokens-mat-optgroup.$prefix)); + } +} diff --git a/src/material/core/option/_option-theme.scss b/src/material/core/option/_option-theme.scss index 662ff5a574eb..ca02734adcd9 100644 --- a/src/material/core/option/_option-theme.scss +++ b/src/material/core/option/_option-theme.scss @@ -1,51 +1,82 @@ +@use 'sass:map'; @use '../tokens/m2/mat/option' as tokens-mat-option; @use '../tokens/token-utils'; @use '../style/sass-utils'; - @use '../theming/theming'; @use '../theming/inspection'; @use '../typography/typography'; -@mixin base($theme) {} +@mixin base($theme) { + @if inspection.get-theme-version($theme) == 1 { + @include _theme-from-tokens(inspection.get-theme-tokens($theme, base)); + } + @else {} +} @mixin color($theme) { - @include sass-utils.current-selector-or-root() { - @include token-utils.create-token-values(tokens-mat-option.$prefix, - tokens-mat-option.get-color-tokens($theme)); + @if inspection.get-theme-version($theme) == 1 { + @include _theme-from-tokens(inspection.get-theme-tokens($theme, color)); } + @else { + @include sass-utils.current-selector-or-root() { + @include token-utils.create-token-values(tokens-mat-option.$prefix, + tokens-mat-option.get-color-tokens($theme)); + } - .mat-accent { - @include token-utils.create-token-values(tokens-mat-option.$prefix, - tokens-mat-option.get-color-tokens($theme, accent)); - } + .mat-accent { + @include token-utils.create-token-values(tokens-mat-option.$prefix, + tokens-mat-option.get-color-tokens($theme, accent)); + } - .mat-warn { - @include token-utils.create-token-values(tokens-mat-option.$prefix, - tokens-mat-option.get-color-tokens($theme, warn)); + .mat-warn { + @include token-utils.create-token-values(tokens-mat-option.$prefix, + tokens-mat-option.get-color-tokens($theme, warn)); + } } } @mixin typography($theme) { - @include sass-utils.current-selector-or-root() { - @include token-utils.create-token-values(tokens-mat-option.$prefix, - tokens-mat-option.get-typography-tokens($theme)); + @if inspection.get-theme-version($theme) == 1 { + @include _theme-from-tokens(inspection.get-theme-tokens($theme, typography)); + } + @else { + @include sass-utils.current-selector-or-root() { + @include token-utils.create-token-values(tokens-mat-option.$prefix, + tokens-mat-option.get-typography-tokens($theme)); + } } } @mixin density($theme) { + @if inspection.get-theme-version($theme) == 1 { + @include _theme-from-tokens(inspection.get-theme-tokens($theme, density)); + } + @else {} } @mixin theme($theme) { @include theming.private-check-duplicate-theme-styles($theme, 'mat-option') { - @include base($theme); - @if inspection.theme-has($theme, color) { - @include color($theme); - } - @if inspection.theme-has($theme, density) { - @include density($theme); + @if inspection.get-theme-version($theme) == 1 { + @include _theme-from-tokens(inspection.get-theme-tokens($theme)); } - @if inspection.theme-has($theme, typography) { - @include typography($theme); + @else { + @include base($theme); + @if inspection.theme-has($theme, color) { + @include color($theme); + } + @if inspection.theme-has($theme, density) { + @include density($theme); + } + @if inspection.theme-has($theme, typography) { + @include typography($theme); + } } } } + +@mixin _theme-from-tokens($tokens) { + @if ($tokens != ()) { + @include token-utils.create-token-values( + tokens-mat-option.$prefix, map.get($tokens, tokens-mat-option.$prefix)); + } +} diff --git a/src/material/core/tokens/m2/_index.scss b/src/material/core/tokens/m2/_index.scss index d1fcc013ccfb..bab478ea17ee 100644 --- a/src/material/core/tokens/m2/_index.scss +++ b/src/material/core/tokens/m2/_index.scss @@ -7,6 +7,8 @@ @use './mat/grid-list' as tokens-mat-grid-list; @use './mat/icon' as tokens-mat-icon; @use './mat/menu' as tokens-mat-menu; +@use './mat/option' as tokens-mat-option; +@use './mat/optgroup' as tokens-mat-optgroup; @use './mat/radio' as tokens-mat-radio; @use './mat/ripple' as tokens-mat-ripple; @use './mat/select' as tokens-mat-select; @@ -88,6 +90,8 @@ _get-tokens-for-module($theme, tokens-mat-grid-list), _get-tokens-for-module($theme, tokens-mat-icon), _get-tokens-for-module($theme, tokens-mat-menu), + _get-tokens-for-module($theme, tokens-mat-optgroup), + _get-tokens-for-module($theme, tokens-mat-option), _get-tokens-for-module($theme, tokens-mat-radio), _get-tokens-for-module($theme, tokens-mat-ripple), _get-tokens-for-module($theme, tokens-mat-select),