Skip to content

Commit 5da528e

Browse files
authored
feat(material/button): allow button color to be configured through DI (angular#29297)
Adds the ability to configure the default button color through the `MAT_BUTTON_CONFIG` injection token.
1 parent 36c627b commit 5da528e

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/material/button/button-base.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ import {
2121
OnDestroy,
2222
OnInit,
2323
} from '@angular/core';
24-
import {MatRipple, MatRippleLoader} from '@angular/material/core';
24+
import {MatRipple, MatRippleLoader, ThemePalette} from '@angular/material/core';
2525

2626
/** Object that can be used to configure the default options for the button component. */
2727
export interface MatButtonConfig {
2828
/** Whether disabled buttons should be interactive. */
2929
disabledInteractive?: boolean;
30+
31+
/** Default palette color to apply to buttons. */
32+
color?: ThemePalette;
3033
}
3134

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

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

172176
// For each of the variant selectors that is present in the button's host

src/material/button/button.spec.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import {ApplicationRef, Component, DebugElement} from '@angular/core';
33
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
44
import {MatRipple, ThemePalette} from '@angular/material/core';
55
import {By} from '@angular/platform-browser';
6-
import {MAT_FAB_DEFAULT_OPTIONS, MatButton, MatButtonModule, MatFabDefaultOptions} from './index';
6+
import {
7+
MAT_BUTTON_CONFIG,
8+
MAT_FAB_DEFAULT_OPTIONS,
9+
MatButton,
10+
MatButtonModule,
11+
MatFabDefaultOptions,
12+
} from './index';
713

814
describe('MDC-based MatButton', () => {
915
beforeEach(waitForAsync(() => {
@@ -374,6 +380,30 @@ describe('MDC-based MatButton', () => {
374380
).toBe(true);
375381
});
376382

383+
it('should be able to configure the default color of buttons', () => {
384+
@Component({
385+
template: `<button mat-button>Click me</button>`,
386+
standalone: true,
387+
imports: [MatButtonModule],
388+
})
389+
class ConfigTestApp {}
390+
391+
TestBed.resetTestingModule();
392+
TestBed.configureTestingModule({
393+
imports: [MatButtonModule, ConfigTestApp],
394+
providers: [
395+
{
396+
provide: MAT_BUTTON_CONFIG,
397+
useValue: {color: 'warn'},
398+
},
399+
],
400+
});
401+
const fixture = TestBed.createComponent(ConfigTestApp);
402+
fixture.detectChanges();
403+
const button = fixture.nativeElement.querySelector('button');
404+
expect(button.classList).toContain('mat-warn');
405+
});
406+
377407
describe('interactive disabled buttons', () => {
378408
let fixture: ComponentFixture<TestApp>;
379409
let button: HTMLButtonElement;

tools/public_api_guard/material/button.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export class MatButton extends MatButtonBase {
4747

4848
// @public
4949
export interface MatButtonConfig {
50+
color?: ThemePalette;
5051
disabledInteractive?: boolean;
5152
}
5253

0 commit comments

Comments
 (0)