Skip to content

Commit 086c334

Browse files
committed
feat(material-experimental/mdc-button): fix ripples
1 parent cc8f37f commit 086c334

File tree

7 files changed

+34
-59
lines changed

7 files changed

+34
-59
lines changed

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

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
CanDisableRippleCtor,
1818
mixinColor,
1919
mixinDisabled,
20-
mixinDisableRipple,
20+
mixinDisableRipple, RippleAnimationConfig,
2121
RippleRenderer,
2222
RippleTarget
2323
} from '@angular/material/core';
@@ -75,30 +75,23 @@ export const _MatButtonBaseMixin: CanDisableRippleCtor&CanDisableCtor&CanColorCt
7575

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

93-
/** The ripple renderer for the button. */
94-
private _rippleRenderer =
95-
new RippleRenderer(this.rippleTarget, this._ngZone, this._elementRef, this._platform);
88+
/** Whether the ripple is centered on the button. */
89+
isRippleCentered = false;
9690

9791
constructor(
9892
elementRef: ElementRef, public _platform: Platform, public _ngZone: NgZone,
9993
public _animationMode?: string) {
10094
super(elementRef);
101-
this._rippleRenderer.setupTriggerEvents(this._elementRef.nativeElement);
10295

10396
// For each of the variant selectors that is present in the button's host
10497
// attributes, add the correct corresponding MDC classes.
@@ -109,12 +102,6 @@ export class MatButtonBase extends _MatButtonBaseMixin implements CanDisable, Ca
109102
}
110103
}
111104

112-
ngOnChanges(simpleChanges: SimpleChanges) {
113-
if (simpleChanges['disableRipple'] || simpleChanges['disabled']) {
114-
this.rippleTarget.rippleDisabled = this.disableRipple || this.disabled;
115-
}
116-
}
117-
118105
/** Focuses the button. */
119106
focus(): void {
120107
this._elementRef.nativeElement.focus();
@@ -124,6 +111,14 @@ export class MatButtonBase extends _MatButtonBaseMixin implements CanDisable, Ca
124111
private _hasHostAttributes(...attributes: string[]) {
125112
return attributes.some(attribute => this._elementRef.nativeElement.hasAttribute(attribute));
126113
}
114+
115+
_getHostElement() {
116+
return this._elementRef.nativeElement;
117+
}
118+
119+
_isRippleDisabled() {
120+
return this.disableRipple || this.disabled;
121+
}
127122
}
128123

129124
/** 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]="_getHostElement()"></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 {
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-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-
}
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: 3 additions & 9 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-
8-
.mat-mdc-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-
}
6+
.mat-mdc-icon-button {
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)