Skip to content

Commit

Permalink
feat(snack-bar): allow addition of extra css classes
Browse files Browse the repository at this point in the history
Allows users to add extra CSS classes to the snack bar container via the `extraClasses` option.

Fixes angular#2664.
  • Loading branch information
crisbeto committed Jan 31, 2017
1 parent 08e9d70 commit fa87e25
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/demo-app/snack-bar/snack-bar-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ <h1>SnackBar demo</h1>
</md-input-container>
</md-checkbox>
</div>

<p>
<md-checkbox [(ngModel)]="addExtraClass">Add extra class to container</md-checkbox>
</p>
</div>

<button md-raised-button (click)="open()">OPEN</button>
20 changes: 17 additions & 3 deletions src/demo-app/snack-bar/snack-bar-demo.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
.demo-button-label-input {
display: inline-block;
}
.party {
animation: party 5s infinite;
}

@keyframes party {
0% { background: #00f; }
10% { background: #8e44ad; }
20% { background: #1abc9c; }
30% { background: #d35400; }
40% { background: #00f; }
50% { background: #34495e; }
60% { background: #00f; }
70% { background: #2980b9; }
80% { background: #f1c40f; }
90% { background: #2980b9; }
100% { background: #0ff; }
}
11 changes: 7 additions & 4 deletions src/demo-app/snack-bar/snack-bar-demo.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import {Component} from '@angular/core';
import {Component, ViewEncapsulation} from '@angular/core';
import {MdSnackBar, MdSnackBarConfig} from '@angular/material';

@Component({
moduleId: module.id,
selector: 'snack-bar-demo',
styleUrls: ['snack-bar-demo.css'],
templateUrl: 'snack-bar-demo.html',
encapsulation: ViewEncapsulation.None,
})
export class SnackBarDemo {
message: string = 'Snack Bar opened.';
actionButtonLabel: string = 'Retry';
action: boolean = false;
setAutoHide: boolean = true;
autoHide: number = 0;
autoHide: number = 10000;
addExtraClass: boolean = false;

constructor(
public snackBar: MdSnackBar) { }
constructor(public snackBar: MdSnackBar) { }

open() {
let config = new MdSnackBarConfig();
config.duration = this.autoHide;
config.extraClasses = this.addExtraClass ? ['party'] : null;
this.snackBar.open(this.message, this.action && this.actionButtonLabel, config);
}
}
2 changes: 1 addition & 1 deletion src/lib/snack-bar/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Please see the official documentation at https://material.angular.io/components/component/snack-bar
Please see the official documentation at https://material.angular.io/components/component/snack-bar
3 changes: 3 additions & 0 deletions src/lib/snack-bar/snack-bar-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ export class MdSnackBarConfig {

/** The length of time in milliseconds to wait before automatically dismissing the snack bar. */
duration?: number = 0;

/** Extra CSS classes to be added to the snack bar container. */
extraClasses?: string[];
}
15 changes: 14 additions & 1 deletion src/lib/snack-bar/snack-bar-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
AnimationTransitionEvent,
NgZone,
OnDestroy,
Renderer,
ElementRef,
} from '@angular/core';
import {
BasePortalHost,
Expand Down Expand Up @@ -71,7 +73,10 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
/** The snack bar configuration. */
snackBarConfig: MdSnackBarConfig;

constructor(private _ngZone: NgZone) {
constructor(
private _ngZone: NgZone,
private _renderer: Renderer,
private _elementRef: ElementRef) {
super();
}

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

if (this.snackBarConfig.extraClasses) {
// Not the most efficient way of adding classes, but the renderer doesn't allow us
// to pass in an array or a space-separated list.
for (let cssClass of this.snackBarConfig.extraClasses) {
this._renderer.setElementClass(this._elementRef.nativeElement, cssClass, true);
}
}

return this._portalHost.attachComponentPortal(portal);
}

Expand Down
12 changes: 12 additions & 0 deletions src/lib/snack-bar/snack-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,18 @@ describe('MdSnackBar', () => {
flushMicrotasks();
expect(dismissObservableCompleted).toBeTruthy('Expected the snack bar to be dismissed');
}));

it('should add extra classes to the container', () => {
snackBar.open(simpleMessage, simpleActionLabel, {
viewContainerRef: testViewContainerRef,
extraClasses: ['one', 'two']
});

let containerClasses = overlayContainerElement.querySelector('snack-bar-container').classList;

expect(containerClasses).toContain('one');
expect(containerClasses).toContain('two');
});
});

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

0 comments on commit fa87e25

Please sign in to comment.