Skip to content

Commit dfdd323

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 4caf039 commit dfdd323

File tree

1 file changed

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

1 file changed

+12
-9
lines changed

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

Lines changed: 12 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,
@@ -143,13 +142,11 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
143142
/** Whether animations for the chip are enabled. */
144143
_animationsDisabled: boolean;
145144

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

@@ -340,6 +337,9 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
340337
this._animationsDisabled = animationMode === 'NoopAnimations';
341338
this._isBasicChip = elementRef.nativeElement.hasAttribute(this.basicChipAttrName) ||
342339
elementRef.nativeElement.tagName.toLowerCase() === this.basicChipAttrName;
340+
_ngZone.runOutsideAngular(() => {
341+
elementRef.nativeElement.addEventListener('transitionend', this._handleTransitionEnd);
342+
});
343343
}
344344

345345
ngAfterContentInit() {
@@ -348,11 +348,14 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
348348

349349
ngAfterViewInit() {
350350
this._chipFoundation.init();
351-
this._textElement = this._elementRef.nativeElement.querySelector('.mdc-chip__text');
351+
this._textElement =
352+
this._elementRef.nativeElement.querySelector('.mdc-chip__text') as HTMLElement;
352353
}
353354

354355
ngOnDestroy() {
355356
this.destroyed.emit({chip: this});
357+
this.destroyed.complete();
358+
this._elementRef.nativeElement.removeEventListener('transitionend', this._handleTransitionEnd);
356359
this._chipFoundation.destroy();
357360
}
358361

0 commit comments

Comments
 (0)