Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(overlay): set overlay size #1583

Merged
merged 1 commit into from
Oct 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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