@@ -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 } ;
0 commit comments