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

Commit b47fe7d

Browse files
authored
fix(theme): Allow CSS variables to be passed to mdc-theme-prop (#3086)
1 parent 2c1caa3 commit b47fe7d

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

packages/mdc-theme/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ Determines whether to use light or dark text on top of a given color.
146146
@debug mdc-theme-contrast-tone(#9c27b0); // light
147147
```
148148

149-
#### `mdc-theme-prop-value($property)`
149+
#### `mdc-theme-prop-value($style)`
150150

151-
If `$property` is a literal color value (e.g., `blue`, `#fff`), it is returned verbatim. Otherwise, the value of the
152-
corresponding theme property (from `$mdc-theme-property-values`) is returned. If `$property` is not a color and no
153-
such theme property exists, an error is thrown.
151+
If `$style` is a color (a literal color value, `currentColor`, or a CSS custom property), it is returned verbatim.
152+
Otherwise, `$style` is treated as a theme property name, and the corresponding value from `$mdc-theme-property-values`
153+
is returned. If this also fails, an error is thrown.
154154

155155
This is mainly useful in situations where `mdc-theme-prop` cannot be used directly (e.g., `box-shadow`).
156156

packages/mdc-theme/_mixins.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
// Applies the correct theme color style to the specified property.
2020
// $property is typically color or background-color, but can be any CSS property that accepts color values.
21-
// $style should be one of the map keys in $mdc-theme-property-values (_variables.scss), or a literal color value.
21+
// $style should be one of the map keys in $mdc-theme-property-values (_variables.scss), or a color value.
2222
// $edgeOptOut controls whether to feature-detect around Edge to avoid emitting CSS variables for it,
2323
// intended for use in cases where interactions with pseudo-element styles cause problems due to Edge bugs.
2424
@mixin mdc-theme-prop($property, $style, $important: false, $edgeOptOut: false) {
25-
@if type-of($style) == "color" or $style == "currentColor" {
25+
@if mdc-theme-is-valid-theme-prop-value_($style) {
2626
@if $important {
2727
#{$property}: $style !important;
2828
} @else {

packages/mdc-theme/_variables.scss

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,28 +98,28 @@ $mdc-theme-property-values: (
9898
text-icon-on-dark: mdc-theme-ink-color-for-fill_(icon, dark)
9999
);
100100

101-
// If `$property` is a literal color value (e.g., `blue`, `#fff`), it is returned verbatim. Otherwise, the value of the
102-
// corresponding theme property (from `$mdc-theme-property-values`) is returned. If `$property` is not a color and no
103-
// such theme property exists, an error is thrown.
101+
// If `$style` is a color (a literal color value, `currentColor`, or a CSS custom property), it is returned verbatim.
102+
// Otherwise, `$style` is treated as a theme property name, and the corresponding value from
103+
// `$mdc-theme-property-values` is returned. If this also fails, an error is thrown.
104104
//
105105
// This is mainly useful in situations where `mdc-theme-prop` cannot be used directly (e.g., `box-shadow`).
106106
//
107107
// Examples:
108108
//
109-
// 1. mdc-theme-prop-value(primary) => "#3f51b5"
109+
// 1. mdc-theme-prop-value(primary) => "#6200ee"
110110
// 2. mdc-theme-prop-value(blue) => "blue"
111111
//
112112
// NOTE: This function must be defined in _variables.scss instead of _functions.scss to avoid circular imports.
113-
@function mdc-theme-prop-value($property) {
114-
@if type-of($property) == "color" or $property == "currentColor" {
115-
@return $property;
113+
@function mdc-theme-prop-value($style) {
114+
@if mdc-theme-is-valid-theme-prop-value_($style) {
115+
@return $style;
116116
}
117117

118-
@if not map-has-key($mdc-theme-property-values, $property) {
119-
@error "Invalid theme property: '#{$property}'. Choose one of: #{map-keys($mdc-theme-property-values)}";
118+
@if not map-has-key($mdc-theme-property-values, $style) {
119+
@error "Invalid theme property: '#{$style}'. Choose one of: #{map-keys($mdc-theme-property-values)}";
120120
}
121121

122-
@return map-get($mdc-theme-property-values, $property);
122+
@return map-get($mdc-theme-property-values, $style);
123123
}
124124

125125
// NOTE: This function must be defined in _variables.scss instead of _functions.scss to avoid circular imports.
@@ -133,3 +133,8 @@ $mdc-theme-property-values: (
133133

134134
@return map-get($color-map-for-tone, $text-style);
135135
}
136+
137+
// NOTE: This function is depended upon by mdc-theme-prop-value (above) and thus must be defined in this file.
138+
@function mdc-theme-is-valid-theme-prop-value_($style) {
139+
@return type-of($style) == "color" or $style == "currentColor" or str_slice($style, 1, 4) == "var(";
140+
}

0 commit comments

Comments
 (0)