@@ -23,6 +23,7 @@ import {
23
23
InjectionToken ,
24
24
Injector ,
25
25
Input ,
26
+ NgZone ,
26
27
OnDestroy ,
27
28
QueryList ,
28
29
ViewChild ,
@@ -49,7 +50,6 @@ import {MatFormFieldLineRipple} from './directives/line-ripple';
49
50
import { MatFormFieldNotchedOutline } from './directives/notched-outline' ;
50
51
import { MAT_PREFIX , MatPrefix } from './directives/prefix' ;
51
52
import { MAT_SUFFIX , MatSuffix } from './directives/suffix' ;
52
- import { matFormFieldAnimations } from './form-field-animations' ;
53
53
import { MatFormFieldControl as _MatFormFieldControl } from './form-field-control' ;
54
54
import {
55
55
getMatFormFieldDuplicatedHintError ,
@@ -141,7 +141,6 @@ interface MatFormFieldControl<T> extends _MatFormFieldControl<T> {}
141
141
exportAs : 'matFormField' ,
142
142
templateUrl : './form-field.html' ,
143
143
styleUrl : './form-field.css' ,
144
- animations : [ matFormFieldAnimations . transitionMessages ] ,
145
144
host : {
146
145
'class' : 'mat-mdc-form-field' ,
147
146
'[class.mat-mdc-form-field-label-always-float]' : '_shouldAlwaysFloat()' ,
@@ -153,7 +152,6 @@ interface MatFormFieldControl<T> extends _MatFormFieldControl<T> {}
153
152
'[class.mat-form-field-invalid]' : '_control.errorState' ,
154
153
'[class.mat-form-field-disabled]' : '_control.disabled' ,
155
154
'[class.mat-form-field-autofilled]' : '_control.autofilled' ,
156
- '[class.mat-form-field-no-animations]' : '_animationMode === "NoopAnimations"' ,
157
155
'[class.mat-form-field-appearance-fill]' : 'appearance == "fill"' ,
158
156
'[class.mat-form-field-appearance-outline]' : 'appearance == "outline"' ,
159
157
'[class.mat-form-field-hide-placeholder]' : '_hasFloatingLabel() && !_shouldLabelFloat()' ,
@@ -191,10 +189,11 @@ export class MatFormField
191
189
private _dir = inject ( Directionality ) ;
192
190
private _platform = inject ( Platform ) ;
193
191
private _idGenerator = inject ( _IdGenerator ) ;
192
+ private _ngZone = inject ( NgZone ) ;
193
+ private _injector = inject ( Injector ) ;
194
194
private _defaults = inject < MatFormFieldDefaultOptions > ( MAT_FORM_FIELD_DEFAULT_OPTIONS , {
195
195
optional : true ,
196
196
} ) ;
197
- _animationMode = inject ( ANIMATION_MODULE_TYPE , { optional : true } ) ;
198
197
199
198
@ViewChild ( 'textField' ) _textField : ElementRef < HTMLElement > ;
200
199
@ViewChild ( 'iconPrefixContainer' ) _iconPrefixContainer : ElementRef < HTMLElement > ;
@@ -310,9 +309,6 @@ export class MatFormField
310
309
// Unique id for the hint label.
311
310
readonly _hintLabelId = this . _idGenerator . getId ( 'mat-mdc-hint-' ) ;
312
311
313
- /** State of the mat-hint and mat-error animations. */
314
- _subscriptAnimationState = '' ;
315
-
316
312
/** Gets the current form field control */
317
313
get _control ( ) : MatFormFieldControl < any > {
318
314
return this . _explicitFormFieldControl || this . _formFieldControl ;
@@ -329,8 +325,7 @@ export class MatFormField
329
325
private _stateChanges : Subscription | undefined ;
330
326
private _valueChanges : Subscription | undefined ;
331
327
private _describedByChanges : Subscription | undefined ;
332
-
333
- private _injector = inject ( Injector ) ;
328
+ protected readonly _animationsDisabled : boolean ;
334
329
335
330
constructor ( ...args : unknown [ ] ) ;
336
331
@@ -346,14 +341,24 @@ export class MatFormField
346
341
this . color = defaults . color ;
347
342
}
348
343
}
344
+
345
+ this . _animationsDisabled = inject ( ANIMATION_MODULE_TYPE , { optional : true } ) === 'NoopAnimations' ;
349
346
}
350
347
351
348
ngAfterViewInit ( ) {
352
349
// Initial focus state sync. This happens rarely, but we want to account for
353
350
// it in case the form field control has "focused" set to true on init.
354
351
this . _updateFocusState ( ) ;
355
- // Enable animations now. This ensures we don't animate on initial render.
356
- this . _subscriptAnimationState = 'enter' ;
352
+
353
+ if ( ! this . _animationsDisabled ) {
354
+ this . _ngZone . runOutsideAngular ( ( ) => {
355
+ // Enable animations after a certain amount of time so that they don't run on init.
356
+ setTimeout ( ( ) => {
357
+ this . _elementRef . nativeElement . classList . add ( 'mat-form-field-animations-enabled' ) ;
358
+ } , 300 ) ;
359
+ } ) ;
360
+ }
361
+
357
362
// Because the above changes a value used in the template after it was checked, we need
358
363
// to trigger CD or the change might not be reflected if there is no other CD scheduled.
359
364
this . _changeDetectorRef . detectChanges ( ) ;
0 commit comments