Skip to content

fix(overlay-directives): update offsets if they change after overlay creation #1981

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

Merged
merged 1 commit into from
Dec 1, 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
31 changes: 27 additions & 4 deletions src/lib/core/overlay/overlay-directives.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,21 @@ describe('Overlay directives', () => {
fixture.componentInstance.isOpen = true;
fixture.detectChanges();

// expected x value is the starting x + offset x
const expectedX = startX + 5;
const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.transform).toContain(`translateX(${expectedX}px)`);
expect(pane.style.transform)
.toContain(`translateX(${startX + 5}px)`,
`Expected overlay translateX to equal the original X + the offsetX.`);

fixture.componentInstance.isOpen = false;
fixture.detectChanges();

fixture.componentInstance.offsetX = 15;
fixture.componentInstance.isOpen = true;
fixture.detectChanges();

expect(pane.style.transform)
.toContain(`translateX(${startX + 15}px)`,
`Expected overlay directive to reflect new offsetX if it changes.`);
});

it('should set the offsetY', () => {
Expand All @@ -138,7 +149,19 @@ describe('Overlay directives', () => {
// expected y value is the starting y + trigger height + offset y
// 30 + 20 + 45 = 95px
const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.transform).toContain(`translateY(95px)`);
expect(pane.style.transform)
.toContain(`translateY(95px)`,
`Expected overlay translateY to equal the start Y + height + offsetY.`);

fixture.componentInstance.isOpen = false;
fixture.detectChanges();

fixture.componentInstance.offsetY = 55;
fixture.componentInstance.isOpen = true;
fixture.detectChanges();
expect(pane.style.transform)
.toContain(`translateY(105px)`,
`Expected overlay directive to reflect new offsetY if it changes.`);
});

});
Expand Down
30 changes: 27 additions & 3 deletions src/lib/core/overlay/overlay-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,38 @@ export class ConnectedOverlayDirective implements OnDestroy {
private _hasBackdrop = false;
private _backdropSubscription: Subscription;
private _positionSubscription: Subscription;
private _offsetX: number = 0;
private _offsetY: number = 0;
private _position: ConnectedPositionStrategy;

@Input() origin: OverlayOrigin;
@Input() positions: ConnectionPositionPair[];

/** The offset in pixels for the overlay connection point on the x-axis */
@Input() offsetX: number = 0;
@Input()
get offsetX(): number {
return this._offsetX;
}

set offsetX(offsetX: number) {
this._offsetX = offsetX;
if (this._position) {
this._position.withOffsetX(offsetX);
}
}

/** The offset in pixels for the overlay connection point on the y-axis */
@Input() offsetY: number = 0;
@Input()
get offsetY() {
return this._offsetY;
}

set offsetY(offsetY: number) {
this._offsetY = offsetY;
if (this._position) {
this._position.withOffsetY(offsetY);
}
}

/** The width of the overlay panel. */
@Input() width: number | string;
Expand Down Expand Up @@ -161,7 +184,8 @@ export class ConnectedOverlayDirective implements OnDestroy {
overlayConfig.backdropClass = this.backdropClass;
}

overlayConfig.positionStrategy = this._createPositionStrategy();
this._position = this._createPositionStrategy() as ConnectedPositionStrategy;
overlayConfig.positionStrategy = this._position;

overlayConfig.direction = this.dir;

Expand Down