Skip to content

Commit 79dec72

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 3bc52df commit 79dec72

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
@@ -41,6 +41,7 @@ import {
4141
} from '@angular/material/core';
4242
import {Subject} from 'rxjs';
4343
import {take} from 'rxjs/operators';
44+
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
4445

4546

4647
/** Represents an event fired on an individual `mat-chip`. */
@@ -108,6 +109,7 @@ export class MatChipTrailingIcon {}
108109
'[class.mat-chip-with-avatar]': 'avatar',
109110
'[class.mat-chip-with-trailing-icon]': 'trailingIcon || removeIcon',
110111
'[class.mat-chip-disabled]': 'disabled',
112+
'[class._mat-animation-noopable]': '_animationsDisabled',
111113
'[attr.disabled]': 'disabled || null',
112114
'[attr.aria-disabled]': 'disabled.toString()',
113115
'[attr.aria-selected]': 'ariaSelected',
@@ -143,6 +145,9 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
143145
/** Whether the chip has focus. */
144146
_hasFocus: boolean = false;
145147

148+
/** Whether animations for the chip are enabled. */
149+
_animationsDisabled: boolean;
150+
146151
/** Whether the chip list is selectable */
147152
chipListSelectable: boolean = true;
148153

@@ -222,16 +227,19 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
222227
return this.selectable ? this.selected.toString() : null;
223228
}
224229

225-
constructor(public _elementRef: ElementRef,
230+
constructor(public _elementRef: ElementRef<HTMLElement>,
226231
private _ngZone: NgZone,
227232
platform: Platform,
228-
@Optional() @Inject(MAT_RIPPLE_GLOBAL_OPTIONS) globalOptions: RippleGlobalOptions) {
233+
@Optional() @Inject(MAT_RIPPLE_GLOBAL_OPTIONS) globalOptions: RippleGlobalOptions,
234+
// @breaking-change 7.0.0 `animationMode` parameter to become required.
235+
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
229236
super(_elementRef);
230237

231238
this._addHostClassName();
232239

233240
this._chipRipple = new RippleRenderer(this, _ngZone, _elementRef, platform);
234241
this._chipRipple.setupTriggerEvents(_elementRef.nativeElement);
242+
this._animationsDisabled = animationMode === 'NoopAnimations';
235243

236244
if (globalOptions) {
237245
// TODO(paul): Do not copy each option manually. Allow dynamic global option changes: #9729
@@ -241,18 +249,23 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
241249
terminateOnPointerUp: globalOptions.terminateOnPointerUp,
242250
};
243251
}
252+
253+
if (this._animationsDisabled) {
254+
this.rippleConfig.animation = {enterDuration: 0, exitDuration: 0};
255+
}
244256
}
245257

246258
_addHostClassName() {
259+
const element = this._elementRef.nativeElement;
260+
247261
// Add class for the different chips
248262
for (const attr of CHIP_ATTRIBUTE_NAMES) {
249-
if (this._elementRef.nativeElement.hasAttribute(attr) ||
250-
this._elementRef.nativeElement.tagName.toLowerCase() === attr) {
251-
(this._elementRef.nativeElement as HTMLElement).classList.add(attr);
263+
if (element.hasAttribute(attr) || element.tagName.toLowerCase() === attr) {
264+
element.classList.add(attr);
252265
return;
253266
}
254267
}
255-
(this._elementRef.nativeElement as HTMLElement).classList.add('mat-standard-chip');
268+
element.classList.add('mat-standard-chip');
256269
}
257270

258271
ngOnDestroy() {

src/lib/chips/chips.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@import '../core/style/variables';
22
@import '../core/style/elevation';
33
@import '../core/style/layout-common';
4+
@import '../core/style/noop-animation';
45
@import '../../cdk/a11y/a11y';
56

67
$mat-chip-min-height: 32px;
@@ -33,6 +34,7 @@ $mat-chip-remove-size: 18px;
3334

3435
.mat-standard-chip {
3536
@include mat-elevation-transition;
37+
@include _noop-animation;
3638
display: inline-flex;
3739
padding: $mat-chip-vertical-padding $mat-chip-horizontal-padding;
3840
border-radius: 16px;

0 commit comments

Comments
 (0)