Skip to content

Commit

Permalink
feat(material-experimental/theming): add M3 option & optgroup support
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalerba committed Nov 17, 2023
1 parent 7a03dfc commit c17164a
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 39 deletions.
6 changes: 6 additions & 0 deletions src/dev-app/theme-m3.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions src/material-experimental/theming/_custom-tokens.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/material-experimental/theming/_m3-density.scss
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ $_density-tokens: (
(mat, grid-list): (),
(mat, icon): (),
(mat, menu): (),
(mat, optgroup): (),
(mat, option): (),
(mat, radio): (),
(mat, ripple): (),
(mat, select): (),
Expand Down
10 changes: 10 additions & 0 deletions src/material-experimental/theming/_m3-tokens.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
63 changes: 47 additions & 16 deletions src/material/core/option/_optgroup-theme.scss
Original file line number Diff line number Diff line change
@@ -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));
}
}
77 changes: 54 additions & 23 deletions src/material/core/option/_option-theme.scss
Original file line number Diff line number Diff line change
@@ -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));
}
}
4 changes: 4 additions & 0 deletions src/material/core/tokens/m2/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit c17164a

Please sign in to comment.