Skip to content

Commit 9062640

Browse files
crisbetommalerba
authored andcommitted
fix(form-field): disable all animations when using NoopAnimationsModule (#11371)
Disables all of the CSS-based animations in the form field when using the `NoopAnimationsModule`. Relates to #10590.
1 parent 2a68edb commit 9062640

File tree

6 files changed

+49
-11
lines changed

6 files changed

+49
-11
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: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import {MatPlaceholder} from './placeholder';
5050
import {MatPrefix} from './prefix';
5151
import {MatSuffix} from './suffix';
5252
import {Platform} from '@angular/cdk/platform';
53+
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
5354

5455

5556
let nextUniqueId = 0;
@@ -130,6 +131,7 @@ export const MAT_FORM_FIELD_DEFAULT_OPTIONS =
130131
'[class.ng-valid]': '_shouldForward("valid")',
131132
'[class.ng-invalid]': '_shouldForward("invalid")',
132133
'[class.ng-pending]': '_shouldForward("pending")',
134+
'[class._mat-animation-noopable]': '!_animationsEnabled',
133135
},
134136
inputs: ['color'],
135137
encapsulation: ViewEncapsulation.None,
@@ -204,10 +206,11 @@ export class MatFormField extends _MatFormFieldMixinBase
204206
}
205207
private _floatLabel: FloatLabelType;
206208

207-
_outlineGapWidth = 0;
209+
/** Whether the Angular animations are enabled. */
210+
_animationsEnabled: boolean;
208211

212+
_outlineGapWidth = 0;
209213
_outlineGapStart = 0;
210-
211214
_initialGapCalculated = false;
212215

213216
/**
@@ -234,13 +237,15 @@ export class MatFormField extends _MatFormFieldMixinBase
234237
@Optional() private _dir: Directionality,
235238
@Optional() @Inject(MAT_FORM_FIELD_DEFAULT_OPTIONS) private _defaultOptions:
236239
MatFormFieldDefaultOptions,
237-
// @deletion-target 7.0.0 _platform and _ngZone to be made required.
240+
// @deletion-target 7.0.0 _platform, _ngZone and _animationMode to be made required.
238241
private _platform?: Platform,
239-
private _ngZone?: NgZone) {
242+
private _ngZone?: NgZone,
243+
@Optional() @Inject(ANIMATION_MODULE_TYPE) _animationMode?: string) {
240244
super(_elementRef);
241245

242246
this._labelOptions = labelOptions ? labelOptions : {};
243247
this.floatLabel = this._labelOptions.float || 'auto';
248+
this._animationsEnabled = _animationMode !== 'NoopAnimations';
244249
}
245250

246251
/**
@@ -345,13 +350,17 @@ export class MatFormField extends _MatFormFieldMixinBase
345350
/** Animates the placeholder up and locks it in position. */
346351
_animateAndLockLabel(): void {
347352
if (this._hasFloatingLabel() && this._canLabelFloat) {
348-
this._showAlwaysAnimate = true;
349-
this.floatLabel = 'always';
353+
// If animations are disabled, we shouldn't go in here,
354+
// because the `transitionend` will never fire.
355+
if (this._animationsEnabled) {
356+
this._showAlwaysAnimate = true;
350357

351-
fromEvent(this._label.nativeElement, 'transitionend').pipe(take(1)).subscribe(() => {
352-
this._showAlwaysAnimate = false;
353-
});
358+
fromEvent(this._label.nativeElement, 'transitionend').pipe(take(1)).subscribe(() => {
359+
this._showAlwaysAnimate = false;
360+
});
361+
}
354362

363+
this.floatLabel = 'always';
355364
this._changeDetectorRef.markForCheck();
356365
}
357366
}

src/lib/input/input.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
MatFormFieldModule,
2828
} from '@angular/material/form-field';
2929
import {By} from '@angular/platform-browser';
30-
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
30+
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
3131
import {MatInputModule} from './index';
3232
import {MatInput} from './input';
3333
import {MatStepperModule} from '@angular/material/stepper';
@@ -1203,7 +1203,7 @@ function createComponent<T>(component: Type<T>,
12031203
FormsModule,
12041204
MatFormFieldModule,
12051205
MatInputModule,
1206-
NoopAnimationsModule,
1206+
BrowserAnimationsModule,
12071207
PlatformModule,
12081208
ReactiveFormsModule,
12091209
...imports

0 commit comments

Comments
 (0)