Skip to content

Commit 17f66d6

Browse files
authored
feat(material-experimental/mdc-button): add default config for FAB (#22102)
1 parent 4aa48a1 commit 17f66d6

File tree

2 files changed

+71
-17
lines changed

2 files changed

+71
-17
lines changed

src/material-experimental/mdc-button/button.spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {waitForAsync, ComponentFixture, TestBed} from '@angular/core/testing';
22
import {Component, DebugElement} from '@angular/core';
33
import {By} from '@angular/platform-browser';
4-
import {MatButtonModule, MatButton} from './index';
4+
import {MatButtonModule, MatButton, MatFabDefaultOptions, MAT_FAB_DEFAULT_OPTIONS} from './index';
55
import {MatRipple, ThemePalette} from '@angular/material-experimental/mdc-core';
66

77

@@ -284,6 +284,35 @@ describe('MDC-based MatButton', () => {
284284
});
285285
});
286286

287+
describe('MatFabDefaultOptions', () => {
288+
function configure(defaults: MatFabDefaultOptions) {
289+
TestBed.configureTestingModule({
290+
imports: [MatButtonModule],
291+
declarations: [TestApp],
292+
providers: [{provide: MAT_FAB_DEFAULT_OPTIONS, useValue: defaults}]
293+
});
294+
295+
TestBed.compileComponents();
296+
}
297+
298+
it('should override default color in component', () => {
299+
configure({color: 'primary'});
300+
const fixture: ComponentFixture<TestApp> = TestBed.createComponent(TestApp);
301+
fixture.detectChanges();
302+
const fabButtonDebugEl = fixture.debugElement.query(By.css('button[mat-fab]'))!;
303+
expect(fabButtonDebugEl.nativeElement.classList).toContain('mat-primary');
304+
});
305+
306+
it('should default to accent if config does not specify color', () => {
307+
configure({});
308+
const fixture: ComponentFixture<TestApp> = TestBed.createComponent(TestApp);
309+
fixture.detectChanges();
310+
const fabButtonDebugEl = fixture.debugElement.query(By.css('button[mat-fab]'))!;
311+
expect(fabButtonDebugEl.nativeElement.classList).toContain('mat-accent');
312+
});
313+
});
314+
315+
287316
/** Test component that contains an MatButton. */
288317
@Component({
289318
selector: 'test-app',

src/material-experimental/mdc-button/fab.ts

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
Component,
1313
ElementRef,
1414
Inject,
15+
InjectionToken,
1516
NgZone,
1617
Optional,
1718
ViewEncapsulation
@@ -29,6 +30,30 @@ import {
2930
import {ThemePalette} from '@angular/material-experimental/mdc-core';
3031
import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
3132

33+
34+
/** Default FAB options that can be overridden. */
35+
export interface MatFabDefaultOptions {
36+
color?: ThemePalette;
37+
}
38+
39+
/** Injection token to be used to override the default options for FAB. */
40+
export const MAT_FAB_DEFAULT_OPTIONS =
41+
new InjectionToken<MatFabDefaultOptions>('mat-mdc-fab-default-options', {
42+
providedIn: 'root',
43+
factory: MAT_FAB_DEFAULT_OPTIONS_FACTORY
44+
});
45+
46+
/** @docs-private */
47+
export function MAT_FAB_DEFAULT_OPTIONS_FACTORY(): MatFabDefaultOptions {
48+
return {
49+
// The FAB by default has its color set to accent.
50+
color: 'accent',
51+
};
52+
}
53+
54+
// Default FAB configuration.
55+
const defaults = MAT_FAB_DEFAULT_OPTIONS_FACTORY();
56+
3257
/**
3358
* Material Design floating action button (FAB) component. These buttons represent the primary
3459
* or most common action for users to interact with.
@@ -60,9 +85,6 @@ import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
6085
changeDetection: ChangeDetectionStrategy.OnPush,
6186
})
6287
export class MatFabButton extends MatButtonBase {
63-
// The FAB by default has its color set to accent.
64-
color = 'accent' as ThemePalette;
65-
6688
_isFab = true;
6789

6890
private _extended: boolean;
@@ -71,8 +93,11 @@ export class MatFabButton extends MatButtonBase {
7193

7294
constructor(
7395
elementRef: ElementRef, platform: Platform, ngZone: NgZone,
74-
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
96+
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string,
97+
@Optional() @Inject(MAT_FAB_DEFAULT_OPTIONS) private _options?: MatFabDefaultOptions) {
7598
super(elementRef, platform, ngZone, animationMode);
99+
this._options = this._options || defaults;
100+
this.color = this.defaultColor = this._options!.color || defaults.color;
76101
}
77102

78103
static ngAcceptInputType_extended: BooleanInput;
@@ -94,15 +119,15 @@ export class MatFabButton extends MatButtonBase {
94119
changeDetection: ChangeDetectionStrategy.OnPush,
95120
})
96121
export class MatMiniFabButton extends MatButtonBase {
97-
// The FAB by default has its color set to accent.
98-
color = 'accent' as ThemePalette;
99-
100122
_isFab = true;
101123

102124
constructor(
103125
elementRef: ElementRef, platform: Platform, ngZone: NgZone,
104-
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
126+
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string,
127+
@Optional() @Inject(MAT_FAB_DEFAULT_OPTIONS) private _options?: MatFabDefaultOptions) {
105128
super(elementRef, platform, ngZone, animationMode);
129+
this._options = this._options || defaults;
130+
this.color = this.defaultColor = this._options!.color || defaults.color;
106131
}
107132
}
108133

@@ -144,9 +169,6 @@ export class MatMiniFabButton extends MatButtonBase {
144169
changeDetection: ChangeDetectionStrategy.OnPush,
145170
})
146171
export class MatFabAnchor extends MatAnchor {
147-
// The FAB by default has its color set to accent.
148-
color = 'accent' as ThemePalette;
149-
150172
_isFab = true;
151173

152174
private _extended: boolean;
@@ -156,8 +178,11 @@ export class MatFabAnchor extends MatAnchor {
156178

157179
constructor(
158180
elementRef: ElementRef, platform: Platform, ngZone: NgZone,
159-
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
181+
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string,
182+
@Optional() @Inject(MAT_FAB_DEFAULT_OPTIONS) private _options?: MatFabDefaultOptions) {
160183
super(elementRef, platform, ngZone, animationMode);
184+
this._options = this._options || defaults;
185+
this.color = this.defaultColor = this._options!.color || defaults.color;
161186
}
162187

163188
static ngAcceptInputType_extended: BooleanInput;
@@ -179,14 +204,14 @@ export class MatFabAnchor extends MatAnchor {
179204
changeDetection: ChangeDetectionStrategy.OnPush,
180205
})
181206
export class MatMiniFabAnchor extends MatAnchor {
182-
// The FAB by default has its color set to accent.
183-
color = 'accent' as ThemePalette;
184-
185207
_isFab = true;
186208

187209
constructor(
188210
elementRef: ElementRef, platform: Platform, ngZone: NgZone,
189-
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
211+
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string,
212+
@Optional() @Inject(MAT_FAB_DEFAULT_OPTIONS) private _options?: MatFabDefaultOptions) {
190213
super(elementRef, platform, ngZone, animationMode);
214+
this._options = this._options || defaults;
215+
this.color = this.defaultColor = this._options!.color || defaults.color;
191216
}
192217
}

0 commit comments

Comments
 (0)