Skip to content

Commit f4d5bd1

Browse files
committed
Allow snackbar position to be set to left or center.
1 parent 1326234 commit f4d5bd1

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ <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+
<span>Position in page: </span>
8+
<md-radio-group [(ngModel)]="position">
9+
<md-radio-button value="left">Left</md-radio-button>
10+
<md-radio-button value="center">Center</md-radio-button>
11+
</md-radio-group>
12+
</div>
613
<div>
714
<md-checkbox [(ngModel)]="action">
815
<p *ngIf="!action">Show button on snack bar</p>
@@ -27,7 +34,6 @@ <h1>SnackBar demo</h1>
2734
</md-input-container>
2835
</md-checkbox>
2936
</div>
30-
3137
<p>
3238
<md-checkbox [(ngModel)]="addExtraClass">Add extra class to container</md-checkbox>
3339
</p>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ export class SnackBarDemo {
1515
setAutoHide: boolean = true;
1616
autoHide: number = 10000;
1717
addExtraClass: boolean = false;
18+
position: 'center' | 'left' = 'center';
1819

1920
constructor(public snackBar: MdSnackBar) { }
2021

2122
open() {
2223
let config = new MdSnackBarConfig();
23-
config.duration = this.autoHide;
24+
config.position = this.position;
25+
config.duration = this.setAutoHide ? this.autoHide : 0;
2426
config.extraClasses = this.addExtraClass ? ['party'] : null;
2527
this.snackBar.open(this.message, this.action && this.actionButtonLabel, config);
2628
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ export class MdSnackBarConfig {
1919

2020
/** Extra CSS classes to be added to the snack bar container. */
2121
extraClasses?: string[];
22+
23+
/** The position to place the snack bar in the view, either 'left' or 'center'. */
24+
position?: string = 'center';
2225
}

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

Lines changed: 9 additions & 3 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,17 @@ 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()
145+
if (config.position === 'left') {
146+
state.positionStrategy = this._overlay.position().global()
147+
.left('24px')
148+
.bottom('24px');
149+
} else {
150+
state.positionStrategy = this._overlay.position().global()
146151
.centerHorizontally()
147152
.bottom('0');
153+
}
148154
return this._overlay.create(state);
149155
}
150156
}

0 commit comments

Comments
 (0)