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

feat(material/button): allow button color to be configured through DI #29297

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion src/material/button/button-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ import {
OnDestroy,
OnInit,
} from '@angular/core';
import {MatRipple, MatRippleLoader} from '@angular/material/core';
import {MatRipple, MatRippleLoader, ThemePalette} from '@angular/material/core';

/** Object that can be used to configure the default options for the button component. */
export interface MatButtonConfig {
/** Whether disabled buttons should be interactive. */
disabledInteractive?: boolean;

/** Default palette color to apply to buttons. */
color?: ThemePalette;
}

/** Injection token that can be used to provide the default options the button component. */
Expand Down Expand Up @@ -167,6 +170,7 @@ export class MatButtonBase implements AfterViewInit, OnDestroy {
const classList = (element as HTMLElement).classList;

this.disabledInteractive = config?.disabledInteractive ?? false;
this.color = config?.color ?? null;
this._rippleLoader?.configureRipple(element, {className: 'mat-mdc-button-ripple'});

// For each of the variant selectors that is present in the button's host
Expand Down
32 changes: 31 additions & 1 deletion src/material/button/button.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import {ApplicationRef, Component, DebugElement} from '@angular/core';
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
import {MatRipple, ThemePalette} from '@angular/material/core';
import {By} from '@angular/platform-browser';
import {MAT_FAB_DEFAULT_OPTIONS, MatButton, MatButtonModule, MatFabDefaultOptions} from './index';
import {
MAT_BUTTON_CONFIG,
MAT_FAB_DEFAULT_OPTIONS,
MatButton,
MatButtonModule,
MatFabDefaultOptions,
} from './index';

describe('MDC-based MatButton', () => {
beforeEach(waitForAsync(() => {
Expand Down Expand Up @@ -374,6 +380,30 @@ describe('MDC-based MatButton', () => {
).toBe(true);
});

it('should be able to configure the default color of buttons', () => {
@Component({
template: `<button mat-button>Click me</button>`,
standalone: true,
imports: [MatButtonModule],
})
class ConfigTestApp {}

TestBed.resetTestingModule();
TestBed.configureTestingModule({
imports: [MatButtonModule, ConfigTestApp],
providers: [
{
provide: MAT_BUTTON_CONFIG,
useValue: {color: 'warn'},
},
],
});
const fixture = TestBed.createComponent(ConfigTestApp);
fixture.detectChanges();
const button = fixture.nativeElement.querySelector('button');
expect(button.classList).toContain('mat-warn');
});

describe('interactive disabled buttons', () => {
let fixture: ComponentFixture<TestApp>;
let button: HTMLButtonElement;
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/material/button.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class MatButton extends MatButtonBase {

// @public
export interface MatButtonConfig {
color?: ThemePalette;
disabledInteractive?: boolean;
}

Expand Down
Loading