Skip to content

Commit 262e2e4

Browse files
authored
perf(material-experimental/mdc-chips): avoid checking the DOM on each change detection (#18929)
The MDC-based chip has a check called `_isBasicChip` which is called on each change detection, however its result won't change since it's based on the element's node name and a static attribute. These change resolve the value once and use a property instead.
1 parent 09d0b1f commit 262e2e4

File tree

1 file changed

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

1 file changed

+9
-10
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ const _MatChipMixinBase:
107107
'[class.mat-mdc-chip-highlighted]': 'highlighted',
108108
'[class.mat-mdc-chip-with-avatar]': 'leadingIcon',
109109
'[class.mat-mdc-chip-with-trailing-icon]': 'trailingIcon || removeIcon',
110-
'[class.mat-mdc-basic-chip]': '_isBasicChip()',
111-
'[class.mat-mdc-standard-chip]': '!_isBasicChip()',
110+
'[class.mat-mdc-basic-chip]': '_isBasicChip',
111+
'[class.mat-mdc-standard-chip]': '!_isBasicChip',
112112
'[class._mat-animation-noopable]': '_animationsDisabled',
113113
'[id]': 'id',
114114
'[attr.disabled]': 'disabled || null',
@@ -133,6 +133,9 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
133133

134134
readonly HANDLED_KEYS: number[] = [];
135135

136+
/** Whether this chip is a basic (unstyled) chip. */
137+
readonly _isBasicChip: boolean;
138+
136139
/** Whether the chip has focus. */
137140
protected _hasFocusInternal = false;
138141

@@ -316,6 +319,9 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
316319
super(_elementRef);
317320
this._chipFoundation = new MDCChipFoundation(this._chipAdapter);
318321
this._animationsDisabled = animationMode === 'NoopAnimations';
322+
this._isBasicChip = _elementRef.nativeElement.hasAttribute(this.basicChipAttrName) ||
323+
_elementRef.nativeElement.tagName.toLowerCase() === this.basicChipAttrName;
324+
319325
}
320326

321327
ngAfterContentInit() {
@@ -382,13 +388,6 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
382388
}
383389
}
384390

385-
/** Whether this chip is a basic (unstyled) chip. */
386-
_isBasicChip() {
387-
const element = this._elementRef.nativeElement as HTMLElement;
388-
return element.hasAttribute(this.basicChipAttrName) ||
389-
element.tagName.toLowerCase() === this.basicChipAttrName;
390-
}
391-
392391
/** Sets whether the given CSS class should be applied to the MDC chip. */
393392
private _setMdcClass(cssClass: string, active: boolean) {
394393
const classes = this._elementRef.nativeElement.classList;
@@ -405,7 +404,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
405404

406405
/** Whether or not the ripple should be disabled. */
407406
_isRippleDisabled(): boolean {
408-
return this.disabled || this.disableRipple || this._isBasicChip();
407+
return this.disabled || this.disableRipple || this._isBasicChip;
409408
}
410409

411410
static ngAcceptInputType_disabled: BooleanInput;

0 commit comments

Comments
 (0)