@@ -26,6 +26,11 @@ export class IronListAdapter {
2626 // Iron-list uses this value to determine how many pages of elements to render
2727 this . _maxPages = 1.3 ;
2828
29+ // Placeholder height (used for sizing elements that have intrinsic 0 height after update)
30+ this . __placeholderHeight = 200 ;
31+ // A queue of 10 previous element heights
32+ this . __elementHeightQueue = Array ( 10 ) ;
33+
2934 this . timeouts = {
3035 SCROLL_REORDER : 500 ,
3136 IGNORE_WHEEL : 500 ,
@@ -141,12 +146,22 @@ export class IronListAdapter {
141146 el . __lastUpdatedIndex = index ;
142147 }
143148
144- if ( el . offsetHeight === 0 ) {
149+ const elementHeight = el . offsetHeight ;
150+ if ( elementHeight === 0 ) {
145151 // If the elements have 0 height after update (for example due to lazy rendering),
146152 // it results in iron-list requesting to create an unlimited count of elements.
147153 // Assign a temporary placeholder sizing to elements that would otherwise end up having
148154 // no height.
149- el . style . paddingTop = '200px' ;
155+ el . style . paddingTop = `${ this . __placeholderHeight } px` ;
156+ } else {
157+ // Add element height to the queue
158+ this . __elementHeightQueue . push ( elementHeight ) ;
159+ this . __elementHeightQueue . shift ( ) ;
160+
161+ // Calcualte new placeholder height based on the average of the defined values in the
162+ // element height queue
163+ const filteredHeights = this . __elementHeightQueue . filter ( ( h ) => h !== undefined ) ;
164+ this . __placeholderHeight = Math . round ( filteredHeights . reduce ( ( a , b ) => a + b , 0 ) / filteredHeights . length ) ;
150165 }
151166 }
152167
@@ -297,16 +312,28 @@ export class IronListAdapter {
297312
298313 _scrollHandler ( ) {
299314 this . _adjustVirtualIndexOffset ( this . _scrollTop - ( this . __previousScrollTop || 0 ) ) ;
315+ const delta = this . scrollTarget . scrollTop - this . _scrollPosition ;
300316
301317 super . _scrollHandler ( ) ;
302318
303319 if ( this . _physicalCount !== 0 ) {
304- // After running super._scrollHandler, fix _virtualStart to workaround an iron-list issue.
305- // See https://github.com/vaadin/web-components/issues/1691
306- const reusables = this . _getReusables ( true ) ;
307- this . _physicalTop = reusables . physicalTop ;
308- this . _virtualStart += reusables . indexes . length ;
309- this . _physicalStart += reusables . indexes . length ;
320+ const isScrollingDown = delta >= 0 ;
321+ const reusables = this . _getReusables ( ! isScrollingDown ) ;
322+
323+ if ( reusables . indexes . length ) {
324+ // After running super._scrollHandler, fix internal properties to workaround an iron-list issue.
325+ // See https://github.com/vaadin/web-components/issues/1691
326+ this . _physicalTop = reusables . physicalTop ;
327+
328+ if ( isScrollingDown ) {
329+ this . _virtualStart -= reusables . indexes . length ;
330+ this . _physicalStart -= reusables . indexes . length ;
331+ } else {
332+ this . _virtualStart += reusables . indexes . length ;
333+ this . _physicalStart += reusables . indexes . length ;
334+ }
335+ this . _resizeHandler ( ) ;
336+ }
310337 }
311338
312339 if ( this . reorderElements ) {
0 commit comments