Skip to content

Commit 47341ac

Browse files
authored
refactor(material/core): switch ripple to tokens theming API (#28042)
Switches the ripple element color to be controlled through a token.
1 parent c8ffcf5 commit 47341ac

File tree

4 files changed

+88
-21
lines changed

4 files changed

+88
-21
lines changed

src/material/core/_core-theme.scss

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@
5454
@include option-theme.typography($theme);
5555
@include optgroup-theme.typography($theme);
5656
@include pseudo-checkbox-theme.typography($theme);
57-
// TODO(mmalerba): add typography mixin for this.
58-
// @include ripple-theme.typography($config);
57+
@include ripple-theme.typography($theme);
5958
}
6059

6160
@mixin density($theme) {
6261
@include option-theme.density($theme);
6362
@include optgroup-theme.density($theme);
63+
@include ripple-theme.density($theme);
64+
6465
// TODO(mmalerba): add density mixins for these.
65-
// @include ripple-theme.density($density-scale);
66-
// @include pseudo-checkbox-theme.density($density-scale);
66+
// @include pseudo-checkbox-theme.density($theme);
6767
}
6868

6969
// Mixin that renders all of the core styles that depend on the theme.
Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
@use 'sass:meta';
1+
@use '../tokens/m2/mat/ripple' as tokens-mat-ripple;
2+
@use '../tokens/token-utils';
3+
@use '../style/sass-utils';
4+
25
@use '../theming/theming';
36
@use '../theming/inspection';
47

5-
@mixin base($theme) {
6-
// TODO(mmalerba): Move ripple base tokens here
7-
}
8+
@mixin base($theme) {}
89

9-
// Colors for the ripple elements.
1010
@mixin color($theme) {
11-
$foreground-base: inspection.get-theme-color($theme, foreground, base);
12-
$color-opacity: 0.1;
11+
@include sass-utils.current-selector-or-root() {
12+
@include token-utils.create-token-values(tokens-mat-ripple.$prefix,
13+
tokens-mat-ripple.get-color-tokens($theme));
14+
}
15+
}
1316

14-
.mat-ripple-element {
15-
// If the ripple color is resolves to a color *type*, we can use it directly, otherwise
16-
// (e.g. it resolves to a CSS variable) we fall back to using the color and setting an opacity.
17-
@if (meta.type-of($foreground-base) == color) {
18-
background-color: rgba($foreground-base, $color-opacity);
19-
}
20-
@else {
21-
background-color: $foreground-base;
22-
opacity: $color-opacity;
23-
}
17+
@mixin typography($theme) {
18+
@include sass-utils.current-selector-or-root() {
19+
@include token-utils.create-token-values(tokens-mat-ripple.$prefix,
20+
tokens-mat-ripple.get-typography-tokens($theme));
21+
}
22+
}
23+
24+
@mixin density($theme) {
25+
@include sass-utils.current-selector-or-root() {
26+
@include token-utils.create-token-values(tokens-mat-ripple.$prefix,
27+
tokens-mat-ripple.get-density-tokens($theme));
2428
}
2529
}
2630

@@ -30,5 +34,11 @@
3034
@if inspection.theme-has($theme, color) {
3135
@include color($theme);
3236
}
37+
@if inspection.theme-has($theme, density) {
38+
@include density($theme);
39+
}
40+
@if inspection.theme-has($theme, typography) {
41+
@include typography($theme);
42+
}
3343
}
3444
}

src/material/core/ripple/_ripple.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
@use '@angular/cdk';
2+
@use '../tokens/m2/mat/ripple' as tokens-mat-ripple;
3+
@use '../tokens/token-utils';
24

35
@mixin ripple() {
46
// The host element of an mat-ripple directive should always have a position of "absolute" or
@@ -35,6 +37,12 @@
3537
// the ripples aren't clipped when inside the shadow DOM (see #24028).
3638
transform: scale3d(0, 0, 0);
3739

40+
@include token-utils.use-tokens(
41+
tokens-mat-ripple.$prefix, tokens-mat-ripple.get-token-slots()) {
42+
// We have to emit a fallback value here, because some internal builds depend on it.
43+
background-color: var(#{token-utils.get-token-variable(color)}, rgba(#000, 0.1));
44+
}
45+
3846
// In high contrast mode the ripple is opaque, causing it to obstruct the content.
3947
@include cdk.high-contrast(active, off) {
4048
display: none;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
@use 'sass:meta';
2+
@use '../../token-utils';
3+
@use '../../../theming/inspection';
4+
@use '../../../style/sass-utils';
5+
6+
// The prefix used to generate the fully qualified name for tokens in this file.
7+
$prefix: (mat, ripple);
8+
9+
// Tokens that can't be configured through Angular Material's current theming API,
10+
// but may be in a future version of the theming API.
11+
@function get-unthemable-tokens() {
12+
@return ();
13+
}
14+
15+
// Tokens that can be configured through Angular Material's color theming API.
16+
@function get-color-tokens($theme) {
17+
$is-dark: inspection.get-theme-type($theme) == dark;
18+
$base: inspection.get-theme-color($theme, foreground, base);
19+
20+
// If the base is a color *type* we can use it directly in the `rgba` call below.
21+
// If it's anything else (e.g. a CSS variable) we fall back to using static colors
22+
// since we don't have a way of adjusting the opacity.
23+
$color: if(meta.type-of($base) == color, $base, if($is-dark, #fff, #000));
24+
25+
@return (
26+
color: rgba($color, 0.1),
27+
);
28+
}
29+
30+
// Tokens that can be configured through Angular Material's typography theming API.
31+
@function get-typography-tokens($theme) {
32+
@return ();
33+
}
34+
35+
// Tokens that can be configured through Angular Material's density theming API.
36+
@function get-density-tokens($theme) {
37+
@return ();
38+
}
39+
40+
// Combines the tokens generated by the above functions into a single map with placeholder values.
41+
// This is used to create token slots.
42+
@function get-token-slots() {
43+
@return sass-utils.deep-merge-all(
44+
get-unthemable-tokens(),
45+
get-color-tokens(token-utils.$placeholder-color-config),
46+
get-typography-tokens(token-utils.$placeholder-typography-config),
47+
get-density-tokens(token-utils.$placeholder-density-config)
48+
);
49+
}

0 commit comments

Comments
 (0)