Skip to content

Commit 31f92f9

Browse files
josephperrottjelbourn
authored andcommitted
Allow snackbar position to be set to left or center.
1 parent c81e608 commit 31f92f9

File tree

7 files changed

+407
-53
lines changed

7 files changed

+407
-53
lines changed

src/demo-app/snack-bar/snack-bar-demo.html

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ <h1>SnackBar demo</h1>
33
<div>
44
Message: <md-input-container><input mdInput type="text" [(ngModel)]="message"></md-input-container>
55
</div>
6+
<div>
7+
<div>Position in page: </div>
8+
<md-select [(ngModel)]="horizontalPosition">
9+
<md-option value="start">Start</md-option>
10+
<md-option value="end">End</md-option>
11+
<md-option value="left">Left</md-option>
12+
<md-option value="right">Right</md-option>
13+
<md-option value="center">Center</md-option>
14+
</md-select>
15+
<md-select [(ngModel)]="verticalPosition">
16+
<md-option value="top">Top</md-option>
17+
<md-option value="bottom">Bottom</md-option>
18+
</md-select>
19+
</div>
620
<div>
721
<md-checkbox [(ngModel)]="action">
822
<p *ngIf="!action">Show button on snack bar</p>
@@ -27,7 +41,6 @@ <h1>SnackBar demo</h1>
2741
</md-input-container>
2842
</md-checkbox>
2943
</div>
30-
3144
<p>
3245
<md-checkbox [(ngModel)]="addExtraClass">Add extra class to container</md-checkbox>
3346
</p>

src/demo-app/snack-bar/snack-bar-demo.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import {Component, ViewEncapsulation} from '@angular/core';
2-
import {MdSnackBar, MdSnackBarConfig} from '@angular/material';
2+
import {
3+
MdSnackBar,
4+
MdSnackBarConfig,
5+
MdSnackBarHorizontalPosition,
6+
MdSnackBarVerticalPosition,
7+
Dir,
8+
} from '@angular/material';
39

410
@Component({
511
moduleId: module.id,
@@ -15,13 +21,18 @@ export class SnackBarDemo {
1521
setAutoHide: boolean = true;
1622
autoHide: number = 10000;
1723
addExtraClass: boolean = false;
24+
horizontalPosition: MdSnackBarHorizontalPosition = 'center';
25+
verticalPosition: MdSnackBarVerticalPosition = 'bottom';
1826

19-
constructor(public snackBar: MdSnackBar) { }
27+
constructor(public snackBar: MdSnackBar, private dir: Dir) { }
2028

2129
open() {
2230
let config = new MdSnackBarConfig();
23-
config.duration = this.autoHide;
31+
config.verticalPosition = this.verticalPosition;
32+
config.horizontalPosition = this.horizontalPosition;
33+
config.duration = this.setAutoHide ? this.autoHide : 0;
2434
config.extraClasses = this.addExtraClass ? ['party'] : undefined;
35+
config.direction = this.dir.value;
2536
this.snackBar.open(this.message, this.action ? this.actionButtonLabel : undefined, config);
2637
}
2738
}

src/lib/snack-bar/snack-bar-config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
import {ViewContainerRef} from '@angular/core';
1010
import {AriaLivePoliteness, Direction} from '../core';
1111

12+
/** Possible values for horizontalPosition on MdSnackBarConfig. */
13+
export type MdSnackBarHorizontalPosition = 'start' | 'center' | 'end' | 'left' | 'right';
14+
15+
/** Possible values for verticalPosition on MdSnackBarConfig. */
16+
export type MdSnackBarVerticalPosition = 'top' | 'bottom';
17+
1218
/**
1319
* Configuration used when opening a snack-bar.
1420
*/
@@ -30,4 +36,10 @@ export class MdSnackBarConfig {
3036

3137
/** Text layout direction for the snack bar. */
3238
direction?: Direction = 'ltr';
39+
40+
/** The horizontal position to place the snack bar. */
41+
horizontalPosition?: MdSnackBarHorizontalPosition = 'center';
42+
43+
/** The vertical position to place the snack bar. */
44+
verticalPosition?: MdSnackBarVerticalPosition = 'bottom';
3345
}

src/lib/snack-bar/snack-bar-container.scss

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
$mat-snack-bar-padding: 14px 24px !default;
55
$mat-snack-bar-min-width: 288px !default;
66
$mat-snack-bar-max-width: 568px !default;
7+
$mat-snack-bar-spacing-margin: 24px !default;
78

89

910
:host {
@@ -12,12 +13,29 @@ $mat-snack-bar-max-width: 568px !default;
1213
border-radius: 2px;
1314
box-sizing: content-box;
1415
display: block;
16+
margin: $mat-snack-bar-spacing-margin;
1517
max-width: $mat-snack-bar-max-width;
1618
min-width: $mat-snack-bar-min-width;
1719
padding: $mat-snack-bar-padding;
18-
// Initial transformation is applied to start snack bar out of view.
20+
// Initial transformation is applied to start snack bar out of view, below its target position.
1921
transform: translateY(100%);
2022

23+
/**
24+
* Removes margin of snack bars which are center positioned horizontally. This
25+
* is done to align snack bars to the edge of the view vertically to match spec.
26+
*/
27+
&.mat-snack-bar-center {
28+
margin: 0;
29+
}
30+
31+
/**
32+
* To allow for animations from a 'top' vertical position to animate in a downward
33+
* direction, set the translation to start the snack bar above the target position.
34+
*/
35+
&.mat-snack-bar-top {
36+
transform: translateY(-100%);
37+
}
38+
2139
@include cdk-high-contrast {
2240
border: solid 1px;
2341
}

src/lib/snack-bar/snack-bar-container.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {Subject} from 'rxjs/Subject';
3535

3636

3737

38-
export type SnackBarState = 'initial' | 'visible' | 'complete' | 'void';
38+
export type SnackBarState = 'visible' | 'hidden' | 'void';
3939

4040
// TODO(jelbourn): we can't use constants from animation.ts here because you can't use
4141
// 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)';
5353
styleUrls: ['snack-bar-container.css'],
5454
host: {
5555
'role': 'alert',
56-
'[@state]': 'animationState',
56+
'[@state]': 'getAnimationState()',
5757
'(@state.done)': 'onAnimationEnd($event)'
5858
},
5959
animations: [
6060
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)),
6672
])
6773
],
6874
})
@@ -77,7 +83,7 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
7783
private onEnter: Subject<any> = new Subject();
7884

