Skip to content

Commit

Permalink
rafactor(material/button): minor optimizations to button-base (#27949)
Browse files Browse the repository at this point in the history
Use vararg version of classList.add which has been allowed since cac8359

also don't use a loop to check the attribute, it's only ever a single thing.

(cherry picked from commit f8c6b40)
  • Loading branch information
esprehn authored and crisbeto committed Oct 19, 2023
1 parent c6fa905 commit defd48d
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/material/button/button-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,33 @@ export const MAT_BUTTON_HOST = {
};

/** List of classes to add to buttons instances based on host attribute selector. */
const HOST_SELECTOR_MDC_CLASS_PAIR: {selector: string; mdcClasses: string[]}[] = [
const HOST_SELECTOR_MDC_CLASS_PAIR: {attribute: string; mdcClasses: string[]}[] = [
{
selector: 'mat-button',
attribute: 'mat-button',
mdcClasses: ['mdc-button', 'mat-mdc-button'],
},
{
selector: 'mat-flat-button',
attribute: 'mat-flat-button',
mdcClasses: ['mdc-button', 'mdc-button--unelevated', 'mat-mdc-unelevated-button'],
},
{
selector: 'mat-raised-button',
attribute: 'mat-raised-button',
mdcClasses: ['mdc-button', 'mdc-button--raised', 'mat-mdc-raised-button'],
},
{
selector: 'mat-stroked-button',
attribute: 'mat-stroked-button',
mdcClasses: ['mdc-button', 'mdc-button--outlined', 'mat-mdc-outlined-button'],
},
{
selector: 'mat-fab',
attribute: 'mat-fab',
mdcClasses: ['mdc-fab', 'mat-mdc-fab'],
},
{
selector: 'mat-mini-fab',
attribute: 'mat-mini-fab',
mdcClasses: ['mdc-fab', 'mdc-fab--mini', 'mat-mdc-mini-fab'],
},
{
selector: 'mat-icon-button',
attribute: 'mat-icon-button',
mdcClasses: ['mdc-icon-button', 'mat-mdc-icon-button'],
},
];
Expand Down Expand Up @@ -128,15 +128,14 @@ export class MatButtonBase implements AfterViewInit, OnDestroy {
className: 'mat-mdc-button-ripple',
});

const classList = (_elementRef.nativeElement as HTMLElement).classList;
const element = this._elementRef.nativeElement;
const classList = (element as HTMLElement).classList;

// For each of the variant selectors that is present in the button's host
// attributes, add the correct corresponding MDC classes.
for (const pair of HOST_SELECTOR_MDC_CLASS_PAIR) {
if (this._hasHostAttributes(pair.selector)) {
pair.mdcClasses.forEach((className: string) => {
classList.add(className);
});
for (const {attribute, mdcClasses} of HOST_SELECTOR_MDC_CLASS_PAIR) {
if (element.hasAttribute(attribute)) {
classList.add(...mdcClasses);
}
}
}
Expand All @@ -158,11 +157,6 @@ export class MatButtonBase implements AfterViewInit, OnDestroy {
}
}

/** Gets whether the button has one of the given attributes. */
private _hasHostAttributes(...attributes: string[]) {
return attributes.some(attribute => this._elementRef.nativeElement.hasAttribute(attribute));
}

private _updateRippleDisabled(): void {
this._rippleLoader?.setDisabled(
this._elementRef.nativeElement,
Expand Down

0 comments on commit defd48d

Please sign in to comment.