Skip to content

Commit 0b54668

Browse files
karajelbourn
authored andcommitted
feat(overlay): add custom classes for backdrop (#1532)
1 parent 25c7fd5 commit 0b54668

File tree

6 files changed

+59
-5
lines changed

6 files changed

+59
-5
lines changed

src/demo-app/overlay/overlay-demo.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@
2525
<template portal>
2626
<p class="demo-fusilli"> Fusilli </p>
2727
</template>
28+
29+
<button (click)="openPanelWithBackdrop()">Backdrop panel</button>

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ export class OverlayDemo {
7474
let overlayRef = this.overlay.create(config);
7575
overlayRef.attach(new ComponentPortal(SpagettiPanel, this.viewContainerRef));
7676
}
77+
78+
openPanelWithBackdrop() {
79+
let config = new OverlayState();
80+
81+
config.positionStrategy = this.overlay.position()
82+
.global()
83+
.centerHorizontally();
84+
config.hasBackdrop = true;
85+
config.backdropClass = 'md-overlay-transparent-backdrop';
86+
87+
let overlayRef = this.overlay.create(config);
88+
overlayRef.attach(this.templatePortals.first);
89+
overlayRef.backdropClick().subscribe(() => overlayRef.detach());
90+
}
91+
7792
}
7893

7994
/** Simple component to load into an overlay */

src/lib/core/overlay/overlay-ref.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export class OverlayRef implements PortalHost {
6262
private _attachBackdrop() {
6363
this._backdropElement = document.createElement('div');
6464
this._backdropElement.classList.add('md-overlay-backdrop');
65+
this._backdropElement.classList.add(this._state.backdropClass);
66+
6567
this._pane.parentElement.appendChild(this._backdropElement);
6668

6769
// Forward backdrop clicks such that the consumer of the overlay can perform whatever
@@ -82,6 +84,7 @@ export class OverlayRef implements PortalHost {
8284

8385
if (backdropToDetach) {
8486
backdropToDetach.classList.remove('md-overlay-backdrop-showing');
87+
backdropToDetach.classList.remove(this._state.backdropClass);
8588
backdropToDetach.addEventListener('transitionend', () => {
8689
backdropToDetach.parentNode.removeChild(backdropToDetach);
8790

src/lib/core/overlay/overlay-state.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export class OverlayState {
1212
/** Whether the overlay has a backdrop. */
1313
hasBackdrop: boolean = false;
1414

15+
backdropClass: string = 'md-overlay-dark-backdrop';
16+
1517
// TODO(jelbourn): configuration still to add
1618
// - overlay size
1719
// - focus trap

src/lib/core/overlay/overlay.scss

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
@mixin md-overlay() {
7-
$md-backdrop-color: md-color($md-grey, 900);
7+
$md-dark-backdrop-color: md-color($md-grey, 900);
88

99
// TODO(jelbourn): change from the `md` prefix to something else for everything in the toolkit.
1010

@@ -45,11 +45,18 @@
4545
// TODO(jelbourn): figure out if there are actually spec'ed colors for both light and dark
4646
// themes here. Currently using the values from Angular Material 1.
4747
transition: opacity $swift-ease-out-duration $swift-ease-out-timing-function;
48-
background: $md-backdrop-color;
4948
opacity: 0;
5049
}
5150

5251
.md-overlay-backdrop.md-overlay-backdrop-showing {
5352
opacity: 0.48;
5453
}
54+
55+
.md-overlay-dark-backdrop {
56+
background: $md-dark-backdrop-color;
57+
}
58+
59+
.md-overlay-transparent-backdrop {
60+
background: none;
61+
}
5562
}

src/lib/core/overlay/overlay.spec.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,49 @@ describe('Overlay', () => {
9999
});
100100

101101
describe('backdrop', () => {
102-
it('should create and destroy an overlay backdrop', () => {
103-
let config = new OverlayState();
102+
let config: OverlayState;
103+
104+
beforeEach(() => {
105+
config = new OverlayState();
104106
config.hasBackdrop = true;
107+
});
105108

109+
it('should create and destroy an overlay backdrop', () => {
106110
let overlayRef = overlay.create(config);
107111
overlayRef.attach(componentPortal);
108112

109113
viewContainerFixture.detectChanges();
110114
let backdrop = <HTMLElement> overlayContainerElement.querySelector('.md-overlay-backdrop');
111115
expect(backdrop).toBeTruthy();
112-
expect(backdrop.classList).not.toContain('.md-overlay-backdrop-showing');
116+
expect(backdrop.classList).not.toContain('md-overlay-backdrop-showing');
113117

114118
let backdropClickHandler = jasmine.createSpy('backdropClickHander');
115119
overlayRef.backdropClick().subscribe(backdropClickHandler);
116120

117121
backdrop.click();
118122
expect(backdropClickHandler).toHaveBeenCalled();
119123
});
124+
125+
it('should apply the default overlay backdrop class', () => {
126+
let overlayRef = overlay.create(config);
127+
overlayRef.attach(componentPortal);
128+
viewContainerFixture.detectChanges();
129+
130+
let backdrop = <HTMLElement> overlayContainerElement.querySelector('.md-overlay-backdrop');
131+
expect(backdrop.classList).toContain('md-overlay-dark-backdrop');
132+
});
133+
134+
it('should apply a custom overlay backdrop class', () => {
135+
config.backdropClass = 'md-overlay-transparent-backdrop';
136+
137+
let overlayRef = overlay.create(config);
138+
overlayRef.attach(componentPortal);
139+
viewContainerFixture.detectChanges();
140+
141+
let backdrop = <HTMLElement> overlayContainerElement.querySelector('.md-overlay-backdrop');
142+
expect(backdrop.classList).toContain('md-overlay-transparent-backdrop');
143+
});
144+
120145
});
121146
});
122147

0 commit comments

Comments
 (0)