Skip to content

refactor(material/expansion): switch to tokens API #27390

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 10, 2023
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
1 change: 1 addition & 0 deletions src/material/core/tokens/_token-utils.scss
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ $placeholder-typography-config: (
caption: $_placeholder-typography-level-config,
button: $_placeholder-typography-level-config,
overline: $_placeholder-typography-level-config,
subheading-1: $_placeholder-typography-level-config,
);

// Placeholder density config that can be passed to token getter functions when generating token
Expand Down
90 changes: 90 additions & 0 deletions src/material/core/tokens/m2/mat/_expansion.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
@use 'sass:map';
@use '../../token-utils';
@use '../../../theming/theming';
@use '../../../style/sass-utils';
@use '../../../typography/typography-utils';

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

// 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 (
container-shape: 4px,
);
}

// Tokens that can be configured through Angular Material's color theming API.
@function get-color-tokens($config) {
$foreground: map.get($config, foreground);
$background: map.get($config, background);

@return (
container-background-color: theming.get-color-from-palette($background, card),
container-text-color: theming.get-color-from-palette($foreground, text),
actions-divider-color: theming.get-color-from-palette($foreground, divider),
header-hover-state-layer-color: theming.get-color-from-palette($background, hover),
header-focus-state-layer-color: theming.get-color-from-palette($background, hover),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ty for including state layer color. Focus states are important for accessibility.

[OPtional] If it's not a lot of work, I suggest adding state layer opacity. We don't need that now, but we will probably need it in the future. I'm okay with hard coding the state layer opacity as 0.12 in get-unthemable-tokens.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For that we'll have to put the state layer in a separate element (right now it's just a background-color). I'd rather we address it separately, because I'm trying to keep the DOM changes to a minimum since moving the declarations into the base styles already causes a bunch of broken targets.

header-disabled-state-text-color: theming.get-color-from-palette($foreground, disabled-button),
header-text-color: theming.get-color-from-palette($foreground, text),
header-description-color: theming.get-color-from-palette($foreground, secondary-text),
header-indicator-color: theming.get-color-from-palette($foreground, secondary-text),
);
}

// Tokens that can be configured through Angular Material's typography theming API.
@function get-typography-tokens($config) {
$fallback-font-family: typography-utils.font-family($config);

@return (
header-text-font: typography-utils.font-family($config, subheading-1) or $fallback-font-family,
header-text-size: typography-utils.font-size($config, subheading-1),
header-text-weight: typography-utils.font-weight($config, subheading-1),

// TODO(crisbeto): these two properties weren't set at all before the introduction of tokens,
// but it's inconsistent not to provide them since the container sets all of them. Eventually
// we should change the values to use come from `subheading-1`.
header-text-line-height: inherit,
header-text-tracking: inherit,

container-text-font: typography-utils.font-family($config, body-1) or $fallback-font-family,
container-text-line-height: typography-utils.line-height($config, body-1),
container-text-size: typography-utils.font-size($config, body-1),
container-text-tracking: typography-utils.letter-spacing($config, body-1),
container-text-weight: typography-utils.font-weight($config, body-1),
);
}

// Tokens that can be configured through Angular Material's density theming API.
@function get-density-tokens($config) {
$scale: theming.clamp-density($config, -3);
$collapsed-scale: (
0: 48px,
-1: 44px,
-2: 40px,
-3: 36px,
);
$expanded-scale: (
0: 64px,
-1: 60px,
-2: 56px,
-3: 48px,
);

@return (
header-collapsed-state-height: map.get($collapsed-scale, $scale),
header-expanded-state-height: map.get($expanded-scale, $scale),
);
}

// 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)
);
}
1 change: 0 additions & 1 deletion src/material/expansion/_expansion-legacy-index.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
@forward 'expansion-variables' as mat-expansion-panel-*;
@forward 'expansion-mixins' as mat-*;
@forward 'expansion-theme' as mat-expansion-panel-*;
1 change: 0 additions & 1 deletion src/material/expansion/_expansion-mixins.import.scss

