Skip to content

Commit 83276b1

Browse files
committed
fix(material-experimental/mdc-chips): avoid unnecessary change detections
Avoids running the `transitionend` inside the `NgZone` for the MDC-based `mat-chip` since it fires a lot and it doesn't need to trigger change detection.
1 parent 198911f commit 83276b1

File tree

1 file changed

+11
-8
lines changed
  • src/material-experimental/mdc-chips

1 file changed

+11
-8
lines changed

src/material-experimental/mdc-chips/chip.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,11 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
136136
/** Whether animations for the chip are enabled. */
137137
_animationsDisabled: boolean;
138138

139-
// We have to use a `HostListener` here in order to support both Ivy and ViewEngine.
140-
// In Ivy the `host` bindings will be merged when this class is extended, whereas in
141-
// ViewEngine they're overwritten.
142-
// TODO(mmalerba): we move this back into `host` once Ivy is turned on by default.
143-
// tslint:disable-next-line:no-host-decorator-in-concrete
144-
@HostListener('transitionend', ['$event'])
145-
_handleTransitionEnd(event: TransitionEvent) {
139+
/**
140+
* Event listener for `transitionend` events. Needs to be an arrow
141+
* function so we can pass it easily into `addEventListener`.
142+
*/
143+
private _handleTransitionEnd = (event: TransitionEvent) => {
146144
this._chipFoundation.handleTransitionEnd(event);
147145
}
148146

@@ -305,14 +303,18 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
305303

306304
constructor(
307305
public _changeDetectorRef: ChangeDetectorRef,
308-
readonly _elementRef: ElementRef,
306+
readonly _elementRef: ElementRef<HTMLElement>,
309307
protected _ngZone: NgZone,
310308
@Optional() private _dir: Directionality,
311309
// @breaking-change 8.0.0 `animationMode` parameter to become required.
312310
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
313311
super(_elementRef);
314312
this._chipFoundation = new MDCChipFoundation(this._chipAdapter);
315313
this._animationsDisabled = animationMode === 'NoopAnimations';
314+
315+
_ngZone.runOutsideAngular(() => {
316+
_elementRef.nativeElement.addEventListener('transitionend', this._handleTransitionEnd);
317+
});
316318
}
317319

318320
ngAfterContentInit() {
@@ -327,6 +329,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
327329
this.destroyed.emit({chip: this});
328330
this._destroyed.next();
329331
this._destroyed.complete();
332+
this._elementRef.nativeElement.removeEventListener('transitionend', this._handleTransitionEnd);
330333
this._chipFoundation.destroy();
331334
}
332335

0 commit comments

Comments
 (0)