Skip to content

Commit cb3433a

Browse files
committed
refactor(material/core): remove unused/unnecessary utilities (#29444)
Removes a few utilities that either aren't necessary anymore or aren't being used. (cherry picked from commit 174cf42)
1 parent dcdc00b commit cb3433a

File tree

12 files changed

+149
-167
lines changed

12 files changed

+149
-167
lines changed

src/material/core/_core.scss

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
@use './ripple/ripple';
55
@use './style/elevation';
66
@use './focus-indicators/private';
7-
@use './mdc-helpers/mdc-helpers';
87

98
// Mixin that renders all of the core styles that are not theme-dependent.
109
@mixin core() {
@@ -20,24 +19,22 @@
2019
// user's content isn't inside of a `mat-sidenav-container`.
2120
@at-root {
2221
// Note: we need to emit fallback values here to avoid errors in internal builds.
23-
@include mdc-helpers.disable-mdc-fallback-declarations {
24-
@include token-utils.use-tokens(tokens-mat-app.$prefix, tokens-mat-app.get-token-slots()) {
25-
.mat-app-background {
26-
@include token-utils.create-token-slot(background-color, background-color, transparent);
27-
@include token-utils.create-token-slot(color, text-color, inherit);
28-
}
22+
@include token-utils.use-tokens(tokens-mat-app.$prefix, tokens-mat-app.get-token-slots()) {
23+
.mat-app-background {
24+
@include token-utils.create-token-slot(background-color, background-color, transparent);
25+
@include token-utils.create-token-slot(color, text-color, inherit);
26+
}
2927

30-
// Provides external CSS classes for each elevation value. Each CSS class is formatted as
31-
// `mat-elevation-z$zValue` where `$zValue` corresponds to the z-space to which the element
32-
// is elevated.
33-
@for $zValue from 0 through 24 {
34-
$selector: elevation.$prefix + $zValue;
35-
// We need the `mat-mdc-elevation-specific`, because some MDC mixins
36-
// come with elevation baked in and we don't have a way of removing it.
37-
.#{$selector}, .mat-mdc-elevation-specific.#{$selector} {
38-
@include token-utils.create-token-slot(box-shadow, 'elevation-shadow-level-#{$zValue}',
39-
none);
40-
}
28+
// Provides external CSS classes for each elevation value. Each CSS class is formatted as
29+
// `mat-elevation-z$z-value` where `$z-value` corresponds to the z-space to which the element
30+
// is elevated.
31+
@for $z-value from 0 through 24 {
32+
$selector: elevation.$prefix + $z-value;
33+
// We need the `mat-mdc-elevation-specific`, because some MDC mixins
34+
// come with elevation baked in and we don't have a way of removing it.
35+
.#{$selector}, .mat-mdc-elevation-specific.#{$selector} {
36+
@include token-utils.create-token-slot(box-shadow, 'elevation-shadow-level-#{$z-value}',
37+
none);
4138
}
4239
}
4340
}

src/material/core/mdc-helpers/_mdc-helpers.scss

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/material/core/tokens/_token-utils.scss

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
@use 'sass:map';
33
@use 'sass:meta';
44
@use 'sass:string';
5+
@use 'sass:color';
6+
@use 'sass:math';
57
@use '@material/tokens/v0_161' as mdc-tokens;
68
@use '../style/elevation';
79
@use '../style/sass-utils';
@@ -367,6 +369,66 @@ $_component-prefix: null;
367369
@return $result;
368370
}
369371

372+
/// Inherited function from MDC that computes which contrast tone to use on top of a color.
373+
/// This is used only in a narrow set of use cases when generating M2 button tokens to maintain
374+
/// backwards compatibility.
375+
/// @param {Color} $value Color for which we're calculating the contrast tone.
376+
/// @param {Boolean} $is-dark Whether the current theme is dark.
377+
/// @return {Map} Either `dark` or `light`.
378+
@function contrast-tone($value, $is-dark) {
379+
@if ($value == 'dark') {
380+
@return 'light';
381+
}
382+
383+
@if ($value == 'light') {
384+
@return 'dark';
385+
}
386+
387+
// Fallback if the app is using a non-color palette (e.g. CSS variable based).
388+
@if (meta.type-of($value) != 'color') {
389+
@return if($is-dark, 'light', 'dark');
390+
}
391+
392+
$minimum-contrast: 3.1;
393+
$light-contrast: _contrast($value, #fff);
394+
$dark-contrast: _contrast($value, rgba(0, 0, 0, 0.87));
395+
396+
@if ($light-contrast < $minimum-contrast) and ($dark-contrast > $light-contrast) {
397+
@return 'dark';
398+
}
399+
400+
@return 'light';
401+
}
402+
403+
@function _linear-channel-value($channel-value) {
404+
$normalized-channel-value: math.div($channel-value, 255);
405+
406+
@if ($normalized-channel-value < 0.03928) {
407+
@return math.div($normalized-channel-value, 12.92);
408+
}
409+
410+
@return math.pow(math.div($normalized-channel-value + 0.055, 1.055), 2.4);
411+
}
412+
413+
// Calculate the luminance for a color.
414+
// See https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests
415+
@function _luminance($color) {
416+
$red: _linear-channel-value(color.red($color));
417+
$green: _linear-channel-value(color.green($color));
418+
$blue: _linear-channel-value(color.blue($color));
419+
420+
@return 0.2126 * $red + 0.7152 * $green + 0.0722 * $blue;
421+
}
422+
423+
// Calculate the contrast ratio between two colors.
424+
// See https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests
425+
@function _contrast($back, $front) {
426+
$back-lum: _luminance($back) + 0.05;
427+
$fore-lum: _luminance($front) + 0.05;
428+
429+
@return math.div(math.max($back-lum, $fore-lum), math.min($back-lum, $fore-lum));
430+
}
431+
370432
/// Verifies that the token overrides exist and are used in one of the given token maps.
371433
@mixin _validate-token-overrides($overrides: (), $token-maps) {
372434
$valid-token-names: ();

src/material/core/tokens/m2/mat/_fab-small.scss

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
@use '../../../theming/theming';
44
@use '../../../theming/inspection';
55
@use '../../../style/sass-utils';
6-
@use '../../../mdc-helpers/mdc-helpers';
76

87
// The prefix used to generate the fully qualified name for tokens in this file.
98
$prefix: (mat, fab-small);
@@ -63,7 +62,7 @@ $prefix: (mat, fab-small);
6362
@if (token-utils.$private-is-internal-build or meta.type-of($contrast-color) != 'color') {
6463
$is-dark: inspection.get-theme-type($theme) == dark;
6564
$container-color: inspection.get-theme-color($theme, $palette-name);
66-
$contrast-tone: mdc-helpers.variable-safe-contrast-tone($container-color, $is-dark);
65+
$contrast-tone: token-utils.contrast-tone($container-color, $is-dark);
6766
$color: if($contrast-tone == 'dark', #000, #fff);
6867
$foreground-color: $color;
6968
$state-layer-color: $color;

src/material/core/tokens/m2/mat/_fab.scss

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
@use '../../../theming/theming';
44
@use '../../../theming/inspection';
55
@use '../../../style/sass-utils';
6-
@use '../../../mdc-helpers/mdc-helpers';
76

87
// The prefix used to generate the fully qualified name for tokens in this file.
98
$prefix: (mat, fab);
@@ -63,7 +62,7 @@ $prefix: (mat, fab);
6362
@if (token-utils.$private-is-internal-build or meta.type-of($contrast-color) != 'color') {
6463
$is-dark: inspection.get-theme-type($theme) == dark;
6564
$container-color: inspection.get-theme-color($theme, $palette-name);
66-
$contrast-tone: mdc-helpers.variable-safe-contrast-tone($container-color, $is-dark);
65+
$contrast-tone: token-utils.contrast-tone($container-color, $is-dark);
6766
$color: if($contrast-tone == 'dark', #000, #fff);
6867
$foreground-color: $color;
6968
$state-layer-color: $color;

src/material/core/tokens/m2/mat/_filled-button.scss

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
@use '../../../theming/theming';
44
@use '../../../theming/inspection';
55
@use '../../../style/sass-utils';
6-
@use '../../../mdc-helpers/mdc-helpers';
76

87
// The prefix used to generate the fully qualified name for tokens in this file.
98
$prefix: (mat, filled-button);
@@ -60,7 +59,7 @@ $prefix: (mat, filled-button);
6059
@if (token-utils.$private-is-internal-build or meta.type-of($contrast-color) != 'color') {
6160
$is-dark: inspection.get-theme-type($theme) == dark;
6261
$container-color: inspection.get-theme-color($theme, $palette-name);
63-
$contrast-tone: mdc-helpers.variable-safe-contrast-tone($container-color, $is-dark);
62+
$contrast-tone: token-utils.contrast-tone($container-color, $is-dark);
6463
$color: if($contrast-tone == 'dark', #000, #fff);
6564
$state-layer-color: $color;
6665
$ripple-color: rgba($color, 0.1);

src/material/core/tokens/m2/mat/_protected-button.scss

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
@use '../../../theming/theming';
44
@use '../../../theming/inspection';
55
@use '../../../style/sass-utils';
6-
@use '../../../mdc-helpers/mdc-helpers';
76

87
// The prefix used to generate the fully qualified name for tokens in this file.
98
$prefix: (mat, protected-button);
@@ -60,7 +59,7 @@ $prefix: (mat, protected-button);
6059
@if (token-utils.$private-is-internal-build or meta.type-of($contrast-color) != 'color') {
6160
$is-dark: inspection.get-theme-type($theme) == dark;
6261
$container-color: inspection.get-theme-color($theme, $palette-name);
63-
$contrast-tone: mdc-helpers.variable-safe-contrast-tone($container-color, $is-dark);
62+
$contrast-tone: token-utils.contrast-tone($container-color, $is-dark);
6463
$color: if($contrast-tone == 'dark', #000, #fff);
6564
$state-layer-color: $color;
6665
$ripple-color: rgba($color, 0.1);

src/material/core/tokens/m2/mdc/_checkbox.scss

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
@use '../../../theming/theming';
55
@use '../../../theming/inspection';
66
@use '../../../style/sass-utils';
7-
@use '../../../mdc-helpers/mdc-helpers';
87
@use '../../token-utils';
98

109
// The prefix used to generate the fully qualified name for tokens in this file.
@@ -61,7 +60,7 @@ $prefix: (mdc, checkbox);
6160
// Ideally we would derive all values directly from the theme, but it causes a lot of regressions
6261
// internally. For now we fall back to the old hardcoded behavior only for internal apps.
6362
@if (token-utils.$private-is-internal-build) {
64-
$contrast-tone: mdc-helpers.variable-safe-contrast-tone($palette-selected, $is-dark);
63+
$contrast-tone: token-utils.contrast-tone($palette-selected, $is-dark);
6564
$selected-checkmark-color: if($contrast-tone == 'dark', #000, #fff);
6665
}
6766
@else {

src/material/core/tokens/m2/mdc/_filled-button.scss

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
@use '../../../style/sass-utils';
44
@use '../../../theming/inspection';
55
@use '../../../theming/theming';
6-
@use '../../../mdc-helpers/mdc-helpers';
76

87
// The prefix used to generate the fully qualified name for tokens in this file.
98
$prefix: (mdc, filled-button);
@@ -71,7 +70,7 @@ $prefix: (mdc, filled-button);
7170
// internally. For now we fall back to the old hardcoded behavior only for internal apps.
7271
@if (token-utils.$private-is-internal-build) {
7372
$is-dark: inspection.get-theme-type($theme) == dark;
74-
$contrast-tone: mdc-helpers.variable-safe-contrast-tone($container-color, $is-dark);
73+
$contrast-tone: token-utils.contrast-tone($container-color, $is-dark);
7574
$label-text-color: if($contrast-tone == 'dark', #000, #fff);
7675
}
7776
@else {

src/material/core/tokens/m2/mdc/_protected-button.scss

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
@use '../../../style/elevation';
55
@use '../../../theming/inspection';
66
@use '../../../theming/theming';
7-
@use '../../../mdc-helpers/mdc-helpers';
87

98
// The prefix used to generate the fully qualified name for tokens in this file.
109
$prefix: (mdc, protected-button);
@@ -77,7 +76,7 @@ $prefix: (mdc, protected-button);
7776
// internally. For now we fall back to the old hardcoded behavior only for internal apps.
7877
@if (token-utils.$private-is-internal-build) {
7978
$is-dark: inspection.get-theme-type($theme) == dark;
80-
$contrast-tone: mdc-helpers.variable-safe-contrast-tone($container-color, $is-dark);
79+
$contrast-tone: token-utils.contrast-tone($container-color, $is-dark);
8180
$label-text-color: if($contrast-tone == 'dark', #000, #fff);
8281
}
8382
@else {

src/material/dialog/dialog.scss

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
@use '@angular/cdk';
22
@use '../core/tokens/m2/mdc/dialog' as tokens-mdc-dialog;
33
@use '../core/tokens/m2/mat/dialog' as tokens-mat-dialog;
4-
@use '../core/mdc-helpers/mdc-helpers';
54
@use '../core/tokens/token-utils';
65
@use '../core/style/variables';
76

@@ -18,20 +17,14 @@ $mat-dialog-button-horizontal-margin: 8px !default;
1817
$_emit-fallbacks: true;
1918

2019
@mixin _use-mat-tokens {
21-
@include mdc-helpers.disable-mdc-fallback-declarations {
22-
@include token-utils.use-tokens(tokens-mat-dialog.$prefix,
23-
tokens-mat-dialog.get-token-slots()) {
24-
@content;
25-
}
20+
@include token-utils.use-tokens(tokens-mat-dialog.$prefix, tokens-mat-dialog.get-token-slots()) {
21+
@content;
2622
}
2723
}
2824

2925
@mixin _use-mdc-tokens {
30-
@include mdc-helpers.disable-mdc-fallback-declarations {
31-
@include token-utils.use-tokens(tokens-mdc-dialog.$prefix,
32-
tokens-mdc-dialog.get-token-slots()) {
33-
@content;
34-
}
26+
@include token-utils.use-tokens(tokens-mdc-dialog.$prefix, tokens-mdc-dialog.get-token-slots()) {
27+
@content;
3528
}
3629
}
3730

0 commit comments

Comments
 (0)