Skip to content

Commit 4c136ca

Browse files
committed
feat(material-experimental/mdc-button): fix ripples (#16773)
* feat(material-experimental/mdc-button): fix ripples * fix lint * revised based on reviews * add material to BUILD * get ripple numbers from constants * add ripple to BUILD for tests * leading underscore for ripple centered * actually fix test BUILD (cherry picked from commit de06e59)
1 parent b8f4ee7 commit 4c136ca

File tree

8 files changed

+30
-60
lines changed

8 files changed

+30
-60
lines changed

src/material-experimental/mdc-button/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ng_module(
2222
deps = [
2323
"//src/cdk/platform",
2424
"//src/material/core",
25+
"@npm//@material/ripple",
2526
],
2627
)
2728

@@ -105,6 +106,7 @@ ng_test_library(
105106

106107
ng_web_test_suite(
107108
name = "unit_tests",
109+
static_files = ["@npm//:node_modules/@material/ripple/dist/mdc.ripple.js"],
108110
deps = [
109111
":button_tests_lib",
110112
"//src/material-experimental:mdc_require_config.js",

src/material-experimental/mdc-button/button-base.ts

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {Platform} from '@angular/cdk/platform';
10-
import {ElementRef, NgZone, OnChanges, SimpleChanges} from '@angular/core';
10+
import {ElementRef, NgZone} from '@angular/core';
1111
import {
1212
CanColor,
1313
CanColorCtor,
@@ -18,9 +18,9 @@ import {
1818
mixinColor,
1919
mixinDisabled,
2020
mixinDisableRipple,
21-
RippleRenderer,
22-
RippleTarget
21+
RippleAnimationConfig
2322
} from '@angular/material/core';
23+
import {numbers} from '@material/ripple';
2424

2525
/** Inputs common to all buttons. */
2626
export const MAT_BUTTON_INPUTS = ['disabled', 'disableRipple', 'color'];
@@ -74,30 +74,20 @@ export const _MatButtonBaseMixin: CanDisableRippleCtor&CanDisableCtor&CanColorCt
7474

7575
/** Base class for all buttons. */
7676
export class MatButtonBase extends _MatButtonBaseMixin implements CanDisable, CanColor,
77-
CanDisableRipple, OnChanges {
78-
rippleTarget: RippleTarget = {
79-
rippleConfig: {
80-
animation: {
81-
// TODO(mmalerba): Use the MDC constants once they are exported separately from the
82-
// foundation. Grabbing them off the foundation prevents the foundation class from being
83-
// tree-shaken. There is an open PR for this:
84-
// https://github.com/material-components/material-components-web/pull/4593
85-
enterDuration: 225 /* MDCRippleFoundation.numbers.DEACTIVATION_TIMEOUT_MS */,
86-
exitDuration: 150 /* MDCRippleFoundation.numbers.FG_DEACTIVATION_MS */,
87-
},
88-
},
89-
rippleDisabled: false,
77+
CanDisableRipple {
78+
/** The ripple animation configuration to use for the buttons. */
79+
_rippleAnimation: RippleAnimationConfig = {
80+
enterDuration: numbers.DEACTIVATION_TIMEOUT_MS,
81+
exitDuration: numbers.FG_DEACTIVATION_MS
9082
};
9183

92-
/** The ripple renderer for the button. */
93-
private _rippleRenderer =
94-
new RippleRenderer(this.rippleTarget, this._ngZone, this._elementRef, this._platform);
84+
/** Whether the ripple is centered on the button. */
85+
_isRippleCentered = false;
9586

9687
constructor(
9788
elementRef: ElementRef, public _platform: Platform, public _ngZone: NgZone,
9889
public _animationMode?: string) {
9990
super(elementRef);
100-
this._rippleRenderer.setupTriggerEvents(this._elementRef.nativeElement);
10191

10292
// For each of the variant selectors that is present in the button's host
10393
// attributes, add the correct corresponding MDC classes.
@@ -108,12 +98,6 @@ export class MatButtonBase extends _MatButtonBaseMixin implements CanDisable, Ca
10898
}
10999
}
110100

111-
ngOnChanges(simpleChanges: SimpleChanges) {
112-
if (simpleChanges['disableRipple'] || simpleChanges['disabled']) {
113-
this.rippleTarget.rippleDisabled = this.disableRipple || this.disabled;
114-
}
115-
}
116-
117101
/** Focuses the button. */
118102
focus(): void {
119103
this._elementRef.nativeElement.focus();
@@ -123,6 +107,10 @@ export class MatButtonBase extends _MatButtonBaseMixin implements CanDisable, Ca
123107
private _hasHostAttributes(...attributes: string[]) {
124108
return attributes.some(attribute => this._elementRef.nativeElement.hasAttribute(attribute));
125109
}
110+
111+
_isRippleDisabled() {
112+
return this.disableRipple || this.disabled;
113+
}
126114
}
127115

128116
/** Shared inputs by buttons using the `<a>` tag */

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@
55

66
<ng-content select=".material-icons[iconPositionEnd], mat-icon[iconPositionEnd]">
77
</ng-content>
8+
9+
<div matRipple class="mat-button-ripple"
10+
[matRippleAnimation]="_rippleAnimation"
11+
[matRippleDisabled]="_isRippleDisabled()"
12+
[matRippleCentered]="_isRippleCentered"
13+
[matRippleTrigger]="_elementRef.nativeElement"></div>

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,7 @@
44

55
@include mdc-button-without-ripple($query: $mat-base-styles-query);
66

7-
$_mat-button-ripple-opacity: 0.1;
8-
97
.mat-mdc-button, .mat-mdc-unelevated-button, .mat-mdc-raised-button, .mat-mdc-outlined-button {
10-
// The ripple element is created from the RippleRenderer rather than the MDC ripple, so it is
11-
// necessary to provide the color and opacity styling manually.
12-
.mat-ripple-element {
13-
opacity: $_mat-button-ripple-opacity;
14-
background-color: currentColor;
15-
}
16-
178
// Add an outline to make buttons more visible in high contrast mode. Stroked buttons
189
// don't need a special look in high-contrast mode, because those already have an outline.
1910
@include cdk-high-contrast {

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@
33

44
@include mdc-fab-without-ripple($query: $mat-base-styles-query);
55

6-
$_mat-button-ripple-opacity: 0.1;
7-
8-
.mat-mdc-fab, .mat-mdc-mini-fab {
9-
// The ripple element is created from the RippleRenderer rather than the MDC ripple, so it is
10-
// necessary to provide the color and opacity styling manually.
11-
.mat-ripple-element {
12-
opacity: $_mat-button-ripple-opacity;
13-
background-color: currentColor;
14-
}
15-
}
16-
176
// MDC expects the fab icon to contain this HTML content:
187
// ```html
198
// <span class="mdc-fab__icon material-icons">favorite</span>

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@
33

44
@include mdc-icon-button-without-ripple($query: $mat-base-styles-query);
55

6-
$_mat-button-ripple-opacity: 0.1;
7-
86
.mat-mdc-icon-button {
9-
// The ripple element is created from the RippleRenderer rather than the MDC ripple, so it is
10-
// necessary to provide the color and opacity styling manually.
11-
.mat-ripple-element {
12-
opacity: $_mat-button-ripple-opacity;
13-
background-color: currentColor;
14-
}
7+
// Border radius is inherited by ripple to know its shape. Set to 50% so the ripple is round.
8+
border-radius: 50%;
159
}

src/material-experimental/mdc-button/icon-button.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ import {
4040
changeDetection: ChangeDetectionStrategy.OnPush,
4141
})
4242
export class MatIconButton extends MatButtonBase {
43+
_isRippleCentered = true;
44+
4345
constructor(
4446
elementRef: ElementRef, platform: Platform, ngZone: NgZone,
4547
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
4648
super(elementRef, platform, ngZone, animationMode);
47-
this.rippleTarget.rippleConfig.centered = true;
48-
this.rippleTarget.rippleConfig.radius = 24;
4949
}
5050
}
5151

@@ -61,11 +61,11 @@ export class MatIconButton extends MatButtonBase {
6161
changeDetection: ChangeDetectionStrategy.OnPush,
6262
})
6363
export class MatIconAnchor extends MatAnchorBase {
64+
_isRippleCentered = true;
65+
6466
constructor(
6567
elementRef: ElementRef, platform: Platform, ngZone: NgZone,
6668
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
6769
super(elementRef, platform, ngZone, animationMode);
68-
this.rippleTarget.rippleConfig.centered = true;
69-
this.rippleTarget.rippleConfig.radius = 24;
7070
}
7171
}

src/material-experimental/mdc-button/module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
import {CommonModule} from '@angular/common';
1010
import {NgModule} from '@angular/core';
11-
import {MatCommonModule} from '@angular/material/core';
11+
import {MatCommonModule, MatRippleModule} from '@angular/material/core';
1212
import {MatAnchor, MatButton} from './button';
1313
import {MatFabAnchor, MatFabButton} from './fab';
1414
import {MatIconAnchor, MatIconButton} from './icon-button';
1515

1616
@NgModule({
17-
imports: [MatCommonModule, CommonModule],
17+
imports: [MatCommonModule, CommonModule, MatRippleModule],
1818
exports: [
1919
MatAnchor,
2020
MatButton,

0 commit comments

Comments
 (0)