Skip to content

Commit c9d55d3

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 1f3324e commit c9d55d3

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/lib/chips/chip.ts

Lines changed: 15 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',
@@ -142,6 +144,9 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
142144
/** Whether the chip has focus. */
143145
_hasFocus: boolean = false;
144146

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

@@ -221,30 +226,34 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
221226
return this.selectable ? this.selected.toString() : null;
222227
}
223228

224-
constructor(public _elementRef: ElementRef,
229+
constructor(public _elementRef: ElementRef<HTMLElement>,
225230
private _ngZone: NgZone,
226231
platform: Platform,
227232
@Optional() @Inject(MAT_RIPPLE_GLOBAL_OPTIONS)
228-
globalRippleOptions: RippleGlobalOptions | null) {
233+
globalRippleOptions: RippleGlobalOptions | null,
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);
235242
this.rippleConfig = globalRippleOptions || {};
243+
this._animationsDisabled = animationMode === 'NoopAnimations';
236244
}
237245

238246
_addHostClassName() {
247+
const element = this._elementRef.nativeElement;
248+
239249
// Add class for the different chips
240250
for (const attr of CHIP_ATTRIBUTE_NAMES) {
241-
if (this._elementRef.nativeElement.hasAttribute(attr) ||
242-
this._elementRef.nativeElement.tagName.toLowerCase() === attr) {
243-
(this._elementRef.nativeElement as HTMLElement).classList.add(attr);
251+
if (element.hasAttribute(attr) || element.tagName.toLowerCase() === attr) {
252+
element.classList.add(attr);
244253
return;
245254
}
246255
}
247-
(this._elementRef.nativeElement as HTMLElement).classList.add('mat-standard-chip');
256+
element.classList.add('mat-standard-chip');
248257
}
249258

250259
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;
@@ -36,6 +37,7 @@ $mat-chip-remove-size: 18px;
3637

3738
.mat-standard-chip {
3839
@include mat-elevation-transition;
40+
@include _noop-animation;
3941
display: inline-flex;
4042
padding: $mat-chip-vertical-padding $mat-chip-horizontal-padding;
4143
border-radius: 16px;

0 commit comments

Comments
 (0)