Skip to content

Commit 6be0d6d

Browse files
committed
Allow snackbar position to be set to left or center.
1 parent 85bc3a6 commit 6be0d6d

File tree

6 files changed

+95
-19
lines changed

6 files changed

+95
-19
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ <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)]="position.horizontal">
9+
<md-option value="left">Left</md-option>
10+
<md-option value="center">Center</md-option>
11+
<md-option value="right">Right</md-option>
12+
</md-select>
13+
<md-select [(ngModel)]="position.vertical">
14+
<md-option value="top">Top</md-option>
15+
<md-option value="bottom">Bottom</md-option>
16+
</md-select>
17+
</div>
618
<div>
719
<md-checkbox [(ngModel)]="action">
820
<p *ngIf="!action">Show button on snack bar</p>
@@ -27,7 +39,6 @@ <h1>SnackBar demo</h1>
2739
</md-input-container>
2840
</md-checkbox>
2941
</div>
30-
3142
<p>
3243
<md-checkbox [(ngModel)]="addExtraClass">Add extra class to container</md-checkbox>
3344
</p>

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Component, ViewEncapsulation} from '@angular/core';
2-
import {MdSnackBar, MdSnackBarConfig} from '@angular/material';
2+
import {MdSnackBar, MdSnackBarConfig, MdSnackBarPosition} from '@angular/material';
33

44
@Component({
55
moduleId: module.id,
@@ -15,12 +15,20 @@ export class SnackBarDemo {
1515
setAutoHide: boolean = true;
1616
autoHide: number = 10000;
1717
addExtraClass: boolean = false;
18+
position: MdSnackBarPosition = {
19+
horizontal: 'center',
20+
vertical: 'bottom'
21+
};
1822

1923
constructor(public snackBar: MdSnackBar) { }
2024

2125
open() {
2226
let config = new MdSnackBarConfig();
23-
config.duration = this.autoHide;
27+
config.position = {
28+
vertical: this.position.vertical,
29+
horizontal: this.position.horizontal
30+
};
31+
config.duration = this.setAutoHide ? this.autoHide : 0;
2432
config.extraClasses = this.addExtraClass ? ['party'] : null;
2533
this.snackBar.open(this.message, this.action && this.actionButtonLabel, config);
2634
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import {ViewContainerRef} from '@angular/core';
22
import {AriaLivePoliteness} from '../core';
33

4+
export interface MdSnackBarPosition {
5+
horizontal: 'left'|'center'|'right';
6+
vertical: 'bottom'|'top';
7+
}
8+
49
/**
510
* Configuration used when opening a snack-bar.
611
*/
@@ -19,4 +24,10 @@ export class MdSnackBarConfig {
1924

2025
/** Extra CSS classes to be added to the snack bar container. */
2126
extraClasses?: string[];
27+
28+
/** The position to place the snack bar in the view, either 'left' or 'center'. */
29+
position?: MdSnackBarPosition = {
30+
horizontal: 'center',
31+
vertical: 'bottom'
32+
};
2233
}

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

Lines changed: 10 additions & 0 deletions
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,21 @@ $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;
1820
// Initial transformation is applied to start snack bar out of view.
1921
transform: translateY(100%);
2022

23+
&.md-snack-bar-center {
24+
margin: 0;
25+
}
26+
27+
&.md-snack-bar-top {
28+
transform: translateY(-100%);
29+
}
30+
2131
@include cdk-high-contrast {
2232
border: solid 1px;
2333
}

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

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {Subject} from 'rxjs/Subject';
2828

2929

3030

31-
export type SnackBarState = 'initial' | 'visible' | 'complete' | 'void';
31+
export type SnackBarState = 'visible' | 'hidden' | 'void';
3232

3333
// TODO(jelbourn): we can't use constants from animation.ts here because you can't use
3434
// a text interpolation in anything that is analyzed statically with ngc (for AoT compile).
@@ -46,16 +46,22 @@ export const HIDE_ANIMATION = '195ms cubic-bezier(0.0,0.0,0.2,1)';
4646
styleUrls: ['snack-bar-container.css'],
4747
host: {
4848
'role': 'alert',
49-
'[@state]': 'animationState',
49+
'[@state]': 'getAnimationState()',
5050
'(@state.done)': 'onAnimationEnd($event)'
5151
},
5252
animations: [
5353
trigger('state', [
54-
state('initial', style({transform: 'translateY(100%)'})),
55-
state('visible', style({transform: 'translateY(0%)'})),
56-
state('complete', style({transform: 'translateY(100%)'})),
57-
transition('visible => complete', animate(HIDE_ANIMATION)),
58-
transition('initial => visible, void => visible', animate(SHOW_ANIMATION)),
54+
// Animation from top.
55+
state('visible-top', style({transform: 'translateY(0%)'})),
56+
state('hidden-top', style({transform: 'translateY(-100%)'})),
57+
transition('visible-top => hidden-top', animate(HIDE_ANIMATION)),
58+
transition('void => visible-top', animate(SHOW_ANIMATION)),
59+
// Animation from bottom.
60+
state('visible-bottom', style({transform: 'translateY(0%)'})),
61+
state('hidden-bottom', style({transform: 'translateY(100%)'})),
62+
transition('visible-bottom => hidden-bottom', animate(HIDE_ANIMATION)),
63+
transition('void => visible-bottom',
64+
animate(SHOW_ANIMATION)),
5965
])
6066
],
6167
})
@@ -70,7 +76,7 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
7076
private onEnter: Subject<any> = new Subject();
7177

7278
/** The state of the snack bar animations. */
73-
animationState: SnackBarState = 'initial';
79+
animationState: SnackBarState;
7480

7581
/** The snack bar configuration. */
7682
snackBarConfig: MdSnackBarConfig;
@@ -82,6 +88,10 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
8288
super();
8389
}
8490

91+
getAnimationState(): string {
92+
return `${this.animationState}-${this.snackBarConfig.position.vertical}`;
93+
}
94+
8595
/** Attach a component portal as content to this snack bar container. */
8696
attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T> {
8797
if (this._portalHost.hasAttached()) {
@@ -96,6 +106,14 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
96106
}
97107
}
98108

109+
if (this.snackBarConfig.position.horizontal === 'center') {
110+
this._renderer.setElementClass(this._elementRef.nativeElement, 'md-snack-bar-center', true);
111+
}
112+
113+
if (this.snackBarConfig.position.vertical === 'top') {
114+
this._renderer.setElementClass(this._elementRef.nativeElement, 'md-snack-bar-top', true);
115+
}
116+
99117
return this._portalHost.attachComponentPortal(portal);
100118
}
101119

