@@ -12,6 +12,7 @@ import {
1212 Component ,
1313 ElementRef ,
1414 Inject ,
15+ InjectionToken ,
1516 NgZone ,
1617 Optional ,
1718 ViewEncapsulation
@@ -29,6 +30,30 @@ import {
2930import { ThemePalette } from '@angular/material-experimental/mdc-core' ;
3031import { 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} )
6287export 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} )
96121export 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} )
146171export 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} )
181206export 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