Skip to content

Commit b36db15

Browse files
karatinayuangao
authored andcommitted
fix(overlay-directives): update offsets if they change after overlay creation (#1981)
1 parent 65bb982 commit b36db15

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,21 @@ describe('Overlay directives', () => {
119119
fixture.componentInstance.isOpen = true;
120120
fixture.detectChanges();
121121

122-
// expected x value is the starting x + offset x
123-
const expectedX = startX + 5;
124122
const pane = overlayContainerElement.children[0] as HTMLElement;
125-
expect(pane.style.transform).toContain(`translateX(${expectedX}px)`);
123+
expect(pane.style.transform)
124+
.toContain(`translateX(${startX + 5}px)`,
125+
`Expected overlay translateX to equal the original X + the offsetX.`);
126+
127+
fixture.componentInstance.isOpen = false;
128+
fixture.detectChanges();
129+
130+
fixture.componentInstance.offsetX = 15;
131+
fixture.componentInstance.isOpen = true;
132+
fixture.detectChanges();
133+
134+
expect(pane.style.transform)
135+
.toContain(`translateX(${startX + 15}px)`,
136+
`Expected overlay directive to reflect new offsetX if it changes.`);
126137
});
127138

128139
it('should set the offsetY', () => {
@@ -138,7 +149,19 @@ describe('Overlay directives', () => {
138149
// expected y value is the starting y + trigger height + offset y
139150
// 30 + 20 + 45 = 95px
140151
const pane = overlayContainerElement.children[0] as HTMLElement;
141-
expect(pane.style.transform).toContain(`translateY(95px)`);
152+
expect(pane.style.transform)
153+
.toContain(`translateY(95px)`,
154+
`Expected overlay translateY to equal the start Y + height + offsetY.`);
155+
156+
fixture.componentInstance.isOpen = false;
157+
fixture.detectChanges();
158+
159+
fixture.componentInstance.offsetY = 55;
160+
fixture.componentInstance.isOpen = true;
161+
fixture.detectChanges();
162+
expect(pane.style.transform)
163+
.toContain(`translateY(105px)`,
164+
`Expected overlay directive to reflect new offsetY if it changes.`);
142165
});
143166

144167
});

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,38 @@ export class ConnectedOverlayDirective implements OnDestroy {
6767
private _hasBackdrop = false;
6868
private _backdropSubscription: Subscription;
6969
private _positionSubscription: Subscription;
70+
private _offsetX: number = 0;
71+
private _offsetY: number = 0;
72+
private _position: ConnectedPositionStrategy;
7073

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

7477
/** The offset in pixels for the overlay connection point on the x-axis */
75-
@Input() offsetX: number = 0;
78+
@Input()
79+
get offsetX(): number {
80+
return this._offsetX;
81+
}
82+
83+
set offsetX(offsetX: number) {
84+
this._offsetX = offsetX;
85+
if (this._position) {
86+
this._position.withOffsetX(offsetX);
87+
}
88+
}
7689

7790
/** The offset in pixels for the overlay connection point on the y-axis */
78-
@Input() offsetY: number = 0;
91+
@Input()
92+
get offsetY() {
93+
return this._offsetY;
94+
}
95+
96+
set offsetY(offsetY: number) {
97+
this._offsetY = offsetY;
98+
if (this._position) {
99+
this._position.withOffsetY(offsetY);
100+
}
101+
}
79102

80103
/** The width of the overlay panel. */
81104
@Input() width: number | string;
@@ -163,7 +186,8 @@ export class ConnectedOverlayDirective implements OnDestroy {
163186
overlayConfig.backdropClass = this.backdropClass;
164187
}
165188

166-
overlayConfig.positionStrategy = this._createPositionStrategy();
189+
this._position = this._createPositionStrategy() as ConnectedPositionStrategy;
190+
overlayConfig.positionStrategy = this._position;
167191

168192
overlayConfig.direction = this.dir;
169193

0 commit comments

Comments
 (0)