Skip to content

Commit c09029d

Browse files
committed
refactor(material-experimental/mdc-button): reduce bundle size
Reworks the MDC buttons to reduce their bundle size.
1 parent 42431f3 commit c09029d

File tree

9 files changed

+86
-146
lines changed

9 files changed

+86
-146
lines changed

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

Lines changed: 27 additions & 16 deletions
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,23 +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-
.mdc-icon-button__ripple::before, .mdc-icon-button__ripple::after {
14-
content: '';
15-
pointer-events: none;
16-
position: absolute;
17-
top: 0;
18-
right: 0;
19-
bottom: 0;
20-
left: 0;
21-
opacity: 0;
22-
border-radius: inherit;
23-
@content;
24-
}
25-
2612
// The ripple container should match the bounds of the entire button.
27-
.mat-mdc-button-ripple, .mdc-button__ripple, .mdc-fab__ripple, .mdc-icon-button__ripple {
13+
.mat-mdc-button-ripple,
14+
.mat-mdc-button-persistent-ripple,
15+
.mat-mdc-button-persistent-ripple::before {
2816
@include layout-common.fill;
2917

3018
// Disable pointer events for the ripple container and state overlay because the container
@@ -39,6 +27,17 @@
3927
border-radius: inherit;
4028
}
4129

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+
4241
// The content should appear over the state and ripple layers, otherwise they may adversely affect
4342
// the accessibility of the text content.
4443
.mdc-button__label {
@@ -77,3 +76,15 @@
7776
$query: mdc-helpers.$mat-base-styles-query);
7877
}
7978
}
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

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
@use 'sass:map';
22
@use '@material/ripple/ripple-theme' as mdc-ripple-theme;
3+
@use '@material/theme/theme-color' as mdc-theme-color;
34
@use '../../material/core/ripple/ripple-theme';
45

