@@ -10,7 +10,15 @@ import {LiveAnnouncer} from '@angular/cdk/a11y';
10
10
import { BreakpointObserver , Breakpoints } from '@angular/cdk/layout' ;
11
11
import { Overlay , OverlayConfig , OverlayRef } from '@angular/cdk/overlay' ;
12
12
import { 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' ;
14
22
import { take } from 'rxjs/operators/take' ;
15
23
import { takeUntil } from 'rxjs/operators/takeUntil' ;
16
24
import { SimpleSnackBar } from './simple-snack-bar' ;
@@ -19,6 +27,10 @@ import {MatSnackBarContainer} from './snack-bar-container';
19
27
import { MatSnackBarRef } from './snack-bar-ref' ;
20
28
21
29
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
+
22
34
/**
23
35
* Service to dispatch Material Design snack bar messages.
24
36
*/
@@ -50,7 +62,8 @@ export class MatSnackBar {
50
62
private _live : LiveAnnouncer ,
51
63
private _injector : Injector ,
52
64
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 ) { }
54
67
55
68
/**
56
69
* Creates and dispatches a snack bar with a custom component for the content, removing any
@@ -60,7 +73,7 @@ export class MatSnackBar {
60
73
* @param config Extra configuration for the snack bar.
61
74
*/
62
75
openFromComponent < T > ( component : ComponentType < T > , config ?: MatSnackBarConfig ) : MatSnackBarRef < T > {
63
- const _config = _applyConfigDefaults ( config ) ;
76
+ const _config = { ... this . _defaultConfig , ... config } ;
64
77
const snackBarRef = this . _attach ( component , _config ) ;
65
78
66
79
// When the snackbar is dismissed, clear the reference to it.
@@ -104,7 +117,7 @@ export class MatSnackBar {
104
117
*/
105
118
open ( message : string , action : string = '' , config ?: MatSnackBarConfig ) :
106
119
MatSnackBarRef < SimpleSnackBar > {
107
- const _config = _applyConfigDefaults ( config ) ;
120
+ const _config = { ... this . _defaultConfig , ... config } ;
108
121
109
122
// Since the user doesn't have access to the component, we can
110
123
// override the data to pass in our own message and action.
@@ -216,12 +229,3 @@ export class MatSnackBar {
216
229
return new PortalInjector ( userInjector || this . _injector , injectionTokens ) ;
217
230
}
218
231
}
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