Skip to content

Commit 4acbae7

Browse files
committed
refactor(material-experimental/mdc-button): switch to new theming API (#24201)
* refactor(material-experimental/mdc-button): switch to new theming API Switches the MDC-based buttons to the new token-based theming API. * refactor(material-experimental/mdc-button): reduce bundle size Reworks the MDC buttons to reduce their bundle size. (cherry picked from commit 4ee7159)
1 parent 95c1889 commit 4acbae7

11 files changed

+349
-207
lines changed

src/material-experimental/mdc-button/_button-base.scss

+28-15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@use 'sass:map';
12
@use '@material/touch-target' as mdc-touch-target;
23
@use '../mdc-helpers/mdc-helpers';
34
@use '../../material/core/style/layout-common';
@@ -8,22 +9,10 @@
89
// ripple and state container so that they fill the button, match the border radius, and avoid
910
// pointer events.
1011
@mixin mat-private-button-interactive() {
11-
.mdc-button__ripple::before, .mdc-button__ripple::after,
12-
.mdc-fab__ripple::before, .mdc-fab__ripple::after {
13-
content: '';
14-
pointer-events: none;
15-
position: absolute;
16-
top: 0;
17-
right: 0;
18-
bottom: 0;
19-
left: 0;
20-
opacity: 0;
21-
border-radius: inherit;
22-
@content;
23-
}
24-
2512
// The ripple container should match the bounds of the entire button.
26-
.mat-mdc-button-ripple, .mdc-button__ripple, .mdc-fab__ripple {
13+
.mat-mdc-button-ripple,
14+
.mat-mdc-button-persistent-ripple,
15+
.mat-mdc-button-persistent-ripple::before {
2716
@include layout-common.fill;
2817

2918
// Disable pointer events for the ripple container and state overlay because the container
@@ -38,6 +27,17 @@
3827
border-radius: inherit;
3928
}
4029

30+
// We use ::before so that we can reuse some of MDC's theming.
31+
.mat-mdc-button-persistent-ripple::before {
32+
content: '';
33+
opacity: 0;
34+
background-color: var(--mat-mdc-button-persistent-ripple-color);
35+
}
36+
37+
.mat-ripple-element {
38+
background-color: var(--mat-mdc-button-ripple-color);
39+
}
40+
4141
// The content should appear over the state and ripple layers, otherwise they may adversely affect
4242
// the accessibility of the text content.
4343
.mdc-button__label {
@@ -59,6 +59,7 @@
5959
&[disabled] {
6060
cursor: default;
6161
pointer-events: none;
62+
@content;
6263
}
6364
}
6465

@@ -75,3 +76,15 @@
7576
$query: mdc-helpers.$mat-base-styles-query);
7677
}
7778
}
79+
80+
// Changes a button theme to exclude the ripple styles.
81+
@function mat-private-button-remove-ripple($theme) {
82+
@return map.merge($theme, (
83+
focus-state-layer-color: null,
84+
focus-state-layer-opacity: null,
85+
hover-state-layer-color: null,
86+
hover-state-layer-opacity: null,
87+
pressed-state-layer-color: null,
88+
pressed-state-layer-opacity: null,
89+
));
90+
}

src/material-experimental/mdc-button/_button-theme-private.scss

+35-28
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
1+
@use 'sass:map';
2+
@use '@material/ripple/ripple-theme' as mdc-ripple-theme;
13
@use '@material/theme/theme-color' as mdc-theme-color;
2-
@use '@material/theme/theme' as mdc-theme;
34
@use '../../material/core/ripple/ripple-theme';
45

5-
// Selector for the element that has a background color and opacity applied to its ::before and
6-
// ::after for state interactions (hover, active, focus). Their API calls this their
7-
// "ripple target", but we do not use it as our ripple, just state color.
8-
$button-state-target: '.mdc-button__ripple';
9-
$fab-state-target: '.mdc-fab__ripple';
10-
11-
// The MDC button's ripple ink color is based on the theme color, not on the foreground base
12-
// which is what the ripple mixin uses. This creates a new theme that sets the color to the
13-
// foreground base to appropriately color the ink.
14-
@mixin ripple-ink-color($mdc-color) {
15-
@include ripple-theme.color((
16-
foreground: (
17-
base: mdc-theme-color.prop-value($mdc-color)
18-
),
19-
));
6+
@mixin _ripple-color($color) {
7+
--mat-mdc-button-persistent-ripple-color: #{$color};
8+
--mat-mdc-button-ripple-color: #{rgba($color, 0.1)};
209
}
2110

22-
// Applies the disabled theme color to the text color.
23-
@mixin apply-disabled-color() {
24-
@include mdc-theme.prop(color,
25-
mdc-theme-color.ink-color-for-fill_(disabled, mdc-theme-color.$background));
11+
@mixin ripple-theme-styles($config, $is-filled) {
12+
$opacities: if(map.get($config, is-dark),
13+
mdc-ripple-theme.$light-ink-opacities, mdc-ripple-theme.$dark-ink-opacities);
14+
15+
// Ideally these styles would be structural, but MDC bases some of the opacities on the theme.
16+
&:hover .mat-mdc-button-persistent-ripple::before {
17+
opacity: map.get($opacities, hover);
18+
}
19+
20+
&:focus .mat-mdc-button-persistent-ripple::before {
21+
opacity: map.get($opacities, focus);
22+
}
23+
24+
&:active .mat-mdc-button-persistent-ripple::before {
25+
opacity: map.get($opacities, press);
26+
}
27+
28+
@include _ripple-color(mdc-theme-color.prop-value(on-surface));
29+
30+
&.mat-primary {
31+
@include _ripple-color(mdc-theme-color.prop-value(if($is-filled, on-primary, primary)));
32+
}
33+
34+
&.mat-accent {
35+
@include _ripple-color(mdc-theme-color.prop-value(if($is-filled, on-secondary, secondary)));
36+
}
37+
38+
&.mat-warn {
39+
@include _ripple-color(mdc-theme-color.prop-value(if($is-filled, on-error, error)));
40+
}
2641
}
2742

2843
// Wraps the content style in a selector for the disabled state.
@@ -37,14 +52,6 @@ $fab-state-target: '.mdc-fab__ripple';
3752
}
3853
}
3954

40-
// Applies the disabled theme background color for raised buttons. Value is taken from
41-
// mixin `mdc-button--filled`.
42-
// TODO(andrewseguin): Discuss with the MDC team about providing a variable for the 0.12 value
43-
// or otherwise have a mixin we can call to apply this style for both button and anchors.
44-
@mixin apply-disabled-background() {
45-
@include mdc-theme.prop(background-color, rgba(mdc-theme-color.prop-value(on-surface), 0.12));
46-
}
47-
4855
// Hides the touch target on lower densities.
4956
@mixin touch-target-density($scale) {
5057
@if ($scale == -2 or $scale == 'minimum') {

0 commit comments

Comments
 (0)