Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 57581ed

Browse files
authored
feat(theme): Add new tone mixins and deprecate old one (#1546)
The old mixin name is confusing. It's also cumbersome to use if you just want to get the tone of a color directly, and you don't care about the text color that pairs with it.
1 parent 5a777af commit 57581ed

File tree

6 files changed

+43
-21
lines changed

6 files changed

+43
-21
lines changed

.stylelintrc.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ rules:
241241
- if
242242
- mixin
243243
- return
244+
- warn
244245
# Disallow "@extend" in scss.
245246
# http://csswizardry.com/2016/02/mixins-better-for-performance/
246247
# http://vanseodesign.com/css/sass-mixin-or-extend/
@@ -259,7 +260,7 @@ rules:
259260
selector-no-qualifying-type: true
260261
# In general, we should not be modifying elements within our components, since we can't
261262
# predict the use cases in which users would add a certain type of element into a component.
262-
selector-max-type:
263+
selector-max-type:
263264
- 0
264265
- ignoreTypes:
265266
- /fieldset/

packages/mdc-button/_mixins.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
@import "./variables";
2323

2424
@mixin mdc-button-filled-accessible($container-fill-color) {
25-
$light-or-dark: mdc-theme-light-or-dark($container-fill-color);
25+
$fill-tone: mdc-theme-tone($container-fill-color);
2626

2727
@include mdc-button-container-fill-color($container-fill-color);
2828

29-
@if ($light-or-dark == "light") {
29+
@if ($fill-tone == "dark") {
3030
@include mdc-button-ink-color(text-primary-on-dark);
3131
@include mdc-ripple-color(text-primary-on-dark, $mdc-filled-button-ripple-opacity);
3232
} @else {

packages/mdc-fab/_mixins.scss

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121
@import "@material/theme/mixins";
2222

2323
@mixin mdc-fab-accessible($container-color) {
24-
@include mdc-fab-container-color($container-color);
24+
$fill-tone: mdc-theme-tone($container-color);
2525

26-
// Calculate whether to use dark or light text on top of given color.
27-
$light-or-dark-text: mdc-theme-light-or-dark($container-color);
26+
@include mdc-fab-container-color($container-color);
2827

29-
@if ($light-or-dark-text == "light") {
28+
@if ($fill-tone == "dark") {
3029
@include mdc-fab-ink-color(text-primary-on-dark);
3130
@include mdc-ripple-color(text-primary-on-dark, $mdc-ripple-pressed-light-ink-opacity);
3231
} @else {

packages/mdc-theme/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,20 @@ Calculates the contrast ratio between two colors.
182182
@debug mdc-theme-contrast(#9c27b0, #000); // 3.33071
183183
```
184184

185-
#### `mdc-theme-light-or-dark($color)`
185+
#### `mdc-theme-tone($color)`
186+
187+
Determines whether the given color is "light" or "dark".
188+
189+
```scss
190+
@debug mdc-theme-tone(#9c27b0); // dark
191+
```
192+
193+
#### `mdc-theme-contrast-tone($color)`
186194

187195
Determines whether to use light or dark text on top of a given color.
188196

189197
```scss
190-
@debug mdc-theme-light-or-dark(#9c27b0); // light
198+
@debug mdc-theme-contrast-tone(#9c27b0); // light
191199
```
192200

193201
#### `mdc-theme-light-variant($color)` and `mdc-theme-dark-variant($color)`

packages/mdc-theme/_functions.scss

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,35 @@ $_mdc-theme-tonal-offset: 7%;
4040
@return max($backLum, $foreLum) / min($backLum, $foreLum);
4141
}
4242

43-
// Determine whether to use dark or light text on top of given color.
44-
// Returns "dark" for dark text and "light" for light text.
45-
@function mdc-theme-light-or-dark($color) {
43+
// Determine whether the color is "light" or "dark".
44+
@function mdc-theme-tone($color) {
4645
$minimumContrast: 3.1;
4746

4847
$lightContrast: mdc-theme-contrast($color, white);
4948
$darkContrast: mdc-theme-contrast($color, rgba(black, .87));
5049

5150
@if ($lightContrast < $minimumContrast) and ($darkContrast > $lightContrast) {
52-
@return "dark";
53-
} @else {
5451
@return "light";
52+
} @else {
53+
@return "dark";
5554
}
5655
}
5756

57+
// Determine whether to use dark or light text on top of given color to meet accessibility standards for contrast.
58+
// Returns "dark" if the given color is light and "light" if the given color is dark.
59+
@function mdc-theme-contrast-tone($color) {
60+
@return if(mdc-theme-tone($color) == "dark", "light", "dark");
61+
}
62+
63+
// DEPRECATED. Use mdc-theme-contrast-tone instead.
64+
@function mdc-theme-light-or-dark($color) {
65+
// stylelint-disable indentation
66+
@warn "The 'mdc-theme-light-or-dark' mixin is DEPRECATED and will be REMOVED in a future version. " +
67+
"Please use 'mdc-theme-contrast-tone' or 'mdc-theme-tone' (as applicable) instead.";
68+
@return mdc-theme-contrast-tone($color);
69+
// stylelint-enable indentation
70+
}
71+
5872
// lighten() and darken() require values to be between 0% and 100%.
5973
@function mdc-theme-clamp-percentage_($percentage) {
6074
@return max(0%, min(100%, $percentage));

packages/mdc-theme/_variables.scss

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ $mdc-theme-background: #fff !default; // White
3939
// Which set of text colors to use for each main theme color (light or dark).
4040
//
4141

42-
$mdc-theme-primary-tone: mdc-theme-light-or-dark($mdc-theme-primary) !default;
43-
$mdc-theme-primary-light-tone: mdc-theme-light-or-dark($mdc-theme-primary-light) !default;
44-
$mdc-theme-primary-dark-tone: mdc-theme-light-or-dark($mdc-theme-primary-dark) !default;
42+
$mdc-theme-primary-tone: mdc-theme-contrast-tone($mdc-theme-primary) !default;
43+
$mdc-theme-primary-light-tone: mdc-theme-contrast-tone($mdc-theme-primary-light) !default;
44+
$mdc-theme-primary-dark-tone: mdc-theme-contrast-tone($mdc-theme-primary-dark) !default;
4545

46-
$mdc-theme-secondary-tone: mdc-theme-light-or-dark($mdc-theme-secondary) !default;
47-
$mdc-theme-secondary-light-tone: mdc-theme-light-or-dark($mdc-theme-secondary-light) !default;
48-
$mdc-theme-secondary-dark-tone: mdc-theme-light-or-dark($mdc-theme-secondary-dark) !default;
46+
$mdc-theme-secondary-tone: mdc-theme-contrast-tone($mdc-theme-secondary) !default;
47+
$mdc-theme-secondary-light-tone: mdc-theme-contrast-tone($mdc-theme-secondary-light) !default;
48+
$mdc-theme-secondary-dark-tone: mdc-theme-contrast-tone($mdc-theme-secondary-dark) !default;
4949

50-
$mdc-theme-background-tone: mdc-theme-light-or-dark($mdc-theme-background) !default;
50+
$mdc-theme-background-tone: mdc-theme-contrast-tone($mdc-theme-background) !default;
5151

5252
//
5353
// Text colors according to light vs dark and text type.

0 commit comments

Comments
 (0)