This file was deleted.

11 changes: 0 additions & 11 deletions src/material/expansion/_expansion-mixins.scss

This file was deleted.

2 changes: 0 additions & 2 deletions src/material/expansion/_expansion-theme.import.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
@forward '../core/style/private.import';
@forward 'expansion-variables' as mat-expansion-panel-*;
@forward '../core/typography/typography-utils.import';
@forward 'expansion-mixins' as mat-*;
@forward 'expansion-theme' as mat-expansion-panel-*;

@import '../core/density/private/compatibility';
Expand All @@ -11,4 +10,3 @@
@import '../core/style/private';
@import '../core/typography/typography-utils';
@import './expansion-variables';
@import './expansion-mixins';
80 changes: 12 additions & 68 deletions src/material/expansion/_expansion-theme.scss
Original file line number Diff line number Diff line change
@@ -1,90 +1,34 @@
@use 'sass:map';
@use '../core/density/private/compatibility';
@use '../core/theming/theming';
@use '../core/style/private';
@use '../core/typography/typography';
@use '../core/typography/typography-utils';
@use './expansion-variables';
@use './expansion-mixins';
@use '../core/style/sass-utils';
@use '../core/tokens/token-utils';
@use '../core/tokens/m2/mat/expansion' as tokens-mat-expansion;

