7
7
*/
8
8
9
9
import { FocusMonitor , FocusOrigin } from '@angular/cdk/a11y' ;
10
+ import { coerceBooleanProperty } from '@angular/cdk/coercion' ;
10
11
import { Platform } from '@angular/cdk/platform' ;
11
12
import {
12
13
AfterViewInit ,
@@ -26,8 +27,8 @@ import {
26
27
mixinColor ,
27
28
mixinDisabled ,
28
29
mixinDisableRipple ,
30
+ MatRippleLoader ,
29
31
} from '@angular/material/core' ;
30
- import { MAT_BUTTON_RIPPLE_UNINITIALIZED , MatButtonLazyLoader } from './button-lazy-loader' ;
31
32
32
33
/** Inputs common to all buttons. */
33
34
export const MAT_BUTTON_INPUTS = [ 'disabled' , 'disableRipple' , 'color' ] ;
@@ -43,7 +44,6 @@ export const MAT_BUTTON_HOST = {
43
44
// Add a class that applies to all buttons. This makes it easier to target if somebody
44
45
// wants to target all Material buttons.
45
46
'[class.mat-mdc-button-base]' : 'true' ,
46
- [ MAT_BUTTON_RIPPLE_UNINITIALIZED ] : '' ,
47
47
} ;
48
48
49
49
/** List of classes to add to buttons instances based on host attribute selector. */
@@ -94,15 +94,15 @@ export const _MatButtonMixin = mixinColor(
94
94
@Directive ( )
95
95
export class MatButtonBase
96
96
extends _MatButtonMixin
97
- implements CanDisable , CanColor , CanDisableRipple , AfterViewInit , OnChanges , OnDestroy
97
+ implements CanDisable , CanColor , CanDisableRipple , AfterViewInit , OnDestroy
98
98
{
99
99
private readonly _focusMonitor = inject ( FocusMonitor ) ;
100
100
101
101
/**
102
102
* Handles the lazy creation of the MatButton ripple.
103
103
* Used to improve initial load time of large applications.
104
104
*/
105
- _rippleLoader : MatButtonLazyLoader = inject ( MatButtonLazyLoader ) ;
105
+ _rippleLoader : MatRippleLoader = inject ( MatRippleLoader ) ;
106
106
107
107
/** Whether this button is a FAB. Used to apply the correct class on the ripple. */
108
108
_isFab = false ;
@@ -113,17 +113,33 @@ export class MatButtonBase
113
113
* @breaking -change 17.0.0
114
114
*/
115
115
get ripple ( ) : MatRipple {
116
- if ( ! this . _ripple && this . _rippleLoader ) {
117
- this . _ripple = this . _rippleLoader . _createMatRipple ( this . _elementRef . nativeElement ) ;
118
- }
119
- return this . _ripple ! ;
116
+ return this . _rippleLoader ?. getRipple ( this . _elementRef . nativeElement ) ! ;
120
117
}
121
118
set ripple ( v : MatRipple ) {
122
- this . _ripple = v ;
119
+ this . _rippleLoader ?. attachRipple ( this . _elementRef . nativeElement , v ) ;
123
120
}
124
121
125
- /** @docs -private Reference to the MatRipple instance of the button. */
126
- protected _ripple ?: MatRipple ;
122
+ // We override `disableRipple` and `disabled` so we can hook into
123
+ // their setters and update the ripple disabled state accordingly.
124
+
125
+ /** Whether the ripple effect is disabled or not. */
126
+ override get disableRipple ( ) : boolean {
127
+ return this . _disableRipple ;
128
+ }
129
+ override set disableRipple ( value : any ) {
130
+ this . _disableRipple = coerceBooleanProperty ( value ) ;
131
+ this . _updateRippleDisabled ( ) ;
132
+ }
133
+ private _disableRipple : boolean = false ;
134
+
135
+ override get disabled ( ) : boolean {
136
+ return this . _disabled ;
137
+ }
138
+ override set disabled ( value : any ) {
139
+ this . _disabled = coerceBooleanProperty ( value ) ;
140
+ this . _updateRippleDisabled ( ) ;
141
+ }
142
+ private _disabled : boolean = false ;
127
143
128
144
constructor (
129
145
elementRef : ElementRef ,
@@ -133,6 +149,10 @@ export class MatButtonBase
133
149
) {
134
150
super ( elementRef ) ;
135
151
152
+ this . _rippleLoader ?. configureRipple ( this . _elementRef . nativeElement , {
153
+ className : 'mat-mdc-button-ripple' ,
154
+ } ) ;
155
+
136
156
const classList = ( elementRef . nativeElement as HTMLElement ) . classList ;
137
157
138
158
// For each of the variant selectors that is present in the button's host
@@ -150,12 +170,6 @@ export class MatButtonBase
150
170
this . _focusMonitor . monitor ( this . _elementRef , true ) ;
151
171
}
152
172
153
- ngOnChanges ( ) {
154
- if ( this . _ripple ) {
155
- this . _ripple . disabled = this . disableRipple || this . disabled ;
156
- }
157
- }
158
-
159
173
ngOnDestroy ( ) {
160
174
this . _focusMonitor . stopMonitoring ( this . _elementRef ) ;
161
175
}
@@ -173,6 +187,13 @@ export class MatButtonBase
173
187
private _hasHostAttributes ( ...attributes : string [ ] ) {
174
188
return attributes . some ( attribute => this . _elementRef . nativeElement . hasAttribute ( attribute ) ) ;
175
189
}
190
+
191
+ private _updateRippleDisabled ( ) : void {
192
+ this . _rippleLoader ?. setDisabled (
193
+ this . _elementRef . nativeElement ,
194
+ this . disableRipple || this . disabled ,
195
+ ) ;
196
+ }
176
197
}
177
198
178
199
/** Shared inputs by buttons using the `<a>` tag */
@@ -195,7 +216,6 @@ export const MAT_ANCHOR_HOST = {
195
216
// Add a class that applies to all buttons. This makes it easier to target if somebody
196
217
// wants to target all Material buttons.
197
218
'[class.mat-mdc-button-base]' : 'true' ,
198
- [ MAT_BUTTON_RIPPLE_UNINITIALIZED ] : '' ,
199
219
} ;
200
220
201
221
/**
0 commit comments