Skip to content

Commit 2539d17

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 a30094b commit 2539d17

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
@@ -26,7 +26,7 @@ import {
2626
OnDestroy,
2727
Optional,
2828
Output,
29-
ViewEncapsulation, HostListener
29+
ViewEncapsulation,
3030
} from '@angular/core';
3131
import {
3232
CanColor,
@@ -128,13 +128,11 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
128128
/** Whether animations for the chip are enabled. */
129129
_animationsDisabled: boolean;
130130

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

@@ -296,7 +294,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
296294

297295
constructor(
298296
public _changeDetectorRef: ChangeDetectorRef,
299-
readonly _elementRef: ElementRef,
297+
readonly _elementRef: ElementRef<HTMLElement>,
300298
private _platform: Platform,
301299
protected _ngZone: NgZone,
302300
@Optional() @Inject(MAT_RIPPLE_GLOBAL_OPTIONS)
@@ -307,6 +305,10 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
307305
super(_elementRef);
308306
this._chipFoundation = new MDCChipFoundation(this._chipAdapter);
309307
this._animationsDisabled = animationMode === 'NoopAnimations';
308+
309+
_ngZone.runOutsideAngular(() => {
310+
_elementRef.nativeElement.addEventListener('transitionend', this._handleTransitionEnd);
311+
});
310312
}
311313

312314
ngAfterContentInit() {
@@ -323,6 +325,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
323325
this._destroyed.next();
324326
this._destroyed.complete();
325327
this._rippleRenderer._removeTriggerEvents();
328+
this._elementRef.nativeElement.removeEventListener('transitionend', this._handleTransitionEnd);
326329
this._chipFoundation.destroy();
327330
}
328331

0 commit comments

Comments
 (0)