Skip to content

Commit 0e24345

Browse files
crisbetotinayuangao
authored andcommitted
feat(snack-bar): allow setting the layout direction (#4726)
* feat(snack-bar): allow setting the layout direction * Allows the consumer to set the layout direction of a snack bar. * Fixes some bad indentation in the snack bar tests. Fixes #4721. * fix: turn off view encapsulation * chore: revert capitalization
1 parent ae46810 commit 0e24345

File tree

5 files changed

+54
-34
lines changed

5 files changed

+54
-34
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
$mat-snack-bar-button-margin: 48px !default;
66

7-
:host {
7+
.mat-simple-snackbar {
88
display: flex;
99
justify-content: space-between;
1010
color: white;
@@ -28,4 +28,9 @@ $mat-snack-bar-button-margin: 48px !default;
2828
size: inherit;
2929
weight: 600;
3030
}
31+
32+
[dir='rtl'] & {
33+
margin-right: $mat-snack-bar-button-margin;
34+
margin-left: 0;
35+
}
3136
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component} from '@angular/core';
1+
import {Component, ViewEncapsulation} from '@angular/core';
22
import {MdSnackBarRef} from './snack-bar-ref';
33

44

@@ -11,6 +11,7 @@ import {MdSnackBarRef} from './snack-bar-ref';
1111
selector: 'simple-snack-bar',
1212
templateUrl: 'simple-snack-bar.html',
1313
styleUrls: ['simple-snack-bar.css'],
14+
encapsulation: ViewEncapsulation.None,
1415
host: {
1516
'[class.mat-simple-snackbar]': 'true',
1617
}
@@ -31,5 +32,7 @@ export class SimpleSnackBar {
3132
}
3233

3334
/** If the action button should be shown. */
34-
get hasAction(): boolean { return !!this.action; }
35+
get hasAction(): boolean {
36+
return !!this.action;
37+
}
3538
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {ViewContainerRef} from '@angular/core';
2-
import {AriaLivePoliteness} from '../core';
2+
import {AriaLivePoliteness, LayoutDirection} from '../core';
33

44
/**
55
* Configuration used when opening a snack-bar.
@@ -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+
/** Text layout direction for the snack bar. */
24+
direction?: LayoutDirection = 'ltr';
2225
}

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

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -309,36 +309,45 @@ describe('MdSnackBar', () => {
309309
tick(500);
310310
}));
311311

312-
it('should dismiss automatically after a specified timeout', fakeAsync(() => {
313-
let dismissObservableCompleted = false;
314-
let config = new MdSnackBarConfig();
315-
config.duration = 250;
316-
let snackBarRef = snackBar.open('content', 'test', config);
317-
snackBarRef.afterDismissed().subscribe(() => {
318-
dismissObservableCompleted = true;
319-
});
312+
it('should dismiss automatically after a specified timeout', fakeAsync(() => {
313+
let dismissObservableCompleted = false;
314+
let config = new MdSnackBarConfig();
315+
config.duration = 250;
316+
let snackBarRef = snackBar.open('content', 'test', config);
317+
snackBarRef.afterDismissed().subscribe(() => {
318+
dismissObservableCompleted = true;
319+
});
320320

321-
viewContainerFixture.detectChanges();
322-
flushMicrotasks();
323-
expect(dismissObservableCompleted).toBeFalsy('Expected the snack bar not to be dismissed');
321+
viewContainerFixture.detectChanges();
322+
flushMicrotasks();
323+
expect(dismissObservableCompleted).toBeFalsy('Expected the snack bar not to be dismissed');
324324

325-
tick(1000);
326-
viewContainerFixture.detectChanges();
327-
flushMicrotasks();
328-
expect(dismissObservableCompleted).toBeTruthy('Expected the snack bar to be dismissed');
329-
}));
325+
tick(1000);
326+
viewContainerFixture.detectChanges();
327+
flushMicrotasks();
328+
expect(dismissObservableCompleted).toBeTruthy('Expected the snack bar to be dismissed');
329+
}));
330330

331-
it('should add extra classes to the container', () => {
332-
snackBar.open(simpleMessage, simpleActionLabel, {
333-
viewContainerRef: testViewContainerRef,
334-
extraClasses: ['one', 'two']
335-
});
331+
it('should add extra classes to the container', () => {
332+
snackBar.open(simpleMessage, simpleActionLabel, {
333+
viewContainerRef: testViewContainerRef,
334+
extraClasses: ['one', 'two']
335+
});
336336

337-
let containerClasses = overlayContainerElement.querySelector('snack-bar-container').classList;
337+
let containerClasses = overlayContainerElement.querySelector('snack-bar-container').classList;
338+
339+
expect(containerClasses).toContain('one');
340+
expect(containerClasses).toContain('two');
341+
});
342+
343+
it('should set the layout direction', () => {
344+
snackBar.open(simpleMessage, simpleActionLabel, { direction: 'rtl' });
345+
346+
let pane = overlayContainerElement.querySelector('.cdk-overlay-pane');
347+
348+
expect(pane.getAttribute('dir')).toBe('rtl', 'Expected the pane to be in RTL mode.');
349+
});
338350

339-
expect(containerClasses).toContain('one');
340-
expect(containerClasses).toContain('two');
341-
});
342351
});
343352

344353
describe('MdSnackBar with parent MdSnackBar', () => {

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

Lines changed: 5 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

@@ -139,12 +139,12 @@ export class MdSnackBar {
139139

140140
/**
141141
* Creates a new overlay and places it in the correct location.
142+
* @param config The user-specified snack bar config.
142143
*/
143-
private _createOverlay(): OverlayRef {
144+
private _createOverlay(config: MdSnackBarConfig): OverlayRef {
144145
let state = new OverlayState();
145-
state.positionStrategy = this._overlay.position().global()
146-
.centerHorizontally()
147-
.bottom('0');
146+
state.direction = config.direction;
147+
state.positionStrategy = this._overlay.position().global().centerHorizontally().bottom('0');
148148
return this._overlay.create(state);
149149
}
150150
}

0 commit comments

Comments
 (0)