5-
// The MDC button's ripple ink color is based on the theme color, not on the foreground base
6-
// which is what the ripple mixin uses. This creates a new theme that sets the color to the
7-
// foreground base to appropriately color the ink.
8-
@mixin ripple-ink-color($color) {
9-
@include ripple-theme.color((
10-
foreground: (
11-
base: $color
12-
),
13-
));
6+
@mixin _ripple-color($color) {
7+
--mat-mdc-button-persistent-ripple-color: #{$color};
8+
--mat-mdc-button-ripple-color: #{rgba($color, 0.1)};
149
}
1510

16-
// MDC's ripple opacity depends on whether the theme is dark or light.
17-
// This function adds the correct mapping to the theme tokens.
18-
@function add-ripple-opacities($is-dark-theme, $tokens: ()) {
19-
$opacities: if($is-dark-theme,
11+
@mixin ripple-theme-styles($config, $is-filled) {
12+
$opacities: if(map.get($config, is-dark),
2013
mdc-ripple-theme.$light-ink-opacities, mdc-ripple-theme.$dark-ink-opacities);
21-
@return map.merge($tokens, (
22-
focus-state-layer-opacity: map.get($opacities, focus),
23-
hover-state-layer-opacity: map.get($opacities, hover),
24-
pressed-state-layer-opacity: map.get($opacities, press),
25-
));
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.

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

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,28 @@
1313

1414
@mixin _button-variant($color) {
1515
@include mdc-button-text-theme.theme((
16-
focus-state-layer-color: $color,
17-
hover-state-layer-color: $color,
1816
label-text-color: $color,
19-
pressed-state-layer-color: $color,
2017
));
2118
}
2219

2320
@mixin _unelevated-button-variant($foreground, $background) {
2421
@include mdc-button-filled-theme.theme((
2522
container-color: $background,
26-
focus-state-layer-color: $foreground,
27-
hover-state-layer-color: $foreground,
2823
label-text-color: $foreground,
29-
pressed-state-layer-color: $foreground,
3024
));
3125
}
3226

3327
@mixin _raised-button-variant($foreground, $background) {
3428
@include mdc-button-protected-theme.theme((
3529
container-color: $background,
36-
focus-state-layer-color: $foreground,
37-
hover-state-layer-color: $foreground,
3830
label-text-color: $foreground,
39-
pressed-state-layer-color: $foreground,
4031
));
4132
}
4233

4334
@mixin _outlined-button-variant($color) {
4435
@include mdc-button-outlined-theme.theme((
45-
focus-state-layer-color: $color,
46-
hover-state-layer-color: $color,
4736
label-text-color: $color,
4837
outline-color: $color,
49-
pressed-state-layer-color: $color,
5038
));
5139
}
5240

@@ -66,8 +54,6 @@
6654
$on-error: mdc-theme-color.prop-value(on-error);
6755

6856
.mat-mdc-button {
69-
@include mdc-button-text-theme.theme(button-theme-private.add-ripple-opacities($is-dark));
70-
7157
&.mat-unthemed {
7258
@include _button-variant($on-surface);
7359
}
@@ -95,8 +81,6 @@
9581
}
9682

9783
.mat-mdc-unelevated-button {
98-
@include mdc-button-filled-theme.theme(button-theme-private.add-ripple-opacities($is-dark));
99-
10084
&.mat-unthemed {
10185
@include _unelevated-button-variant($on-surface, $surface);
10286
}
@@ -126,9 +110,6 @@
126110
}
127111

128112
.mat-mdc-raised-button {
129-
@include mdc-button-protected-theme.theme(
130-
button-theme-private.add-ripple-opacities($is-dark));
131-
132113
&.mat-unthemed {
133114
@include _raised-button-variant($on-surface, $surface);
134115
}
@@ -159,8 +140,6 @@
159140
}
160141

161142
.mat-mdc-outlined-button {
162-
@include mdc-button-outlined-theme.theme(button-theme-private.add-ripple-opacities($is-dark));
163-
164143
&.mat-unthemed {
165144
@include _outlined-button-variant($on-surface);
166145
}
@@ -191,31 +170,11 @@
191170

192171
// Ripple colors
193172
.mat-mdc-button, .mat-mdc-outlined-button {
194-
&.mat-primary {
195-
@include button-theme-private.ripple-ink-color($primary);
196-
}
197-
198-
&.mat-accent {
199-
@include button-theme-private.ripple-ink-color($secondary);
200-
}
201-
202-
&.mat-warn {
203-
@include button-theme-private.ripple-ink-color($error);
204-
}
173+
@include button-theme-private.ripple-theme-styles($config, false);
205174
}
206175

207176
.mat-mdc-raised-button, .mat-mdc-unelevated-button {
208-
&.mat-primary {
209-
@include button-theme-private.ripple-ink-color($on-primary);
210-
}
211-
212-
&.mat-accent {
213-
@include button-theme-private.ripple-ink-color($on-secondary);
214-
}
215-
216-
&.mat-warn {
217-
@include button-theme-private.ripple-ink-color($on-error);
218-
}
177+
@include button-theme-private.ripple-theme-styles($config, true);
219178
}
220179
}
221180
}

src/material-experimental/mdc-button/_fab-theme.scss

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@
88
@use './button-theme-private';
99

1010
@mixin _fab-variant($foreground, $background) {
11-
@include button-theme-private.ripple-ink-color($foreground);
1211
@include mdc-fab-theme.theme((
1312
container-color: $background,
1413
icon-color: $foreground,
15-
focus-state-layer-color: $foreground,
16-
hover-state-layer-color: $foreground,
17-
pressed-state-layer-color: $foreground,
1814
));
1915

2016
--mat-mdc-fab-color: #{$foreground};
@@ -27,7 +23,7 @@
2723
$is-dark: map.get($config, is-dark);
2824

2925
.mat-mdc-fab, .mat-mdc-mini-fab {
30-
@include mdc-fab-theme.theme(button-theme-private.add-ripple-opacities($is-dark));
26+
@include button-theme-private.ripple-theme-styles($config, true);
3127

3228
&.mat-unthemed {
3329
@include _fab-variant($on-surface, mdc-theme-color.prop-value(surface));

src/material-experimental/mdc-button/_icon-button-theme.scss

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@
77
@use '../../material/core/typography/typography';
88
@use './button-theme-private';
99

10-
@mixin _icon-button-variant($color) {
11-
@include mdc-icon-button-theme.theme((
12-
icon-color: $color,
13-
focus-state-layer-color: $color,
14-
hover-state-layer-color: $color,
15-
pressed-state-layer-color: $color,
16-
));
17-
@include button-theme-private.ripple-ink-color($color);
18-
}
19-
2010
@mixin color($config-or-theme) {
2111
$config: theming.get-color-config($config-or-theme);
2212
@include mdc-helpers.mat-using-mdc-theme($config) {
@@ -25,23 +15,18 @@
2515
$disabled-color: rgba($on-surface, if($is-dark, 0.5, 0.38));
2616

2717
.mat-mdc-icon-button {
28-
@include button-theme-private.ripple-ink-color($on-surface);
29-
@include mdc-icon-button-theme.theme(button-theme-private.add-ripple-opacities($is-dark, (
30-
focus-state-layer-color: $on-surface,
31-
hover-state-layer-color: $on-surface,
32-
pressed-state-layer-color: $on-surface,
33-
)));
18+
@include button-theme-private.ripple-theme-styles($config, false);
3419

3520
&.mat-primary {
36-
@include _icon-button-variant(mdc-theme-color.prop-value(primary));
21+
@include mdc-icon-button-theme.theme((icon-color: mdc-theme-color.prop-value(primary)));
3722
}
3823

3924
&.mat-accent {
40-
@include _icon-button-variant(mdc-theme-color.prop-value(secondary));
25+
@include mdc-icon-button-theme.theme((icon-color: mdc-theme-color.prop-value(secondary)));
4126
}
4227

4328
&.mat-warn {
44-
@include _icon-button-variant(mdc-theme-color.prop-value(error));
29+
@include mdc-icon-button-theme.theme((icon-color: (mdc-theme-color.prop-value(error))));
4530
}
4631

4732
@include button-theme-private.apply-disabled-style() {

src/material-experimental/mdc-button/button.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<span
2+
class="mat-mdc-button-persistent-ripple"
23
[class.mdc-button__ripple]="!_isFab && !_isIconButton"
34
[class.mdc-fab__ripple]="_isFab"
45
[class.mdc-icon-button__ripple]="_isIconButton"></span>

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

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,57 +14,40 @@
1414
@include mdc-button.static-styles-without-ripple($query: mdc-helpers.$mat-base-styles-query);
1515

1616
// Keys to exclude from the MDC theme config, allowing us to drop styles we don't need.
17-
$excluded-keys: (
17+
$override-keys: button-base.mat-private-button-remove-ripple((
1818
label-text-font: null,
1919
label-text-size: null,
2020
label-text-tracking: null,
2121
label-text-transform: null,
2222
label-text-weight: null,
2323
with-icon-icon-size: null,
24-
);
24+
label-text-color: inherit,
25+
));
2526

2627
// Note that we don't include a feature query, because this mixins declare
2728
// all the "slots" for CSS variables that will be defined in the theme.
2829
.mat-mdc-button {
2930
@include mdc-button-text-theme.theme-styles(
30-
map.merge(map.merge(mdc-button-text-theme.$light-theme, $excluded-keys), (
31-
focus-state-layer-color: #000,
32-
hover-state-layer-color: #000,
33-
label-text-color: inherit,
34-
pressed-state-layer-color: inherit,
35-
)));
31+
map.merge(mdc-button-text-theme.$light-theme, $override-keys));
3632
}
3733

3834
.mat-mdc-unelevated-button {
3935
@include mdc-button-filled-theme.theme-styles(
40-
map.merge(map.merge(mdc-button-filled-theme.$light-theme, $excluded-keys), (
36+
map.merge(map.merge(mdc-button-filled-theme.$light-theme, $override-keys), (
4137
container-color: transparent,
42-
focus-state-layer-color: #000,
43-
hover-state-layer-color: #000,
44-
label-text-color: inherit,
45-
pressed-state-layer-color: #000,
4638
)));
4739
}
4840

4941
.mat-mdc-raised-button {
5042
@include mdc-button-protected-theme.theme-styles(
51-
map.merge(map.merge(mdc-button-protected-theme.$light-theme, $excluded-keys), (
43+
map.merge(map.merge(mdc-button-protected-theme.$light-theme, $override-keys), (
5244
container-color: transparent,
53-
focus-state-layer-color: #000,
54-
hover-state-layer-color: #000,
55-
label-text-color: inherit,
56-
pressed-state-layer-color: #000,
5745
)));
5846
}
5947

6048
.mat-mdc-outlined-button {
6149
@include mdc-button-outlined-theme.theme-styles(
62-
map.merge(map.merge(mdc-button-outlined-theme.$light-theme, $excluded-keys), (
63-
focus-state-layer-color: #000,
64-
hover-state-layer-color: #000,
65-
label-text-color: inherit,
66-
pressed-state-layer-color: #000,
67-
)));
50+
map.merge(mdc-button-outlined-theme.$light-theme, $override-keys));
6851
}
6952
}
7053

0 commit comments

Comments
 (0)