Skip to content

Commit 758b851

Browse files
jelbournkara
authored andcommitted
feat(dialog): add styles and ability to close. (#862)
1 parent b2471f2 commit 758b851

File tree

8 files changed

+93
-16
lines changed

8 files changed

+93
-16
lines changed
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
@import 'elevation';
2+
@import 'default-theme';
3+
4+
$md-dialog-padding: 24px !default;
25

36
:host {
4-
// TODO(jelbourn): add real Material Design dialog styles.
57
display: block;
6-
background: deeppink;
7-
@include md-elevation(2);
8+
overflow: hidden;
9+
10+
padding: $md-dialog-padding;
11+
12+
background: md-color($md-background, dialog);
13+
@include md-elevation(24);
814
}

src/components/dialog/dialog-ref.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
1+
import {OverlayRef} from '@angular2-material/core/overlay/overlay-ref';
2+
import {Observable} from 'rxjs/Observable';
3+
import {Subject} from 'rxjs/Subject';
4+
5+
6+
// TODO(jelbourn): resizing
7+
// TODO(jelbourn): afterOpen and beforeClose
8+
9+
110
/**
211
* Reference to a dialog opened via the MdDialog service.
312
*/
413
export class MdDialogRef<T> {
514
/** The instance of component opened into the dialog. */
615
componentInstance: T;
716

8-
// TODO(jelbourn): Add methods to resize, close, and get results from the dialog.
17+
/** Subject for notifying the user that the dialog has finished closing. */
18+
private _afterClosed: Subject<any> = new Subject();
19+
20+
constructor(private _overlayRef: OverlayRef) { }
21+
22+
/**
23+
* Close the dialog.
24+
* @param dialogResult Optional result to return to the dialog opener.
25+
*/
26+
close(dialogResult?: any): void {
27+
this._overlayRef.dispose();
28+
this._afterClosed.next(dialogResult);
29+
this._afterClosed.complete();
30+
}
31+
32+
/** Gets an observable that is notified when the dialog is finished closing. */
33+
afterClosed(): Observable<any> {
34+
return this._afterClosed.asObservable();
35+
}
936
}

src/components/dialog/dialog.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,29 @@ describe('MdDialog', () => {
8484

8585
detectChangesForDialogOpen(viewContainerFixture);
8686
}));
87+
88+
it('should close a dialog and get back a result', async(() => {
89+
let config = new MdDialogConfig();
90+
config.viewContainerRef = testViewContainerRef;
91+
92+
dialog.open(PizzaMsg, config).then(dialogRef => {
93+
viewContainerFixture.detectChanges();
94+
95+
let afterCloseResult: string;
96+
dialogRef.afterClosed().subscribe(result => {
97+
afterCloseResult = result;
98+
});
99+
100+
dialogRef.close('Charmander');
101+
102+
viewContainerFixture.whenStable().then(() => {
103+
expect(afterCloseResult).toBe('Charmander');
104+
expect(overlayContainerElement.childNodes.length).toBe(0);
105+
});
106+
});
107+
108+
detectChangesForDialogOpen(viewContainerFixture);
109+
}));
87110
});
88111

89112

