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