Skip to content

Commit 2460ff6

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 9343d08 commit 2460ff6

File tree

1 file changed

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

1 file changed

+11
-9
lines changed

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

Lines changed: 11 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,
@@ -139,13 +138,11 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
139138
/** Whether animations for the chip are enabled. */
140139
_animationsDisabled: boolean;
141140

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

@@ -308,14 +305,18 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
308305

309306
constructor(
310307
public _changeDetectorRef: ChangeDetectorRef,
311-
readonly _elementRef: ElementRef,
308+
readonly _elementRef: ElementRef<HTMLElement>,
312309
protected _ngZone: NgZone,
313310
@Optional() private _dir: Directionality,
314311
// @breaking-change 8.0.0 `animationMode` parameter to become required.
315312
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
316313
super(_elementRef);
317314
this._chipFoundation = new MDCChipFoundation(this._chipAdapter);
318315
this._animationsDisabled = animationMode === 'NoopAnimations';
316+
317+
_ngZone.runOutsideAngular(() => {
318+
_elementRef.nativeElement.addEventListener('transitionend', this._handleTransitionEnd);
319+
});
319320
}
320321

321322
ngAfterContentInit() {
@@ -330,6 +331,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
330331
this.destroyed.emit({chip: this});
331332
this._destroyed.next();
332333
this._destroyed.complete();
334+
this._elementRef.nativeElement.removeEventListener('transitionend', this._handleTransitionEnd);
333335
this._chipFoundation.destroy();
334336
}
335337

0 commit comments

Comments
 (0)