7985
/** The state of the snack bar animations. */
80-
animationState: SnackBarState = 'initial';
86+
private _animationState: SnackBarState;
8187

8288
/** The snack bar configuration. */
8389
snackBarConfig: MdSnackBarConfig;
@@ -89,6 +95,14 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
8995
super();
9096
}
9197

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+
92106
/** Attach a component portal as content to this snack bar container. */
93107
attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T> {
94108
if (this._portalHost.hasAttached()) {
@@ -103,6 +117,14 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
103117
}
104118
}
105119

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+
106128
return this._portalHost.attachComponentPortal(portal);
107129
}
108130

@@ -113,15 +135,14 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
113135

114136
/** Handle end of animations, updating the state of the snackbar. */
115137
onAnimationEnd(event: AnimationEvent) {
116-
if (event.toState === 'void' || event.toState === 'complete') {
138+
if (event.toState === 'void' || event.toState.startsWith('hidden')) {
117139
this._completeExit();
118140
}
119141

120-
if (event.toState === 'visible') {
142+
if (event.toState.startsWith('visible')) {
121143
// Note: we shouldn't use `this` inside the zone callback,
122144
// because it can cause a memory leak.
123145
const onEnter = this.onEnter;
124-
125146
this._ngZone.run(() => {
126147
onEnter.next();
127148
onEnter.complete();
@@ -131,18 +152,18 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
131152

132153
/** Begin animation of snack bar entrance into view. */
133154
enter(): void {
134-
this.animationState = 'visible';
155+
this._animationState = 'visible';
135156
}
136157

137158
/** Returns an observable resolving when the enter animation completes. */
138159
_onEnter(): Observable<void> {
139-
this.animationState = 'visible';
160+
this._animationState = 'visible';
140161
return this.onEnter.asObservable();
141162
}
142163

143164
/** Begin animation of the snack bar exiting from view. */
144165
exit(): Observable<void> {
145-
this.animationState = 'complete';
166+
this._animationState = 'hidden';
146167
return this._onExit();
147168
}
148169

0 commit comments

Comments
 (0)