Skip to content

Commit 9b103fe

Browse files
committed
feat(dialog): add hasBackdrop and backdropClass options to dialog config
Implement hasBackdrop as laid out in the MdDialog design document. Add backdropClass option to override default `cdk-overlay-dark-backdrop` class, allowing custom styling to be applied. Closes #2806
1 parent 7b30fdc commit 9b103fe

File tree

6 files changed

+72
-1
lines changed

6 files changed

+72
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ <h2>Dialog position</h2>
3636
</md-input-container>
3737
</p>
3838

39+
<h2>Dialog backdrop</h2>
40+
41+
<p>
42+
<md-input-container>
43+
<input mdInput [(ngModel)]="config.backdropClass" placeholder="Backdrop class">
44+
</md-input-container>
45+
</p>
46+
47+
<md-checkbox [(ngModel)]="config.hasBackdrop">Has backdrop</md-checkbox>
48+
3949
<h2>Other options</h2>
4050

4151
<p>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export class DialogDemo {
1313
actionsAlignment: string;
1414
config: MdDialogConfig = {
1515
disableClose: false,
16+
hasBackdrop: true,
17+
backdropClass: '',
1618
width: '',
1719
height: '',
1820
position: {

src/lib/dialog/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ MdDialog is a service, which opens dialogs components in the view.
1414
| Key | Description |
1515
| --- | ------------ |
1616
| `role: DialogRole = 'dialog'` | The ARIA role of the dialog element. Possible values are `dialog` and `alertdialog`. Optional. |
17+
| `hasBackdrop: boolean = true` | Whether the dialog has a backdrop. Optional. |
18+
| `backdropClass: string = ''` | A custom CSS class to apply to the backdrop to replace the default dark backdrop class. Optional. |
1719
| `disableClose: boolean = false` | Whether to prevent the user from closing a dialog by clicking on the backdrop or pressing escape. Optional. |
1820
| `width: string = ''` | Width of the dialog. Takes any valid CSS value. Optional. |
1921
| `height: string = ''` | Height of the dialog. Takes any valid CSS value. Optional. |

src/lib/dialog/dialog-config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export class MdDialogConfig {
2121
/** The ARIA role of the dialog element. */
2222
role?: DialogRole = 'dialog';
2323

24+
/** Whether the dialog has a backdrop. */
25+
hasBackdrop?: boolean = true;
26+
27+
/** Custom class for the backdrop, */
28+
backdropClass?: string = '';
29+
2430
/** Whether the user can use escape or clicking outside to close a modal. */
2531
disableClose?: boolean = false;
2632

src/lib/dialog/dialog.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,54 @@ describe('MdDialog', () => {
275275
});
276276
});
277277

278+
describe('hasBackdrop option', () => {
279+
it('should have a backdrop', () => {
280+
dialog.open(PizzaMsg, {
281+
hasBackdrop: true,
282+
viewContainerRef: testViewContainerRef
283+
});
284+
285+
viewContainerFixture.detectChanges();
286+
287+
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeTruthy();
288+
});
289+
290+
it('should not have a backdrop', () => {
291+
dialog.open(PizzaMsg, {
292+
hasBackdrop: false,
293+
viewContainerRef: testViewContainerRef
294+
});
295+
296+
viewContainerFixture.detectChanges();
297+
298+
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeFalsy();
299+
});
300+
});
301+
302+
describe('backdropClass option', () => {
303+
it('should have default backdrop class', () => {
304+
dialog.open(PizzaMsg, {
305+
backdropClass: '',
306+
viewContainerRef: testViewContainerRef
307+
});
308+
309+
viewContainerFixture.detectChanges();
310+
311+
expect(overlayContainerElement.querySelector('.cdk-overlay-dark-backdrop')).toBeTruthy();
312+
});
313+
314+
it('should have custom backdrop class', () => {
315+
dialog.open(PizzaMsg, {
316+
backdropClass: 'custom-backdrop-class',
317+
viewContainerRef: testViewContainerRef
318+
});
319+
320+
viewContainerFixture.detectChanges();
321+
322+
expect(overlayContainerElement.querySelector('.custom-backdrop-class')).toBeTruthy();
323+
});
324+
});
325+
278326
describe('focus management', () => {
279327

280328
// When testing focus, all of the elements must be in the DOM.

src/lib/dialog/dialog.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ export class MdDialog {
140140
let strategy = this._overlay.position().global();
141141
let position = dialogConfig.position;
142142

143-
state.hasBackdrop = true;
143+
state.hasBackdrop = dialogConfig.hasBackdrop;
144+
if(dialogConfig.backdropClass && dialogConfig.backdropClass !== '') {
145+
state.backdropClass = dialogConfig.backdropClass;
146+
}
144147
state.positionStrategy = strategy;
145148

146149
if (position && (position.left || position.right)) {

0 commit comments

Comments
 (0)