Skip to content

Commit e783494

Browse files
crisbetokara
authored andcommitted
feat(snack-bar): allow addition of extra css classes (#2804)
Fixes #2664.
1 parent 68a0c90 commit e783494

File tree

7 files changed

+58
-9
lines changed

7 files changed

+58
-9
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ <h1>SnackBar demo</h1>
2727
</md-input-container>
2828
</md-checkbox>
2929
</div>
30+
31+
<p>
32+
<md-checkbox [(ngModel)]="addExtraClass">Add extra class to container</md-checkbox>
33+
</p>
3034
</div>
3135

3236
<button md-raised-button (click)="open()">OPEN</button>
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
.demo-button-label-input {
2-
display: inline-block;
3-
}
1+
.party {
2+
animation: party 5000ms infinite;
3+
}
4+
5+
@keyframes party {
6+
0% { background: #00f; }
7+
10% { background: #8e44ad; }
8+
20% { background: #1abc9c; }
9+
30% { background: #d35400; }
10+
40% { background: #00f; }
11+
50% { background: #34495e; }
12+
60% { background: #00f; }
13+
70% { background: #2980b9; }
14+
80% { background: #f1c40f; }
15+
90% { background: #2980b9; }
16+
100% { background: #0ff; }
17+
}
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
import {Component} from '@angular/core';
1+
import {Component, ViewEncapsulation} from '@angular/core';
22
import {MdSnackBar, MdSnackBarConfig} from '@angular/material';
33

44
@Component({
55
moduleId: module.id,
66
selector: 'snack-bar-demo',
7+
styleUrls: ['snack-bar-demo.css'],
78
templateUrl: 'snack-bar-demo.html',
9+
encapsulation: ViewEncapsulation.None,
810
})
911
export class SnackBarDemo {
1012
message: string = 'Snack Bar opened.';
1113
actionButtonLabel: string = 'Retry';
1214
action: boolean = false;
1315
setAutoHide: boolean = true;
14-
autoHide: number = 0;
16+
autoHide: number = 10000;
17+
addExtraClass: boolean = false;
1518

16-
constructor(
17-
public snackBar: MdSnackBar) { }
19+
constructor(public snackBar: MdSnackBar) { }
1820

1921
open() {
2022
let config = new MdSnackBarConfig();
2123
config.duration = this.autoHide;
24+
config.extraClasses = this.addExtraClass ? ['party'] : null;
2225
this.snackBar.open(this.message, this.action && this.actionButtonLabel, config);
2326
}
2427
}

src/lib/snack-bar/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Please see the official documentation at https://material.angular.io/components/component/snack-bar
1+
Please see the official documentation at https://material.angular.io/components/component/snack-bar

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

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

1717
/** The length of time in milliseconds to wait before automatically dismissing the snack bar. */
1818
duration?: number = 0;
19+
20+
/** Extra CSS classes to be added to the snack bar container. */
21+
extraClasses?: string[];
1922
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
AnimationTransitionEvent,
1111
NgZone,
1212
OnDestroy,
13+
Renderer,
14+
ElementRef,
1315
} from '@angular/core';
1416
import {
1517
BasePortalHost,
@@ -71,7 +73,10 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
7173
/** The snack bar configuration. */
7274
snackBarConfig: MdSnackBarConfig;
7375

74-
constructor(private _ngZone: NgZone) {
76+
constructor(
77+
private _ngZone: NgZone,
78+
private _renderer: Renderer,
79+
private _elementRef: ElementRef) {
7580
super();
7681
}
7782

@@ -81,6 +86,14 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
8186
throw new MdSnackBarContentAlreadyAttached();
8287
}
8388

89+
if (this.snackBarConfig.extraClasses) {
90+
// Not the most efficient way of adding classes, but the renderer doesn't allow us
91+
// to pass in an array or a space-separated list.
92+
for (let cssClass of this.snackBarConfig.extraClasses) {
93+
this._renderer.setElementClass(this._elementRef.nativeElement, cssClass, true);
94+
}
95+
}
96+
8497
return this._portalHost.attachComponentPortal(portal);
8598
}
8699

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,18 @@ describe('MdSnackBar', () => {
320320
flushMicrotasks();
321321
expect(dismissObservableCompleted).toBeTruthy('Expected the snack bar to be dismissed');
322322
}));
323+
324+
it('should add extra classes to the container', () => {
325+
snackBar.open(simpleMessage, simpleActionLabel, {
326+
viewContainerRef: testViewContainerRef,
327+
extraClasses: ['one', 'two']
328+
});
329+
330+
let containerClasses = overlayContainerElement.querySelector('snack-bar-container').classList;
331+
332+
expect(containerClasses).toContain('one');
333+
expect(containerClasses).toContain('two');
334+
});
323335
});
324336

325337
describe('MdSbackBar with parent MdSnackBar', () => {

0 commit comments

Comments
 (0)