Skip to content

Commit 9e75237

Browse files
committed
Set snack bar to be responsive.
1 parent cef1eba commit 9e75237

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class SnackBarDemo {
3333
let config = new MatSnackBarConfig();
3434
config.verticalPosition = this.verticalPosition;
3535
config.horizontalPosition = this.horizontalPosition;
36-
config.duration = this.autoHide;
36+
config.duration = this.setAutoHide ? this.autoHide: 0;
3737
config.extraClasses = this.addExtraClass ? ['party'] : undefined;
3838
config.direction = this.dir.value;
3939
this.snackBar.open(this.message, this.action ? this.actionButtonLabel : undefined, config);

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ $mat-snack-bar-spacing-margin: 24px !default;
88

99
.mat-snack-bar-container {
1010
border-radius: 2px;
11-
box-sizing: content-box;
11+
box-sizing: border-box;
1212
display: block;
1313
margin: $mat-snack-bar-spacing-margin;
1414
max-width: $mat-snack-bar-max-width;
@@ -44,3 +44,17 @@ $mat-snack-bar-spacing-margin: 24px !default;
4444
border: solid 1px;
4545
}
4646
}
47+
48+
/**
49+
* The mat-snack-bar-handset class will be applied to the overlay
50+
* element causing styling to both it and the snack bar container.
51+
*/
52+
.mat-snack-bar-handset {
53+
width: 100%;
54+
55+
.mat-snack-bar-container {
56+
margin: 0;
57+
max-width: inherit;
58+
width: 100%;
59+
}
60+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
PortalHostDirective,
3434
} from '@angular/cdk/portal';
3535
import {first} from '@angular/cdk/rxjs';
36+
import {BreakpointObserver, Breakpoints} from '@angular/cdk/layout';
3637
import {Observable} from 'rxjs/Observable';
3738
import {Subject} from 'rxjs/Subject';
3839
import {MatSnackBarConfig} from './snack-bar-config';
@@ -83,6 +84,9 @@ export class MatSnackBarContainer extends BasePortalHost implements OnDestroy {
8384
/** Subject for notifying that the snack bar has finished entering the view. */
8485
_onEnter: Subject<any> = new Subject();
8586

87+
/** Whether the snackbar is currently displaying on a mobile screen.. */
88+
_isHandset: boolean;
89+
8690
/** The state of the snack bar animations. */
8791
_animationState = 'void';
8892

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {CommonModule} from '@angular/common';
1111
import {OverlayModule} from '@angular/cdk/overlay';
1212
import {PortalModule} from '@angular/cdk/portal';
1313
import {LIVE_ANNOUNCER_PROVIDER} from '@angular/cdk/a11y';
14+
import {LayoutModule} from '@angular/cdk/layout';
1415
import {MatCommonModule} from '@angular/material/core';
1516
import {MatSnackBar} from './snack-bar';
1617
import {MatSnackBarContainer} from './snack-bar-container';
@@ -23,6 +24,7 @@ import {SimpleSnackBar} from './simple-snack-bar';
2324
PortalModule,
2425
CommonModule,
2526
MatCommonModule,
27+
LayoutModule,
2628
],
2729
exports: [MatSnackBarContainer, MatCommonModule],
2830
declarations: [MatSnackBarContainer, SimpleSnackBar],

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {SimpleSnackBar} from './simple-snack-bar';
1515
import {MAT_SNACK_BAR_DATA, MatSnackBarConfig} from './snack-bar-config';
1616
import {MatSnackBarContainer} from './snack-bar-container';
1717
import {MatSnackBarRef} from './snack-bar-ref';
18+
import {Observable} from 'rxjs/Observable';
19+
import {BreakpointObserver, Breakpoints, BreakpointState} from '@angular/cdk/layout';
1820

1921

2022
/**
@@ -47,6 +49,7 @@ export class MatSnackBar {
4749
private _overlay: Overlay,
4850
private _live: LiveAnnouncer,
4951
private _injector: Injector,
52+
private _breakpointObserver: BreakpointObserver,
5053
@Optional() @SkipSelf() private _parentSnackBar: MatSnackBar) {}
5154

5255
/**
@@ -145,6 +148,18 @@ export class MatSnackBar {
145148
// We can't pass this via the injector, because the injector is created earlier.
146149
snackBarRef.instance = contentRef.instance;
147150

151+
// Subscribe to the breakpoint observer and attacg the mat-snack-bar-handset class as
152+
// appropriate. This class is applied to the overlay element because the overlay must expand to
153+
// fill the width of the screen for full width snackbars.
154+
const isHandset = this._breakpointObserver.observe(Breakpoints.Handset).subscribe(state => {
155+
overlayRef.overlayElement.classList.toggle('mat-snack-bar-handset', state.matches);
156+
});
157+
// Unsubscribe from the isHandset subscription when the overlay is detached from the view.
158+
const overlayDetach = overlayRef.detachments().subscribe(() => {
159+
overlayDetach.unsubscribe();
160+
isHandset.unsubscribe();
161+
});
162+
148163
return snackBarRef;
149164
}
150165

0 commit comments

Comments
 (0)