@mixin color($config-or-theme) {
$config: theming.get-color-config($config-or-theme);
$background: map.get($config, background);
$foreground: map.get($config, foreground);

.mat-expansion-panel {
@include private.private-theme-overridable-elevation(2, $config);
background: theming.get-color-from-palette($background, card);
color: theming.get-color-from-palette($foreground, text);
}

.mat-action-row {
border-top-color: theming.get-color-from-palette($foreground, divider);
}

@include expansion-mixins.private-expansion-focus {
background: theming.get-color-from-palette($background, hover);
}

// Disable the hover on touch devices since it can appear like it is stuck. We can't use
// `@media (hover)` above, because the desktop support browser support isn't great.
@media (hover: none) {
.mat-expansion-panel:not(.mat-expanded):not([aria-disabled='true'])
.mat-expansion-panel-header:hover {
background: theming.get-color-from-palette($background, card);
}
}

.mat-expansion-panel-header-title {
color: theming.get-color-from-palette($foreground, text);
}

.mat-expansion-panel-header-description,
.mat-expansion-indicator::after {
color: theming.get-color-from-palette($foreground, secondary-text);
}

.mat-expansion-panel-header[aria-disabled='true'] {
color: theming.get-color-from-palette($foreground, disabled-button);

.mat-expansion-panel-header-title,
.mat-expansion-panel-header-description {
color: inherit;
}
@include sass-utils.current-selector-or-root() {
@include token-utils.create-token-values(tokens-mat-expansion.$prefix,
tokens-mat-expansion.get-color-tokens($config));
}
}

@mixin typography($config-or-theme) {
$config: typography.private-typography-to-2014-config(
theming.get-typography-config($config-or-theme));
.mat-expansion-panel-header {
font: {
family: typography-utils.font-family($config, subheading-1);
size: typography-utils.font-size($config, subheading-1);
weight: typography-utils.font-weight($config, subheading-1);
}
}

.mat-expansion-panel-content {
@include typography-utils.typography-level($config, body-1);
@include sass-utils.current-selector-or-root() {
@include token-utils.create-token-values(tokens-mat-expansion.$prefix,
tokens-mat-expansion.get-typography-tokens($config));
}
}

@mixin density($config-or-theme) {
$density-scale: theming.get-density-config($config-or-theme);
$expanded-height: compatibility.private-density-prop-value(
expansion-variables.$header-density-config, $density-scale, expanded-height);
$collapsed-height: compatibility.private-density-prop-value(
expansion-variables.$header-density-config, $density-scale, collapsed-height);

@include compatibility.private-density-legacy-compatibility() {
.mat-expansion-panel-header {
height: $collapsed-height;

&.mat-expanded {
height: $expanded-height;
}
}
@include sass-utils.current-selector-or-root() {
@include token-utils.create-token-values(tokens-mat-expansion.$prefix,
tokens-mat-expansion.get-density-tokens($density-scale));
}
}

Expand Down
65 changes: 61 additions & 4 deletions src/material/expansion/expansion-panel-header.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@use '@angular/cdk';
@use '../core/tokens/m2/mat/expansion' as tokens-mat-expansion;
@use '../core/tokens/token-utils';
@use './expansion-variables';

.mat-expansion-panel-header {
Expand All @@ -9,6 +11,44 @@
border-radius: inherit;
transition: height expansion-variables.$header-transition;

@include token-utils.use-tokens(
tokens-mat-expansion.$prefix, tokens-mat-expansion.get-token-slots()) {
@include token-utils.create-token-slot(height, header-collapsed-state-height);
@include token-utils.create-token-slot(font-family, header-text-font);
@include token-utils.create-token-slot(font-size, header-text-size);
@include token-utils.create-token-slot(font-weight, header-text-weight);
@include token-utils.create-token-slot(line-height, header-text-line-height);
@include token-utils.create-token-slot(letter-spacing, header-text-tracking);

&.mat-expanded {
@include token-utils.create-token-slot(height, header-expanded-state-height);
}

&[aria-disabled='true'] {
@include token-utils.create-token-slot(color, header-disabled-state-text-color);
}

&:not([aria-disabled='true']) {
cursor: pointer;

.mat-expansion-panel:not(.mat-expanded) &:hover {
@include token-utils.create-token-slot(background, header-hover-state-layer-color);

// Disable the hover on touch devices since it can appear like it is stuck. We can't use
// `@media (hover)` above, because the desktop support browser support isn't great.
@media (hover: none) {
@include token-utils.create-token-slot(background, container-background-color);
}
}

// The `.mat-expansion-panel` here is redundant, but we need the additional specificity.
.mat-expansion-panel &.cdk-keyboard-focused,
.mat-expansion-panel &.cdk-program-focused {
@include token-utils.create-token-slot(background, header-focus-state-layer-color);
}
}
}

// If the `NoopAnimationsModule` is used, disable the height transition.
&._mat-animation-noopable {
transition: none;
Expand All @@ -24,10 +64,6 @@
background: inherit;
}

&:not([aria-disabled='true']) {
cursor: pointer;
}

&.mat-expansion-toggle-indicator-before {
flex-direction: row-reverse;

Expand Down Expand Up @@ -68,6 +104,13 @@
}
}

.mat-expansion-panel-header-title {
@include token-utils.use-tokens(
tokens-mat-expansion.$prefix, tokens-mat-expansion.get-token-slots()) {
@include token-utils.create-token-slot(color, header-text-color);
}
}

.mat-expansion-panel-header-title,
.mat-expansion-panel-header-description {
display: flex;
Expand All @@ -80,10 +123,19 @@
margin-right: 0;
margin-left: 16px;
}

.mat-expansion-panel-header[aria-disabled='true'] & {
color: inherit;
}
}

.mat-expansion-panel-header-description {
flex-grow: 2;

@include token-utils.use-tokens(
tokens-mat-expansion.$prefix, tokens-mat-expansion.get-token-slots()) {
@include token-utils.create-token-slot(color, header-description-color);
}
}

// Creates the expansion indicator arrow. Done using ::after
Expand All @@ -96,6 +148,11 @@
padding: 3px;
transform: rotate(45deg);
vertical-align: middle;

@include token-utils.use-tokens(
tokens-mat-expansion.$prefix, tokens-mat-expansion.get-token-slots()) {
@include token-utils.create-token-slot(color, header-indicator-color);
}
}

@include cdk.high-contrast(active, off) {
Expand Down
Loading