Skip to content

Commit

Permalink
feat(overlay): set overlay size
Browse files Browse the repository at this point in the history
  • Loading branch information
kara committed Oct 24, 2016
1 parent add0d23 commit 03ee7eb
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/lib/core/overlay/overlay-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class OverlayRef implements PortalHost {
}

let attachResult = this._portalHost.attach(portal);
this.updateSize();
this.updatePosition();

return attachResult;
Expand Down Expand Up @@ -58,6 +59,17 @@ export class OverlayRef implements PortalHost {
}
}

/** Updates the size of the overlay based on the overlay config. */
updateSize() {
if (this._state.width || this._state.width === 0) {
this._pane.style.width = formatCssUnit(this._state.width);
}

if (this._state.height || this._state.height === 0) {
this._pane.style.height = formatCssUnit(this._state.height);
}
}

/** Attaches a backdrop for this overlay. */
private _attachBackdrop() {
this._backdropElement = document.createElement('div');
Expand Down Expand Up @@ -98,3 +110,7 @@ export class OverlayRef implements PortalHost {
}
}
}

function formatCssUnit(value: number | string) {
return typeof value === 'string' ? value as string : `${value}px`;
}
8 changes: 7 additions & 1 deletion src/lib/core/overlay/overlay-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ export class OverlayState {
/** Whether the overlay has a backdrop. */
hasBackdrop: boolean = false;

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

/** The width of the overlay panel. If a number is provided, pixel units are assumed. **/
width: number | string;

/** The height of the overlay panel. If a number is provided, pixel units are assumed. **/
height: number | string;

// TODO(jelbourn): configuration still to add
// - overlay size
// - focus trap
// - disable pointer events
// - z-index
Expand Down
51 changes: 51 additions & 0 deletions src/lib/core/overlay/overlay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,57 @@ describe('Overlay', () => {
});
});

describe('size', () => {
let state: OverlayState;

beforeEach(() => {
state = new OverlayState();
});

it('should apply the width set in the config', () => {
state.width = 500;

overlay.create(state).attach(componentPortal);
const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.width).toEqual('500px');
});

it('should support using other units if a string width is provided', () => {
state.width = '200%';

overlay.create(state).attach(componentPortal);
const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.width).toEqual('200%');
});

it('should apply the height set in the config', () => {
state.height = 500;

overlay.create(state).attach(componentPortal);
const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.height).toEqual('500px');
});

it('should support using other units if a string height is provided', () => {
state.height = '100vh';

overlay.create(state).attach(componentPortal);
const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.height).toEqual('100vh');
});

it('should support zero widths and heights', () => {
state.width = 0;
state.height = 0;

overlay.create(state).attach(componentPortal);
const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.width).toEqual('0px');
expect(pane.style.height).toEqual('0px');
});

});

describe('backdrop', () => {
let config: OverlayState;

Expand Down

0 comments on commit 03ee7eb

Please sign in to comment.