Skip to content

Commit 7e45c83

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 948dfa9 commit 7e45c83

File tree

1 file changed

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

1 file changed

+13
-10
lines changed

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

Lines changed: 13 additions & 10 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

@@ -334,7 +331,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
334331

335332
constructor(
336333
public _changeDetectorRef: ChangeDetectorRef,
337-
readonly _elementRef: ElementRef, protected _ngZone: NgZone,
334+
readonly _elementRef: ElementRef<HTMLElement>, protected _ngZone: NgZone,
338335
@Optional() private _dir: Directionality,
339336
// @breaking-change 8.0.0 `animationMode` parameter to become required.
340337
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
@@ -343,6 +340,10 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
343340
this._animationsDisabled = animationMode === 'NoopAnimations';
344341
this._isBasicChip = _elementRef.nativeElement.hasAttribute(this.basicChipAttrName) ||
345342
_elementRef.nativeElement.tagName.toLowerCase() === this.basicChipAttrName;
343+
344+
_ngZone.runOutsideAngular(() => {
345+
_elementRef.nativeElement.addEventListener('transitionend', this._handleTransitionEnd);
346+
});
346347
}
347348

348349
ngAfterContentInit() {
@@ -351,13 +352,15 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
351352

352353
ngAfterViewInit() {
353354
this._chipFoundation.init();
354-
this._textElement = this._elementRef.nativeElement.querySelector('.mdc-chip__text');
355+
this._textElement =
356+
this._elementRef.nativeElement.querySelector('.mdc-chip__text') as HTMLElement;
355357
}
356358

357359
ngOnDestroy() {
358360
this.destroyed.emit({chip: this});
359361
this._destroyed.next();
360362
this._destroyed.complete();
363+
this._elementRef.nativeElement.removeEventListener('transitionend', this._handleTransitionEnd);
361364
this._chipFoundation.destroy();
362365
}
363366

0 commit comments

Comments
 (0)