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

Commit ec6b68b

Browse files
abhiomkarcopybara-github
authored andcommitted
refactor(ripple): Add CSS custom property support to MDC ripple
PiperOrigin-RevId: 341078189
1 parent 0e52860 commit ec6b68b

File tree

4 files changed

+110
-28
lines changed

4 files changed

+110
-28
lines changed

packages/mdc-ripple/_ripple-theme.scss

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
@use '@material/animation/variables' as variables2;
3030
@use '@material/base/mixins' as base-mixins;
3131
@use '@material/feature-targeting/feature-targeting';
32+
@use '@material/theme/css';
33+
@use '@material/theme/custom-properties';
3234
@use '@material/theme/theme';
3335
@use '@material/theme/theme-color';
3436

@@ -70,11 +72,18 @@ $pressed-light-ink-opacity: 0.32 !default;
7072
) {
7173
$feat-color: feature-targeting.create-target($query, color);
7274

75+
@if not custom-properties.is-custom-prop($color) {
76+
$color: custom-properties.create(
77+
--mdc-ripple-color,
78+
theme-color.get-custom-property($color)
79+
);
80+
}
81+
7382
#{$ripple-target}::before,
7483
#{$ripple-target}::after {
7584
@include feature-targeting.targets($feat-color) {
7685
@if color.alpha(theme-color.prop-value($color)) > 0 {
77-
@include theme.prop(background-color, $color);
86+
@include theme.property(background-color, $color);
7887
} @else {
7988
// If a color with 0 alpha is specified, don't render the ripple pseudo-elements at all.
8089
// This avoids unnecessary transitions and overflow.
@@ -131,11 +140,15 @@ $pressed-light-ink-opacity: 0.32 !default;
131140
$feat-color: feature-targeting.create-target($query, color);
132141

133142
// Background wash styles, for both CSS-only and upgraded stateful surfaces
134-
&:hover {
143+
&:hover,
144+
&.mdc-ripple-surface--hover {
135145
#{$ripple-target}::before {
136146
// Opacity falls under color because the chosen opacity is color-dependent in typical usage
137147
@include feature-targeting.targets($feat-color) {
138-
opacity: $opacity;
148+
@include theme.property(
149+
opacity,
150+
custom-properties.create(--mdc-ripple-hover-opacity, $opacity)
151+
);
139152
}
140153
}
141154
}
@@ -192,7 +205,10 @@ $pressed-light-ink-opacity: 0.32 !default;
192205
}
193206

194207
@include feature-targeting.targets($feat-color) {
195-
opacity: $opacity;
208+
@include theme.property(
209+
opacity,
210+
custom-properties.create(--mdc-ripple-focus-opacity, $opacity)
211+
);
196212
}
197213
}
198214

@@ -222,15 +238,18 @@ $pressed-light-ink-opacity: 0.32 !default;
222238

223239
// Opacity falls under color because the chosen opacity is color-dependent in typical usage
224240
@include feature-targeting.targets($feat-color) {
225-
opacity: $opacity;
241+
@include theme.property(
242+
opacity,
243+
custom-properties.create(--mdc-ripple-press-opacity, $opacity)
244+
);
226245
}
227246
}
228247
}
229248
}
230249

231250
&.mdc-ripple-upgraded {
232251
@include feature-targeting.targets($feat-color) {
233-
--mdc-ripple-fg-opacity: #{$opacity};
252+
--mdc-ripple-fg-opacity: var(--mdc-ripple-press-opacity, #{$opacity});
234253
}
235254
}
236255
}
@@ -270,7 +289,13 @@ $pressed-light-ink-opacity: 0.32 !default;
270289
#{$ripple-target}::before {
271290
// Opacity falls under color because the chosen opacity is color-dependent.
272291
@include feature-targeting.targets($feat-color) {
273-
opacity: $activated-opacity;
292+
@include theme.property(
293+
opacity,
294+
custom-properties.create(
295+
--mdc-ripple-activated-opacity,
296+
$activated-opacity
297+
)
298+
);
274299
}
275300
}
276301

@@ -300,7 +325,13 @@ $pressed-light-ink-opacity: 0.32 !default;
300325
#{$ripple-target}::before {
301326
// Opacity falls under color because the chosen opacity is color-dependent.
302327
@include feature-targeting.targets($feat-color) {
303-
opacity: $selected-opacity;
328+
@include theme.property(
329+
opacity,
330+
custom-properties.create(
331+
--mdc-ripple-selected-opacity,
332+
$selected-opacity
333+
)
334+
);
304335
}
305336
}
306337

packages/mdc-ripple/_ripple.scss

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,48 @@
2828
@use '@material/animation/functions' as functions2;
2929
@use '@material/animation/variables' as variables2;
3030
@use '@material/base/mixins' as base-mixins;
31+
@use '@material/theme/custom-properties';
3132
@use '@material/feature-targeting/feature-targeting';
3233
@use '@material/theme/theme';
3334
@use '@material/theme/theme-color';
3435
@use './ripple-theme';
3536

