Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(material/button): simplify host bindings #30630

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 18 additions & 22 deletions src/material/button/button-base.ts
Original file line number Diff line number Diff line change
@@ -45,34 +45,30 @@ export interface MatButtonConfig {
/** Injection token that can be used to provide the default options the button component. */
export const MAT_BUTTON_CONFIG = new InjectionToken<MatButtonConfig>('MAT_BUTTON_CONFIG');

/** Shared host configuration for all buttons */
export const MAT_BUTTON_HOST = {
'[attr.disabled]': '_getDisabledAttribute()',
'[attr.aria-disabled]': '_getAriaDisabled()',
'[class.mat-mdc-button-disabled]': 'disabled',
'[class.mat-mdc-button-disabled-interactive]': 'disabledInteractive',
'[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
// MDC automatically applies the primary theme color to the button, but we want to support
// an unthemed version. If color is undefined, apply a CSS class that makes it easy to
// select and style this "theme".
'[class.mat-unthemed]': '!color',
// Add a class that applies to all buttons. This makes it easier to target if somebody
// wants to target all Material buttons.
'[class.mat-mdc-button-base]': 'true',
'[class]': 'color ? "mat-" + color : ""',
'[attr.tabindex]': '_getTabIndex()',
};

function transformTabIndex(value: unknown): number | undefined {
return value == null ? undefined : numberAttribute(value);
}

/** Base class for all buttons. */
@Directive()
/** Base class for all buttons. */
@Directive({
host: {
// Add a class that applies to all buttons. This makes it easier to target if somebody
// wants to target all Material buttons.
'class': 'mat-mdc-button-base',
'[class]': 'color ? "mat-" + color : ""',
'[attr.disabled]': '_getDisabledAttribute()',
'[attr.aria-disabled]': '_getAriaDisabled()',
'[attr.tabindex]': '_getTabIndex()',
'[class.mat-mdc-button-disabled]': 'disabled',
'[class.mat-mdc-button-disabled-interactive]': 'disabledInteractive',
'[class.mat-unthemed]': '!color',
'[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
},
})
export class MatButtonBase implements AfterViewInit, OnDestroy {
_elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
_ngZone = inject(NgZone);
_animationMode = inject(ANIMATION_MODULE_TYPE, {optional: true});
protected _ngZone = inject(NgZone);
protected _animationMode = inject(ANIMATION_MODULE_TYPE, {optional: true});

protected readonly _config = inject(MAT_BUTTON_CONFIG, {optional: true});
private readonly _focusMonitor = inject(FocusMonitor);
12 changes: 5 additions & 7 deletions src/material/button/button.ts
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
*/

import {ChangeDetectionStrategy, Component, Input, ViewEncapsulation} from '@angular/core';
import {MAT_BUTTON_HOST, MatButtonAppearance, MatButtonBase} from './button-base';
import {MatButtonAppearance, MatButtonBase} from './button-base';

/**
* Classes that need to be set for each appearance of the button.
@@ -32,7 +32,9 @@ const APPEARANCE_CLASSES: Map<MatButtonAppearance, readonly string[]> = new Map(
`,
templateUrl: 'button.html',
styleUrls: ['button.css', 'button-high-contrast.css'],
host: MAT_BUTTON_HOST,
host: {
'class': 'mdc-button',
},
exportAs: 'matButton, matAnchor',
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
@@ -54,11 +56,7 @@ export class MatButton extends MatButtonBase {

constructor() {
super();
const element = this._elementRef.nativeElement;
const inferredAppearance = _inferAppearance(element);

// This class is common across all the appearances so we add it ahead of time.
element.classList.add('mdc-button');
const inferredAppearance = _inferAppearance(this._elementRef.nativeElement);

// Only set the appearance if we managed to infer it from the static attributes, rather than
// doing something like `setAppearance(inferredAppearance || 'text')`, because doing so can
12 changes: 5 additions & 7 deletions src/material/button/fab.ts
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ import {
inject,
} from '@angular/core';

import {MAT_BUTTON_HOST, MatButtonBase} from './button-base';
import {MatButtonBase} from './button-base';
import {ThemePalette} from '../core';

/** Default FAB options that can be overridden. */
@@ -67,7 +67,7 @@ const defaults = MAT_FAB_DEFAULT_OPTIONS_FACTORY();
templateUrl: 'button.html',
styleUrl: 'fab.css',
host: {
...MAT_BUTTON_HOST,
'class': 'mdc-fab mat-mdc-fab-base mat-mdc-fab',
'[class.mdc-fab--extended]': 'extended',
'[class.mat-mdc-extended-fab]': 'extended',
},
@@ -86,8 +86,6 @@ export class MatFabButton extends MatButtonBase {

constructor() {
super();
const element = this._elementRef.nativeElement;
element.classList.add('mdc-fab', 'mat-mdc-fab-base', 'mat-mdc-fab');
this._options = this._options || defaults;
this.color = this._options!.color || defaults.color;
}
@@ -102,7 +100,9 @@ export class MatFabButton extends MatButtonBase {
selector: `button[mat-mini-fab], a[mat-mini-fab], button[matMiniFab], a[matMiniFab]`,
templateUrl: 'button.html',
styleUrl: 'fab.css',
host: MAT_BUTTON_HOST,
host: {
'class': 'mdc-fab mat-mdc-fab-base mdc-fab--mini mat-mdc-mini-fab',
},
exportAs: 'matButton, matAnchor',
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
@@ -116,8 +116,6 @@ export class MatMiniFabButton extends MatButtonBase {

constructor() {
super();
const element = this._elementRef.nativeElement;
element.classList.add('mdc-fab', 'mat-mdc-fab-base', 'mdc-fab--mini', 'mat-mdc-mini-fab');
this._options = this._options || defaults;
this.color = this._options!.color || defaults.color;
}
10 changes: 5 additions & 5 deletions src/material/button/icon-button.ts
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
*/

import {ChangeDetectionStrategy, Component, ViewEncapsulation} from '@angular/core';
import {MAT_BUTTON_HOST, MatButtonBase} from './button-base';
import {MatButtonBase} from './button-base';

/**
* Material Design icon button component. This type of button displays a single interactive icon for
@@ -18,7 +18,9 @@ import {MAT_BUTTON_HOST, MatButtonBase} from './button-base';
selector: `button[mat-icon-button], a[mat-icon-button], button[matIconButton], a[matIconButton]`,
templateUrl: 'icon-button.html',
styleUrls: ['icon-button.css', 'button-high-contrast.css'],
host: MAT_BUTTON_HOST,
host: {
'class': 'mdc-icon-button mat-mdc-icon-button',
},
exportAs: 'matButton, matAnchor',
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
@@ -28,9 +30,7 @@ export class MatIconButton extends MatButtonBase {

constructor() {
super();
const element = this._elementRef.nativeElement;
element.classList.add('mdc-icon-button', 'mat-mdc-icon-button');
this._rippleLoader.configureRipple(element, {centered: true});
this._rippleLoader.configureRipple(this._elementRef.nativeElement, {centered: true});
}
}

7 changes: 0 additions & 7 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -111,13 +111,6 @@
}
],
"Directive": [
{
"argument": 0,
"properties": {
"!host": "\\[class\\]"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context for this lint check is that [class] bindings in ViewEngine would clobber static classes. This isn't the case with Ivy and we actually had a binding like this for a while in the MAT_BUTTON_HOST constant, the lint rule just wasn't picking it up.

},
"excludeFiles": ["**/dev-app/**"]
},
// Enforce standalone even in the dev-app.
{
"argument": 0,
Loading