src/components/dialog/dialog.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ export class MdDialog {
3737
* @param config
3838
*/
3939
open<T>(component: ComponentType<T>, config: MdDialogConfig): Promise<MdDialogRef<T>> {
40+
let overlayRef: OverlayRef;
41+
4042
return this._createOverlay(config)
41-
.then(overlayRef => this._attachDialogContainer(overlayRef, config))
42-
.then(containerRef => this._attachDialogContent(component, containerRef));
43+
.then(overlay => overlayRef = overlay)
44+
.then(overlay => this._attachDialogContainer(overlay, config))
45+
.then(containerRef => this._attachDialogContent(component, containerRef, overlayRef));
4346
}
4447

4548
/**
@@ -54,14 +57,14 @@ export class MdDialog {
5457

5558
/**
5659
* Attaches an MdDialogContainer to a dialog's already-created overlay.
57-
* @param overlayRef Reference to the dialog's underlying overlay.
60+
* @param overlay Reference to the dialog's underlying overlay.
5861
* @param config The dialog configuration.
5962
* @returns A promise resolving to a ComponentRef for the attached container.
6063
*/
61-
private _attachDialogContainer(overlayRef: OverlayRef, config: MdDialogConfig):
64+
private _attachDialogContainer(overlay: OverlayRef, config: MdDialogConfig):
6265
Promise<ComponentRef<MdDialogContainer>> {
6366
let containerPortal = new ComponentPortal(MdDialogContainer, config.viewContainerRef);
64-
return overlayRef.attach(containerPortal).then(containerRef => {
67+
return overlay.attach(containerPortal).then((containerRef: ComponentRef<MdDialogContainer>) => {
6568
// Pass the config directly to the container so that it can consume any relevant settings.
6669
containerRef.instance.dialogConfig = config;
6770
return containerRef;
@@ -72,16 +75,18 @@ export class MdDialog {
7275
* Attaches the user-provided component to the already-created MdDialogContainer.
7376
* @param component The type of component being loaded into the dialog.
7477
* @param containerRef Reference to the wrapping MdDialogContainer.
78+
* @param overlayRef Reference to the overlay in which the dialog resides.
7579
* @returns A promise resolving to the MdDialogRef that should be returned to the user.
7680
*/
7781
private _attachDialogContent<T>(
7882
component: ComponentType<T>,
79-
containerRef: ComponentRef<MdDialogContainer>): Promise<MdDialogRef<T>> {
83+
containerRef: ComponentRef<MdDialogContainer>,
84+
overlayRef: OverlayRef): Promise<MdDialogRef<T>> {
8085
let dialogContainer = containerRef.instance;
8186

8287
// Create a reference to the dialog we're creating in order to give the user a handle
8388
// to modify and close it.
84-
let dialogRef = new MdDialogRef();
89+
let dialogRef = new MdDialogRef(overlayRef);
8590

8691
// We create an injector specifically for the component we're instantiating so that it can
8792
// inject the MdDialogRef. This allows a component loaded inside of a dialog to close itself

src/demo-app/demo-app/demo-app.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.demo-root {
1+
body {
22
font-family: Roboto, 'Helvetica Neue', sans-serif;
33

44
// Helps fonts on OSX looks more consistent with other systems

src/demo-app/demo-app/demo-app.ts

Lines changed: 3 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 {ROUTER_DIRECTIVES} from '@angular/router';
33

44
import {Dir} from '@angular2-material/core/rtl/dir';
@@ -32,6 +32,7 @@ export class Home {}
3232
MD_LIST_DIRECTIVES,
3333
MdToolbar,
3434
],
35-
pipes: []
35+
pipes: [],
36+
encapsulation: ViewEncapsulation.None,
3637
})
3738
export class DemoApp { }

src/demo-app/dialog/dialog-demo.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
<h1>Dialog demo</h1>
22

33
<button (click)="open()" [disabled]="dialogRef">Open dialog</button>
4+
5+
<p>
6+
Last close result: {{lastCloseResult}}
7+
</p>

src/demo-app/dialog/dialog-demo.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {OVERLAY_PROVIDERS} from '@angular2-material/core/overlay/overlay';
1111
})
1212
export class DialogDemo {
1313
dialogRef: MdDialogRef<JazzDialog>;
14+
lastCloseResult: string;
1415

1516
constructor(
1617
public dialog: MdDialog,
@@ -22,13 +23,23 @@ export class DialogDemo {
2223

2324
this.dialog.open(JazzDialog, config).then(ref => {
2425
this.dialogRef = ref;
26+
27+
this.dialogRef.afterClosed().subscribe(result => {
28+
this.lastCloseResult = result;
29+
this.dialogRef = null;
30+
});
2531
});
2632
}
2733
}
2834

2935

3036
@Component({
3137
selector: 'demo-jazz-dialog',
32-
template: `<p>It's Jazz!</p>`
38+
template: `
39+
<p>It's Jazz!</p>
40+
<p><label>How much? <input #howMuch></label></p>
41+
<button type="button" (click)="dialogRef.close(howMuch.value)">Close dialog</button>`
3342
})
34-
export class JazzDialog { }
43+
export class JazzDialog {
44+
constructor(public dialogRef: MdDialogRef<JazzDialog>) { }
45+
}

0 commit comments

Comments
 (0)