@@ -23,6 +23,7 @@ import {
2323 InjectionToken ,
2424 Injector ,
2525 Input ,
26+ NgZone ,
2627 OnDestroy ,
2728 QueryList ,
2829 ViewChild ,
@@ -49,7 +50,6 @@ import {MatFormFieldLineRipple} from './directives/line-ripple';
4950import { MatFormFieldNotchedOutline } from './directives/notched-outline' ;
5051import { MAT_PREFIX , MatPrefix } from './directives/prefix' ;
5152import { MAT_SUFFIX , MatSuffix } from './directives/suffix' ;
52- import { matFormFieldAnimations } from './form-field-animations' ;
5353import { MatFormFieldControl as _MatFormFieldControl } from './form-field-control' ;
5454import {
5555 getMatFormFieldDuplicatedHintError ,
@@ -141,7 +141,6 @@ interface MatFormFieldControl<T> extends _MatFormFieldControl<T> {}
141141 exportAs : 'matFormField' ,
142142 templateUrl : './form-field.html' ,
143143 styleUrl : './form-field.css' ,
144- animations : [ matFormFieldAnimations . transitionMessages ] ,
145144 host : {
146145 'class' : 'mat-mdc-form-field' ,
147146 '[class.mat-mdc-form-field-label-always-float]' : '_shouldAlwaysFloat()' ,
@@ -153,7 +152,6 @@ interface MatFormFieldControl<T> extends _MatFormFieldControl<T> {}
153152 '[class.mat-form-field-invalid]' : '_control.errorState' ,
154153 '[class.mat-form-field-disabled]' : '_control.disabled' ,
155154 '[class.mat-form-field-autofilled]' : '_control.autofilled' ,
156- '[class.mat-form-field-no-animations]' : '_animationMode === "NoopAnimations"' ,
157155 '[class.mat-form-field-appearance-fill]' : 'appearance == "fill"' ,
158156 '[class.mat-form-field-appearance-outline]' : 'appearance == "outline"' ,
159157 '[class.mat-form-field-hide-placeholder]' : '_hasFloatingLabel() && !_shouldLabelFloat()' ,
@@ -191,10 +189,11 @@ export class MatFormField
191189 private _dir = inject ( Directionality ) ;
192190 private _platform = inject ( Platform ) ;
193191 private _idGenerator = inject ( _IdGenerator ) ;
192+ private _ngZone = inject ( NgZone ) ;
193+ private _injector = inject ( Injector ) ;
194194 private _defaults = inject < MatFormFieldDefaultOptions > ( MAT_FORM_FIELD_DEFAULT_OPTIONS , {
195195 optional : true ,
196196 } ) ;
197- _animationMode = inject ( ANIMATION_MODULE_TYPE , { optional : true } ) ;
198197
199198 @ViewChild ( 'textField' ) _textField : ElementRef < HTMLElement > ;
200199 @ViewChild ( 'iconPrefixContainer' ) _iconPrefixContainer : ElementRef < HTMLElement > ;
@@ -310,9 +309,6 @@ export class MatFormField
310309 // Unique id for the hint label.
311310 readonly _hintLabelId = this . _idGenerator . getId ( 'mat-mdc-hint-' ) ;
312311
313- /** State of the mat-hint and mat-error animations. */
314- _subscriptAnimationState = '' ;
315-
316312 /** Gets the current form field control */
317313 get _control ( ) : MatFormFieldControl < any > {
318314 return this . _explicitFormFieldControl || this . _formFieldControl ;
@@ -329,8 +325,7 @@ export class MatFormField
329325 private _stateChanges : Subscription | undefined ;
330326 private _valueChanges : Subscription | undefined ;
331327 private _describedByChanges : Subscription | undefined ;
332-
333- private _injector = inject ( Injector ) ;
328+ protected readonly _animationsDisabled : boolean ;
334329
335330 constructor ( ...args : unknown [ ] ) ;
336331
@@ -346,14 +341,24 @@ export class MatFormField
346341 this . color = defaults . color ;
347342 }
348343 }
344+
345+ this . _animationsDisabled = inject ( ANIMATION_MODULE_TYPE , { optional : true } ) === 'NoopAnimations' ;
349346 }
350347
351348 ngAfterViewInit ( ) {
352349 // Initial focus state sync. This happens rarely, but we want to account for
353350 // it in case the form field control has "focused" set to true on init.
354351 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+
357362 // Because the above changes a value used in the template after it was checked, we need
358363 // to trigger CD or the change might not be reflected if there is no other CD scheduled.
359364 this . _changeDetectorRef . detectChanges ( ) ;
0 commit comments