@@ -106,11 +124,11 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
106124

107125
/** Handle end of animations, updating the state of the snackbar. */
108126
onAnimationEnd(event: AnimationEvent) {
109-
if (event.toState === 'void' || event.toState === 'complete') {
127+
if (event.toState === 'void' || event.toState.startsWith('hidden')) {
110128
this._completeExit();
111129
}
112130

113-
if (event.toState === 'visible') {
131+
if (event.toState.startsWith('visible')) {
114132
this._ngZone.run(() => {
115133
this.onEnter.next();
116134
this.onEnter.complete();
@@ -131,7 +149,7 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
131149

132150
/** Begin animation of the snack bar exiting from view. */
133151
exit(): Observable<void> {
134-
this.animationState = 'complete';
152+
this.animationState = 'hidden';
135153
return this._onExit();
136154
}
137155

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class MdSnackBar {
5454
*/
5555
openFromComponent<T>(component: ComponentType<T>, config?: MdSnackBarConfig): MdSnackBarRef<T> {
5656
config = _applyConfigDefaults(config);
57-
let overlayRef = this._createOverlay();
57+
let overlayRef = this._createOverlay(config);
5858
let snackBarContainer = this._attachSnackBarContainer(overlayRef, config);
5959
let snackBarRef = this._attachSnackbarContent(component, snackBarContainer, overlayRef);
6060

@@ -140,11 +140,29 @@ export class MdSnackBar {
140140
/**
141141
* Creates a new overlay and places it in the correct location.
142142
*/
143-
private _createOverlay(): OverlayRef {
143+
private _createOverlay(config: MdSnackBarConfig): OverlayRef {
144144
let state = new OverlayState();
145-
state.positionStrategy = this._overlay.position().global()
146-
.centerHorizontally()
147-
.bottom('0');
145+
let positionStrategy = this._overlay.position().global();
146+
switch (config.position.horizontal) {
147+
case 'left':
148+
positionStrategy.left('0');
149+
break;
150+
case 'right':
151+
positionStrategy.right('0');
152+
break;
153+
case 'center':
154+
positionStrategy.centerHorizontally();
155+
break;
156+
}
157+
switch (config.position.vertical) {
158+
case 'top':
159+
positionStrategy.top('0');
160+
break;
161+
case 'bottom':
162+
positionStrategy.bottom('0');
163+
break;
164+
}
165+
state.positionStrategy = positionStrategy;
148166
return this._overlay.create(state);
149167
}
150168
}

0 commit comments

Comments
 (0)