36-
@mixin core-styles($query: feature-targeting.all()) {
37-
// postcss-bem-linter: define ripple-surface
38-
37+
@mixin core-styles(
38+
$query: feature-targeting.all(),
39+
// TODO(b/151931961): Remove this param once resolved.
40+
$include-will-change: true
41+
) {
3942
$feat-structure: feature-targeting.create-target($query, structure);
4043

4144
.mdc-ripple-surface {
42-
@include surface($query: $query);
45+
@include surface(
46+
$include-will-change: $include-will-change,
47+
$query: $query
48+
);
4349
@include ripple-theme.states($query: $query);
4450
@include radius-bounded($query: $query);
4551
@include surface-styles($query: $query);
52+
}
4653

47-
&[data-mdc-ripple-is-unbounded] {
48-
@include radius-unbounded($query: $query);
49-
@include unbounded-styles($query: $query);
50-
}
51-
52-
&--primary {
53-
@include ripple-theme.states(primary, $query: $query);
54-
}
55-
56-
&--accent {
57-
@include ripple-theme.states(secondary, $query: $query);
58-
}
54+
.mdc-ripple-surface[data-mdc-ripple-is-unbounded],
55+
.mdc-ripple-upgraded--unbounded {
56+
@include radius-unbounded($query: $query);
57+
@include unbounded-styles($query: $query);
5958
}
59+
}
6060

61-
// postcss-bem-linter: end
61+
/// Sets all states (including hover, focus, press, activated and selected) with
62+
/// given color as base color.
63+
///
64+
/// This mixin is for internal use only. Use `ripple-theme.states($color)` mixin
65+
/// to set interactive states (hover, focus & press) color.
66+
///
67+
/// @param {Color|String} $color - Target base color. Can be valid CSS color or
68+
/// a color string literal (i.e., `primary`, `secondary`, etc).
69+
@mixin states-for-color($color, $query: feature-targeting.all()) {
70+
@include ripple-theme.states($color, $query: $query);
71+
@include ripple-theme.states-activated($color, $query: $query);
72+
@include ripple-theme.states-selected($color, $query: $query);
6273
}
6374

6475
@mixin surface-styles($query: feature-targeting.all()) {
@@ -133,7 +144,20 @@
133144
}
134145

135146
@include feature-targeting.targets($feat-structure) {
136-
z-index: 1; // Ensure that the ripple wash for hover/focus states is displayed on top of positioned child elements
147+
// Ensure that the ripple wash for hover/focus states is displayed on top of positioned child elements
148+
@include theme.property(
149+
z-index,
150+
custom-properties.create(--mdc-ripple-z-index, 1)
151+
);
152+
}
153+
}
154+
155+
#{$ripple-target}::after {
156+
@include feature-targeting.targets($feat-structure) {
157+
@include theme.property(
158+
z-index,
159+
custom-properties.create(--mdc-ripple-z-index, 0)
160+
);
137161
}
138162
}
139163

packages/mdc-theme/_theme-color.scss

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,31 @@ $property-values: (
246246
@function text-emphasis($emphasis) {
247247
@return map.get($text-emphasis, $emphasis);
248248
}
249+
250+
///
251+
/// @param {Color|String} Color property key name (i.e., `primary`, `secondary`,
252+
/// etc).
253+
/// @return Returns custom property map containing CSS custom property and
254+
/// fallback value (i.e., (varname: ..., fallback: ...). Returns color if
255+
/// valid color value is provided. Throws error otherwise.
256+
/// @examples
257+
/// 1. get-custom-property(primary)
258+
/// => (varname: --mdc-theme-primary, fallback: #6200ee)
259+
///
260+
/// 2. get-custom-property(#fff)
261+
/// => #fff
262+
///
263+
@function get-custom-property($color) {
264+
@if custom-properties.is-custom-prop($color) or
265+
is-valid-theme-prop-value_($color)
266+
{
267+
@return $color;
268+
} @else if map.has-key($property-values, $color) {
269+
@return custom-properties.create(
270+
--mdc-theme-#{$color},
271+
map.get($property-values, $color)
272+
);
273+
} @else {
274+
@error "Invalid theme property: '#{$style}'. Choose one of: #{map.keys($property-values)}";
275+
}
276+
}

packages/mdc-theme/_theme.scss

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@
121121
);
122122
} @else if map.has-key(theme-color.$property-values, $value) {
123123
// $value is a theme property String
124-
$fallback: map.get(theme-color.$property-values, $value);
125-
$custom-prop: custom-properties.create(--mdc-theme-#{$value}, $fallback);
124+
$custom-prop: theme-color.get-custom-property($value);
126125
@include custom-properties.apply(
127126
$property,
128127
$custom-prop,

0 commit comments

Comments
 (0)