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

Commit b4b954b

Browse files
patrickrodeeKenneth G. Franqueiro
authored andcommitted
feat(theme): Add support for arbitrary CSS vars with fallback (#4470)
(cherry picked from commit 0bfb393)
1 parent 59d5c14 commit b4b954b

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

packages/mdc-theme/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,38 @@ Property Name | Description
110110
`on-secondary` | A text/iconography color that is usable on top of secondary color
111111
`on-surface` | A text/iconography color that is usable on top of surface color
112112

113+
#### `mdc-theme-prop` with CSS Custom Properties
114+
115+
> **Note** The Sass map `$style` argument is intended *only* for use with color mixins.
116+
117+
The `mdc-theme-prop` mixin also accepts a Sass map for the `$style` argument. The map must contain the following fields:
118+
119+
Fields | Description
120+
--- | ---
121+
`varname` | The name of a CSS custom property
122+
`fallback` | A fallback value for the CSS custom property
123+
124+
For example, the following Sass...
125+
126+
```
127+
.foo {
128+
@include mdc-theme-prop(color, (
129+
varname: --foo-color,
130+
fallback: red,
131+
));
132+
}
133+
```
134+
135+
...will produce the following CSS...
136+
137+
```
138+
.foo {
139+
color: red;
140+
color: var(--foo-color, red);
141+
}
142+
```
143+
144+
The above output CSS will apply the `fallback` field's value for all supported browsers (including IE11) while allowing for CSS custom property use as a progressive enhancement. Browsers like IE11 that do not support CSS custom properties will apply the `color: red;` and ignore the `color: var(--foo-color, red);`. This argument type is intended for clients who need custom color application outside of the existing theme properties.
113145

114146
#### `mdc-theme-luminance($color)`
115147

packages/mdc-theme/_functions.scss

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,18 @@
6464
@function mdc-theme-contrast-tone($color) {
6565
@return if(mdc-theme-tone($color) == "dark", "light", "dark");
6666
}
67+
68+
@function mdc-theme-is-var-with-fallback_($style) {
69+
@return type-of($style) == "map" and map-has-key($style, "varname") and map-has-key($style, "fallback");
70+
}
71+
72+
@function mdc-theme-get-var-fallback_($style) {
73+
@return map-get($style, "fallback");
74+
}
75+
76+
@function mdc-theme-var_($style) {
77+
$var: map-get($style, "varname");
78+
$fallback: mdc-theme-get-var-fallback_($style);
79+
80+
@return var(#{$var}, $fallback);
81+
}

packages/mdc-theme/_mixins.scss

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
@import "@material/feature-targeting/functions";
2424
@import "@material/feature-targeting/mixins";
2525
@import "./variables";
26+
@import "./functions";
2627

2728
@mixin mdc-theme-core-styles($query: mdc-feature-all()) {
2829
$feat-color: mdc-feature-create-target($query, color);
@@ -67,7 +68,17 @@
6768
// $edgeOptOut controls whether to feature-detect around Edge to avoid emitting CSS variables for it,
6869
// intended for use in cases where interactions with pseudo-element styles cause problems due to Edge bugs.
6970
@mixin mdc-theme-prop($property, $style, $important: false, $edgeOptOut: false) {
70-
@if mdc-theme-is-valid-theme-prop-value_($style) {
71+
@if mdc-theme-is-var-with-fallback_($style) {
72+
@if $important {
73+
#{$property}: mdc-theme-get-var-fallback_($style) !important;
74+
/* @alternate */
75+
#{$property}: mdc-theme-var_($style) !important;
76+
} @else {
77+
#{$property}: mdc-theme-get-var-fallback_($style);
78+
/* @alternate */
79+
#{$property}: mdc-theme-var_($style);
80+
}
81+
} @else if mdc-theme-is-valid-theme-prop-value_($style) {
7182
@if $important {
7283
#{$property}: $style !important;
7384
} @else {

packages/mdc-theme/_variables.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ $mdc-theme-property-values: (
129129
//
130130
// NOTE: This function must be defined in _variables.scss instead of _functions.scss to avoid circular imports.
131131
@function mdc-theme-prop-value($style) {
132+
@if mdc-theme-is-var-with-fallback_($style) {
133+
@return mdc-theme-get-var-fallback_($style);
134+
}
135+
132136
@if mdc-theme-is-valid-theme-prop-value_($style) {
133137
@return $style;
134138
}

0 commit comments

Comments
 (0)