Skip to content

Commit 82c3d17

Browse files
committed
feat(overlay): set overlay size
1 parent add0d23 commit 82c3d17

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class OverlayRef implements PortalHost {
2323
}
2424

2525
let attachResult = this._portalHost.attach(portal);
26+
this.updateSize();
2627
this.updatePosition();
2728

2829
return attachResult;
@@ -58,6 +59,17 @@ export class OverlayRef implements PortalHost {
5859
}
5960
}
6061

62+
/** Updates the size of the overlay based on the overlay config. */
63+
updateSize() {
64+
if (this._state.width) {
65+
this._pane.style.width = coerceToCSSFormat(this._state.width);
66+
}
67+
68+
if (this._state.height) {
69+
this._pane.style.height = coerceToCSSFormat(this._state.height);
70+
}
71+
}
72+
6173
/** Attaches a backdrop for this overlay. */
6274
private _attachBackdrop() {
6375
this._backdropElement = document.createElement('div');
@@ -98,3 +110,7 @@ export class OverlayRef implements PortalHost {
98110
}
99111
}
100112
}
113+
114+
export function coerceToCSSFormat(value: number | string) {
115+
return typeof value === 'string' ? value as string : `${value}px`;
116+
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ export class OverlayState {
1212
/** Whether the overlay has a backdrop. */
1313
hasBackdrop: boolean = false;
1414

15+
/** Custom class to add to the backdrop **/
1516
backdropClass: string = 'md-overlay-dark-backdrop';
1617

18+
/** The width of the overlay panel. If a number is provided, pixel units are assumed. **/
19+
width: number | string;
20+
21+
/** The height of the overlay panel. If a number is provided, pixel units are assumed. **/
22+
height: number | string;
23+
1724
// TODO(jelbourn): configuration still to add
18-
// - overlay size
1925
// - focus trap
2026
// - disable pointer events
2127
// - z-index

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,46 @@ describe('Overlay', () => {
9898
});
9999
});
100100

101+
describe('size', () => {
102+
let state: OverlayState;
103+
104+
beforeEach(() => {
105+
state = new OverlayState();
106+
});
107+
108+
it('should apply the width set in the config', () => {
109+
state.width = 500;
110+
111+
overlay.create(state).attach(componentPortal);
112+
const pane = overlayContainerElement.children[0] as HTMLElement;
113+
expect(pane.style.width).toEqual('500px');
114+
});
115+
116+
it('should support using other units if a string width is provided', () => {
117+
state.width = '200%';
118+
119+
overlay.create(state).attach(componentPortal);
120+
const pane = overlayContainerElement.children[0] as HTMLElement;
121+
expect(pane.style.width).toEqual('200%');
122+
});
123+
124+
it('should apply the height set in the config', () => {
125+
state.height = 500;
126+
127+
overlay.create(state).attach(componentPortal);
128+
const pane = overlayContainerElement.children[0] as HTMLElement;
129+
expect(pane.style.height).toEqual('500px');
130+
});
131+
132+
it('should support using other units if a string height is provided', () => {
133+
state.height = '100vh';
134+
135+
overlay.create(state).attach(componentPortal);
136+
const pane = overlayContainerElement.children[0] as HTMLElement;
137+
expect(pane.style.height).toEqual('100vh');
138+
});
139+
});
140+
101141
describe('backdrop', () => {
102142
let config: OverlayState;
103143

0 commit comments

Comments
 (0)