@@ -38,7 +38,7 @@ import {Subject} from 'rxjs/Subject';
38
38
import { MdSnackBarConfig } from './snack-bar-config' ;
39
39
40
40
41
- export type SnackBarState = 'initial' | ' visible' | 'complete ' | 'void' ;
41
+ export type SnackBarState = 'visible' | 'hidden ' | 'void' ;
42
42
43
43
// TODO(jelbourn): we can't use constants from animation.ts here because you can't use
44
44
// a text interpolation in anything that is analyzed statically with ngc (for AoT compile).
@@ -59,15 +59,22 @@ export const HIDE_ANIMATION = '195ms cubic-bezier(0.0,0.0,0.2,1)';
59
59
host : {
60
60
'role' : 'alert' ,
61
61
'class' : 'mat-snack-bar-container' ,
62
- '[@state]' : 'animationState ' ,
62
+ '[@state]' : 'getAnimationState() ' ,
63
63
'(@state.done)' : 'onAnimationEnd($event)'
64
64
} ,
65
65
animations : [
66
66
trigger ( 'state' , [
67
- state ( 'void, initial, complete' , style ( { transform : 'translateY(100%)' } ) ) ,
68
- state ( 'visible' , style ( { transform : 'translateY(0%)' } ) ) ,
69
- transition ( 'visible => complete' , animate ( HIDE_ANIMATION ) ) ,
70
- transition ( 'initial => visible, void => visible' , animate ( SHOW_ANIMATION ) ) ,
67
+ // Animation from top.
68
+ state ( 'visible-top' , style ( { transform : 'translateY(0%)' } ) ) ,
69
+ state ( 'hidden-top' , style ( { transform : 'translateY(-100%)' } ) ) ,
70
+ transition ( 'visible-top => hidden-top' , animate ( HIDE_ANIMATION ) ) ,
71
+ transition ( 'void => visible-top' , animate ( SHOW_ANIMATION ) ) ,
72
+ // Animation from bottom.
73
+ state ( 'visible-bottom' , style ( { transform : 'translateY(0%)' } ) ) ,
74
+ state ( 'hidden-bottom' , style ( { transform : 'translateY(100%)' } ) ) ,
75
+ transition ( 'visible-bottom => hidden-bottom' , animate ( HIDE_ANIMATION ) ) ,
76
+ transition ( 'void => visible-bottom' ,
77
+ animate ( SHOW_ANIMATION ) ) ,
71
78
] )
72
79
] ,
73
80
} )
@@ -85,7 +92,7 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
85
92
_onEnter : Subject < any > = new Subject ( ) ;
86
93
87
94
/** The state of the snack bar animations. */
88
- animationState : SnackBarState = 'initial' ;
95
+ private _animationState : SnackBarState ;
89
96
90
97
/** The snack bar configuration. */
91
98
snackBarConfig : MdSnackBarConfig ;
@@ -98,6 +105,14 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
98
105
super ( ) ;
99
106
}
100
107
108
+ /**
109
+ * Gets the current animation state both combining one of the possibilities from
110
+ * SnackBarState and the vertical location.
111
+ */
112
+ getAnimationState ( ) : string {
113
+ return `${ this . _animationState } -${ this . snackBarConfig . verticalPosition } ` ;
114
+ }
115
+
101
116
/** Attach a component portal as content to this snack bar container. */
102
117
attachComponentPortal < T > ( portal : ComponentPortal < T > ) : ComponentRef < T > {
103
118
if ( this . _portalHost . hasAttached ( ) ) {
@@ -112,6 +127,14 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
112
127
}
113
128
}
114
129
130
+ if ( this . snackBarConfig . horizontalPosition === 'center' ) {
131
+ this . _renderer . addClass ( this . _elementRef . nativeElement , 'mat-snack-bar-center' ) ;
132
+ }
133
+
134
+ if ( this . snackBarConfig . verticalPosition === 'top' ) {
135
+ this . _renderer . addClass ( this . _elementRef . nativeElement , 'mat-snack-bar-top' ) ;
136
+ }
137
+
115
138
return this . _portalHost . attachComponentPortal ( portal ) ;
116
139
}
117
140
@@ -122,11 +145,11 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
122
145
123
146
/** Handle end of animations, updating the state of the snackbar. */
124
147
onAnimationEnd ( event : AnimationEvent ) {
125
- if ( event . toState === 'void' || event . toState === 'complete' ) {
148
+ if ( event . toState === 'void' || event . toState . startsWith ( 'hidden' ) ) {
126
149
this . _completeExit ( ) ;
127
150
}
128
151
129
- if ( event . toState === 'visible' ) {
152
+ if ( event . toState . startsWith ( 'visible' ) ) {
130
153
// Note: we shouldn't use `this` inside the zone callback,
131
154
// because it can cause a memory leak.
132
155
const onEnter = this . _onEnter ;
@@ -141,14 +164,14 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
141
164
/** Begin animation of snack bar entrance into view. */
142
165
enter ( ) : void {
143
166
if ( ! this . _destroyed ) {
144
- this . animationState = 'visible' ;
167
+ this . _animationState = 'visible' ;
145
168
this . _changeDetectorRef . detectChanges ( ) ;
146
169
}
147
170
}
148
171
149
172
/** Begin animation of the snack bar exiting from view. */
150
173
exit ( ) : Observable < void > {
151
- this . animationState = 'complete ' ;
174
+ this . _animationState = 'hidden ' ;
152
175
return this . _onExit ;
153
176
}
154
177
0 commit comments