Skip to content

Commit ad3f743

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 695dde6 commit ad3f743

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

@@ -312,7 +309,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
312309

313310
constructor(
314311
public _changeDetectorRef: ChangeDetectorRef,
315-
readonly _elementRef: ElementRef,
312+
readonly _elementRef: ElementRef<HTMLElement>,
316313
protected _ngZone: NgZone,
317314
@Optional() private _dir: Directionality,
318315
// @breaking-change 8.0.0 `animationMode` parameter to become required.
@@ -322,6 +319,10 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
322319
this._animationsDisabled = animationMode === 'NoopAnimations';
323320
this._isBasicChip = _elementRef.nativeElement.hasAttribute(this.basicChipAttrName) ||
324321
_elementRef.nativeElement.tagName.toLowerCase() === this.basicChipAttrName;
322+
323+
_ngZone.runOutsideAngular(() => {
324+
_elementRef.nativeElement.addEventListener('transitionend', this._handleTransitionEnd);
325+
});
325326
}
326327

327328
ngAfterContentInit() {
@@ -330,13 +331,15 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
330331

331332
ngAfterViewInit() {
332333
this._chipFoundation.init();
333-
this._textElement = this._elementRef.nativeElement.querySelector('.mdc-chip__text');
334+
this._textElement =
335+
this._elementRef.nativeElement.querySelector('.mdc-chip__text') as HTMLElement;
334336
}
335337

336338
ngOnDestroy() {
337339
this.destroyed.emit({chip: this});
338340
this._destroyed.next();
339341
this._destroyed.complete();
342+
this._elementRef.nativeElement.removeEventListener('transitionend', this._handleTransitionEnd);
340343
this._chipFoundation.destroy();
341344
}
342345

0 commit comments

Comments
 (0)