Skip to content

Commit fc58823

Browse files
committed
fix(chips): disable all animations when using NoopAnimationsModule
Disables all of the chip animations and transitions when the consumer is using the `NoopAnimationsModule`. Also cleans up some repetitive code. Relates to #10590.
1 parent 89d16b2 commit fc58823

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/lib/chips/chip.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
} from '@angular/material/core';
3939
import {Subject} from 'rxjs';
4040
import {take} from 'rxjs/operators';
41+
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
4142

4243

4344
/** Represents an event fired on an individual `mat-chip`. */
@@ -104,6 +105,7 @@ export class MatChipTrailingIcon {}
104105
'[class.mat-chip-with-avatar]': 'avatar',
105106
'[class.mat-chip-with-trailing-icon]': 'trailingIcon || removeIcon',
106107
'[class.mat-chip-disabled]': 'disabled',
108+
'[class._mat-animation-noopable]': '_animationsDisabled',
107109
'[attr.disabled]': 'disabled || null',
108110
'[attr.aria-disabled]': 'disabled.toString()',
109111
'[attr.aria-selected]': 'ariaSelected',
@@ -139,6 +141,9 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
139141
/** Whether the chip has focus. */
140142
_hasFocus: boolean = false;
141143

144+
/** Whether animations for the chip are enabled. */
145+
_animationsDisabled: boolean;
146+
142147
/** Whether the chip list is selectable */
143148
chipListSelectable: boolean = true;
144149

@@ -218,16 +223,19 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
218223
return this.selectable ? this.selected.toString() : null;
219224
}
220225

221-
constructor(public _elementRef: ElementRef,
226+
constructor(public _elementRef: ElementRef<HTMLElement>,
222227
private _ngZone: NgZone,
223228
platform: Platform,
224-
@Optional() @Inject(MAT_RIPPLE_GLOBAL_OPTIONS) globalOptions: RippleGlobalOptions) {
229+
@Optional() @Inject(MAT_RIPPLE_GLOBAL_OPTIONS) globalOptions: RippleGlobalOptions,
230+
// @breaking-change 7.0.0 `animationMode` parameter to become required.
231+
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
225232
super(_elementRef);
226233

227234
this._addHostClassName();
228235

229236
this._chipRipple = new RippleRenderer(this, _ngZone, _elementRef, platform);
230237
this._chipRipple.setupTriggerEvents(_elementRef.nativeElement);
238+
this._animationsDisabled = animationMode === 'NoopAnimations';
231239

232240
if (globalOptions) {
233241
this._ripplesGloballyDisabled = !!globalOptions.disabled;
@@ -238,18 +246,23 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
238246
terminateOnPointerUp: globalOptions.terminateOnPointerUp,
239247
};
240248
}
249+
250+
if (this._animationsDisabled) {
251+
this.rippleConfig.animation = {enterDuration: 0, exitDuration: 0};
252+
}
241253
}
242254

243255
_addHostClassName() {
256+
const element = this._elementRef.nativeElement;
257+
244258
// Add class for the different chips
245259
for (const attr of CHIP_ATTRIBUTE_NAMES) {
246-
if (this._elementRef.nativeElement.hasAttribute(attr) ||
247-
this._elementRef.nativeElement.tagName.toLowerCase() === attr) {
248-
(this._elementRef.nativeElement as HTMLElement).classList.add(attr);
260+
if (element.hasAttribute(attr) || element.tagName.toLowerCase() === attr) {
261+
element.classList.add(attr);
249262
return;
250263
}
251264
}
252-
(this._elementRef.nativeElement as HTMLElement).classList.add('mat-standard-chip');
265+
element.classList.add('mat-standard-chip');
253266
}
254267

255268
ngOnDestroy() {

src/lib/chips/chips.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@import '../core/style/elevation';
2+
@import '../core/style/noop-animation';
23
@import '../../cdk/a11y/a11y';
34

45
$mat-chip-vertical-padding: 7px;
@@ -29,6 +30,7 @@ $mat-chip-remove-size: 18px;
2930

3031
.mat-standard-chip {
3132
@include mat-elevation-transition;
33+
@include _noop-animation;
3234
display: inline-flex;
3335
padding: $mat-chip-vertical-padding $mat-chip-horizontal-padding;
3436
border-radius: $mat-chip-horizontal-padding * 2;

0 commit comments

Comments
 (0)