Skip to content

Commit 81bb4f1

Browse files
committed
fix(form-field): disable all animations when using NoopAnimationsModule
Disables all of the CSS-based animations in the form field when using the `NoopAnimationsModule`. Relates to #10590.
1 parent abc3d38 commit 81bb4f1

File tree

5 files changed

+48
-10
lines changed

5 files changed

+48
-10
lines changed

src/lib/form-field/form-field-fill.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ $mat-form-field-fill-subscript-padding:
5353
}
5454
}
5555

56+
&._mat-animation-noopable:not(.mat-form-field-disabled) .mat-form-field-flex:hover {
57+
& ~ .mat-form-field-underline .mat-form-field-ripple {
58+
transition: none;
59+
}
60+
}
61+
5662
.mat-form-field-subscript-wrapper {
5763
padding: 0 $mat-form-field-fill-subscript-padding;
5864
}

src/lib/form-field/form-field-outline.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,14 @@ $mat-form-field-outline-subscript-padding:
130130
.mat-form-field-subscript-wrapper {
131131
padding: 0 $mat-form-field-outline-subscript-padding;
132132
}
133+
134+
&._mat-animation-noopable {
135+
&:not(.mat-form-field-disabled) .mat-form-field-flex:hover ~ .mat-form-field-outline,
136+
.mat-form-field-outline,
137+
.mat-form-field-outline-start,
138+
.mat-form-field-outline-end,
139+
.mat-form-field-outline-gap {
140+
transition: none;
141+
}
142+
}
133143
}

src/lib/form-field/form-field-standard.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,10 @@ $mat-form-field-standard-padding-top: 0.75em !default;
4141
transition: opacity 600ms $swift-ease-out-timing-function;
4242
}
4343
}
44+
45+
&._mat-animation-noopable:not(.mat-form-field-disabled) .mat-form-field-flex:hover {
46+
& ~ .mat-form-field-underline .mat-form-field-ripple {
47+
transition: none;
48+
}
49+
}
4450
}

src/lib/form-field/form-field.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,10 @@ $mat-form-field-default-infix-width: 180px !default;
207207
.mat-error {
208208
display: block;
209209
}
210+
211+
.mat-form-field._mat-animation-noopable {
212+
.mat-form-field-label,
213+
.mat-form-field-ripple {
214+
transition: none;
215+
}
216+
}

src/lib/form-field/form-field.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {MatPlaceholder} from './placeholder';
4949
import {MatPrefix} from './prefix';
5050
import {MatSuffix} from './suffix';
5151
import {Platform} from '@angular/cdk/platform';
52+
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
5253

5354

5455
let nextUniqueId = 0;
@@ -117,6 +118,7 @@ export const MAT_FORM_FIELD_DEFAULT_OPTIONS =
117118
'[class.ng-valid]': '_shouldForward("valid")',
118119
'[class.ng-invalid]': '_shouldForward("invalid")',
119120
'[class.ng-pending]': '_shouldForward("pending")',
121+
'[class._mat-animation-noopable]': '!_animationsEnabled',
120122
},
121123
inputs: ['color'],
122124
encapsulation: ViewEncapsulation.None,
@@ -191,10 +193,11 @@ export class MatFormField extends _MatFormFieldMixinBase
191193
}
192194
private _floatLabel: FloatLabelType;
193195

194-
_outlineGapWidth = 0;
196+
/** Whether the Angular animations are enabled. */
197+
_animationsEnabled: boolean;
195198

199+
_outlineGapWidth = 0;
196200
_outlineGapStart = 0;
197-
198201
_initialGapCalculated = false;
199202

200203
/**
@@ -221,12 +224,14 @@ export class MatFormField extends _MatFormFieldMixinBase
221224
@Optional() private _dir: Directionality,
222225
@Optional() @Inject(MAT_FORM_FIELD_DEFAULT_OPTIONS) private _defaultOptions:
223226
MatFormFieldDefaultOptions,
224-
// @deletion-target 7.0.0 _platform to be made required.
225-
private _platform?: Platform) {
227+
// @deletion-target 7.0.0 _platform and _animationMode to be made required.
228+
private _platform?: Platform,
229+
@Optional() @Inject(ANIMATION_MODULE_TYPE) _animationMode?: string) {
226230
super(_elementRef);
227231

228232
this._labelOptions = labelOptions ? labelOptions : {};
229233
this.floatLabel = this._labelOptions.float || 'auto';
234+
this._animationsEnabled = _animationMode !== 'NoopAnimations';
230235
}
231236

232237
/**
@@ -320,13 +325,17 @@ export class MatFormField extends _MatFormFieldMixinBase
320325
/** Animates the placeholder up and locks it in position. */
321326
_animateAndLockLabel(): void {
322327
if (this._hasFloatingLabel() && this._canLabelFloat) {
323-
this._showAlwaysAnimate = true;
324-
this.floatLabel = 'always';
325-
326-
fromEvent(this._label.nativeElement, 'transitionend').pipe(take(1)).subscribe(() => {
327-
this._showAlwaysAnimate = false;
328-
});
328+
// If animations are disabled, we shouldn't go in here,
329+
// because the `transitionend` will never fire.
330+
if (this._animationsEnabled) {
331+
this._showAlwaysAnimate = true;
332+
333+
fromEvent(this._label.nativeElement, 'transitionend').pipe(take(1)).subscribe(() => {
334+
this._showAlwaysAnimate = false;
335+
});
336+
}
329337

338+
this.floatLabel = 'always';
330339
this._changeDetectorRef.markForCheck();
331340
}
332341
}

0 commit comments

Comments
 (0)