Skip to content

Commit 4934305

Browse files
fix: adjust end property value on viewport resize (#4695)
Co-authored-by: web-padawan <iamkulykov@gmail.com>
1 parent 99ea663 commit 4934305

2 files changed

Lines changed: 61 additions & 12 deletions

File tree

‎packages/vaadin-overlay/src/vaadin-overlay-position-mixin.js‎

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,6 @@ export const PositionMixin = (superClass) =>
191191
return;
192192
}
193193

194-
// Cleanup styles to avoid wrong values after window resize
195-
// See https://github.com/vaadin/web-components/issues/4604
196-
Object.assign(this.style, {
197-
top: '',
198-
right: '',
199-
bottom: '',
200-
left: '',
201-
justifyContent: '',
202-
alignItems: '',
203-
});
204-
205194
const targetRect = this.positionTarget.getBoundingClientRect();
206195

207196
// Detect the desired alignment and update the layout accordingly
@@ -301,22 +290,67 @@ export const PositionMixin = (superClass) =>
301290
return defaultAlignStart === shouldGoToDefaultSide;
302291
}
303292

293+
/**
294+
* Returns an adjusted value after resizing the browser window,
295+
* to avoid wrong calculations when e.g. previously set `bottom`
296+
* CSS property value is larger than the updated viewport height.
297+
* See https://github.com/vaadin/web-components/issues/4604
298+
*/
299+
__adjustBottomProperty(cssPropNameToSet, propNames, currentValue) {
300+
let adjustedProp;
301+
302+
if (cssPropNameToSet === propNames.end) {
303+
// Adjust horizontally
304+
if (propNames.end === PROP_NAMES_VERTICAL.end) {
305+
const viewportHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
306+
307+
if (currentValue > viewportHeight && this.__oldViewportHeight) {
308+
const heightDiff = this.__oldViewportHeight - viewportHeight;
309+
adjustedProp = currentValue - heightDiff;
310+
}
311+
312+
this.__oldViewportHeight = viewportHeight;
313+
}
314+
315+
// Adjust vertically
316+
if (propNames.end === PROP_NAMES_HORIZONTAL.end) {
317+
const viewportWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
318+
319+
if (currentValue > viewportWidth && this.__oldViewportWidth) {
320+
const widthDiff = this.__oldViewportWidth - viewportWidth;
321+
adjustedProp = currentValue - widthDiff;
322+
}
323+
324+
this.__oldViewportWidth = viewportWidth;
325+
}
326+
}
327+
328+
return adjustedProp;
329+
}
330+
304331
/**
305332
* Returns an object with CSS position properties to set,
306333
* e.g. { top: "100px" }
307334
*/
308335
// eslint-disable-next-line max-params
309336
__calculatePositionInOneDimension(targetRect, overlayRect, noOverlap, propNames, overlay, shouldAlignStart) {
310337
const cssPropNameToSet = shouldAlignStart ? propNames.start : propNames.end;
338+
const cssPropNameToClear = shouldAlignStart ? propNames.end : propNames.start;
311339

312340
const currentValue = parseFloat(overlay.style[cssPropNameToSet] || getComputedStyle(overlay)[cssPropNameToSet]);
341+
const adjustedValue = this.__adjustBottomProperty(cssPropNameToSet, propNames, currentValue);
313342

314343
const diff =
315344
overlayRect[shouldAlignStart ? propNames.start : propNames.end] -
316345
targetRect[noOverlap === shouldAlignStart ? propNames.end : propNames.start];
317346

347+
const valueToSet = adjustedValue
348+
? `${adjustedValue}px`
349+
: `${currentValue + diff * (shouldAlignStart ? -1 : 1)}px`;
350+
318351
return {
319-
[cssPropNameToSet]: `${currentValue + diff * (shouldAlignStart ? -1 : 1)}px`,
352+
[cssPropNameToSet]: valueToSet,
353+
[cssPropNameToClear]: '',
320354
};
321355
}
322356
};

‎packages/vaadin-overlay/test/position-mixin.test.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ describe('position mixin', () => {
9696
expectEdgesAligned(LEFT, LEFT);
9797
});
9898

99+
it('should not lose scroll position on overlay position update', () => {
100+
// Move the target to the center of the viewport
101+
target.style.top = '50vh';
102+
// Increase the height of the child to make the overlay scrollable
103+
overlay.querySelector('#overlay-child').style.height = '100vh';
104+
105+
// Scroll the overlay
106+
overlay.$.overlay.scrollTop = 100;
107+
108+
updatePosition();
109+
110+
// Expect the overlay to be scrolled to the same position
111+
expect(overlay.$.overlay.scrollTop).to.equal(100);
112+
});
113+
99114
describe('vertical align top', () => {
100115
beforeEach(() => {
101116
overlay.verticalAlign = TOP;

0 commit comments

Comments
 (0)