@@ -10,7 +10,15 @@ import {LiveAnnouncer} from '@angular/cdk/a11y';
1010import { BreakpointObserver , Breakpoints } from '@angular/cdk/layout' ;
1111import { Overlay , OverlayConfig , OverlayRef } from '@angular/cdk/overlay' ;
1212import { ComponentPortal , ComponentType , PortalInjector } from '@angular/cdk/portal' ;
13- import { ComponentRef , Injectable , Injector , Optional , SkipSelf } from '@angular/core' ;
13+ import {
14+ ComponentRef ,
15+ Inject ,
16+ Injectable ,
17+ Injector ,
18+ InjectionToken ,
19+ Optional ,
20+ SkipSelf ,
21+ } from '@angular/core' ;
1422import { take } from 'rxjs/operators/take' ;
1523import { takeUntil } from 'rxjs/operators/takeUntil' ;
1624import { SimpleSnackBar } from './simple-snack-bar' ;
@@ -19,6 +27,10 @@ import {MatSnackBarContainer} from './snack-bar-container';
1927import { MatSnackBarRef } from './snack-bar-ref' ;
2028
2129
30+ /** Injection token that can be used to specify default snack bar. */
31+ export const MAT_SNACK_BAR_DEFAULT_OPTIONS =
32+ new InjectionToken < MatSnackBarConfig > ( 'mat-snack-bar-default-options' ) ;
33+
2234/**
2335 * Service to dispatch Material Design snack bar messages.
2436 */
@@ -50,7 +62,8 @@ export class MatSnackBar {
5062 private _live : LiveAnnouncer ,
5163 private _injector : Injector ,
5264 private _breakpointObserver : BreakpointObserver ,
53- @Optional ( ) @SkipSelf ( ) private _parentSnackBar : MatSnackBar ) { }
65+ @Optional ( ) @SkipSelf ( ) private _parentSnackBar : MatSnackBar ,
66+ @Inject ( MAT_SNACK_BAR_DEFAULT_OPTIONS ) private _defaultConfig : MatSnackBarConfig ) { }
5467
5568 /**
5669 * Creates and dispatches a snack bar with a custom component for the content, removing any
@@ -60,7 +73,7 @@ export class MatSnackBar {
6073 * @param config Extra configuration for the snack bar.
6174 */
6275 openFromComponent < T > ( component : ComponentType < T > , config ?: MatSnackBarConfig ) : MatSnackBarRef < T > {
63- const _config = _applyConfigDefaults ( config ) ;
76+ const _config = { ... this . _defaultConfig , ... config } ;
6477 const snackBarRef = this . _attach ( component , _config ) ;
6578
6679 // When the snackbar is dismissed, clear the reference to it.
@@ -104,7 +117,7 @@ export class MatSnackBar {
104117 */
105118 open ( message : string , action : string = '' , config ?: MatSnackBarConfig ) :
106119 MatSnackBarRef < SimpleSnackBar > {
107- const _config = _applyConfigDefaults ( config ) ;
120+ const _config = { ... this . _defaultConfig , ... config } ;
108121
109122 // Since the user doesn't have access to the component, we can
110123 // override the data to pass in our own message and action.
@@ -216,12 +229,3 @@ export class MatSnackBar {
216229 return new PortalInjector ( userInjector || this . _injector , injectionTokens ) ;
217230 }
218231}
219-
220- /**
221- * Applies default options to the snackbar config.
222- * @param config The configuration to which the defaults will be applied.
223- * @returns The new configuration object with defaults applied.
224- */
225- function _applyConfigDefaults ( config ?: MatSnackBarConfig ) : MatSnackBarConfig {
226- return { ...new MatSnackBarConfig ( ) , ...config } ;
227- }
0 commit comments