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