Skip to content

Commit 0023ed8

Browse files
committed
Set snack bar to be responsive.
1 parent 6ab275c commit 0023ed8

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

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

Lines changed: 8 additions & 2 deletions
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;
@@ -17,7 +17,7 @@ $mat-snack-bar-spacing-margin: 24px !default;
1717
// Initial transformation is applied to start snack bar out of view, below its target position.
1818
transform: translateY(100%);
1919

20-
/**
20+
/**
2121
* Removes margin of snack bars which are center positioned horizontally. This
2222
* is done to align snack bars to the edge of the view vertically to match spec.
2323
*/
@@ -33,6 +33,12 @@ $mat-snack-bar-spacing-margin: 24px !default;
3333
transform: translateY(-100%);
3434
}
3535

36+
&.mobile {
37+
margin: 0px;
38+
max-width: initial;
39+
width: 100vw;
40+
}
41+
3642
@include cdk-high-contrast {
3743
border: solid 1px;
3844
}

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

Lines changed: 10 additions & 1 deletion
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, BreakpointState, Breakpoints} from '@angular/cdk/layout';
3637
import {Observable} from 'rxjs/Observable';
3738
import {Subject} from 'rxjs/Subject';
3839
import {MatSnackBarConfig} from './snack-bar-config';
@@ -58,6 +59,7 @@ export const HIDE_ANIMATION = '195ms cubic-bezier(0.0,0.0,0.2,1)';
5859
host: {
5960
'role': 'alert',
6061
'class': 'mat-snack-bar-container',
62+
'[class.mobile]': '_isHandset',
6163
'[@state]': '_animationState',
6264
'(@state.done)': 'onAnimationEnd($event)'
6365
},
@@ -90,6 +92,9 @@ export class MatSnackBarContainer extends BasePortalHost implements OnDestroy {
9092
/** Subject for notifying that the snack bar has finished entering the view. */
9193
_onEnter: Subject<any> = new Subject();
9294

95+
/** Whether the snackbar is currently displaying on a mobile screen.. */
96+
_isHandset: boolean;
97+
9398
/** The state of the snack bar animations. */
9499
_animationState = 'void';
95100

@@ -100,8 +105,12 @@ export class MatSnackBarContainer extends BasePortalHost implements OnDestroy {
100105
private _ngZone: NgZone,
101106
private _renderer: Renderer2,
102107
private _elementRef: ElementRef,
103-
private _changeDetectorRef: ChangeDetectorRef) {
108+
private _changeDetectorRef: ChangeDetectorRef,
109+
breakpointObserver: BreakpointObserver) {
104110
super();
111+
// Listen for the screen matcheing the `Handset` breakpoint to adjust snackbar styling.
112+
breakpointObserver.observe(Breakpoints.Handset).subscribe(
113+
breakpointState => this._isHandset = breakpointState.matches);
105114
}
106115

107116
/** Attach a component portal as content to this snack bar container. */

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],

0 commit comments

Comments
 (0)