Skip to content

Commit 4253d49

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 8a71288 commit 4253d49

File tree

1 file changed

+10
-9
lines changed
  • src/material-experimental/mdc-chips

1 file changed

+10
-9
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
Directive,
2020
ElementRef,
2121
EventEmitter,
22-
HostListener,
2322
Inject,
2423
Input,
2524
NgZone,
@@ -142,13 +141,11 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
142141
/** Whether animations for the chip are enabled. */
143142
_animationsDisabled: boolean;
144143

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

@@ -311,7 +308,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
311308

312309
constructor(
313310
public _changeDetectorRef: ChangeDetectorRef,
314-
readonly _elementRef: ElementRef,
311+
readonly _elementRef: ElementRef<HTMLElement>,
315312
protected _ngZone: NgZone,
316313
@Optional() private _dir: Directionality,
317314
// @breaking-change 8.0.0 `animationMode` parameter to become required.
@@ -322,6 +319,9 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
322319
this._isBasicChip = _elementRef.nativeElement.hasAttribute(this.basicChipAttrName) ||
323320
_elementRef.nativeElement.tagName.toLowerCase() === this.basicChipAttrName;
324321

322+
_ngZone.runOutsideAngular(() => {
323+
_elementRef.nativeElement.addEventListener('transitionend', this._handleTransitionEnd);
324+
});
325325
}
326326

327327
ngAfterContentInit() {
@@ -336,6 +336,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
336336
this.destroyed.emit({chip: this});
337337
this._destroyed.next();
338338
this._destroyed.complete();
339+
this._elementRef.nativeElement.removeEventListener('transitionend', this._handleTransitionEnd);
339340
this._chipFoundation.destroy();
340341
}
341342

0 commit comments

Comments
 (0)