@@ -157,28 +157,35 @@ export class InfiniteScroll implements ComponentInterface {
157157 return 4 ;
158158 } ;
159159
160+ /**
161+ * Loop through our sibling elements and lock or unlock their min height.
162+ * This keeps our siblings, for example `ion-list`, the same height as their
163+ * content currently is, so when it loads new data and the DOM removes the old
164+ * data, the height of the container doesn't change and we don't lose our scroll position.
165+ *
166+ * We preserve existing min-height values, if they're set, so we don't erase what
167+ * has been previously set by the user when we restore after complete is called.
168+ */
160169 private lockSiblingMinHeight ( lock : boolean ) {
161170 if ( ! this . preserveRerenderScrollPosition ) {
162171 return ;
163172 }
164173
165174 // Loop through all the siblings of the infinite scroll, but ignore the infinite scroll itself
166- const siblings = this . el . parentElement ?. children ;
167- if ( siblings ) {
168- for ( const sibling of siblings ) {
169- if ( sibling !== this . el && sibling instanceof HTMLElement ) {
170- if ( lock ) {
171- const elementHeight = sibling . getBoundingClientRect ( ) . height ;
172- const previousMinHeight = sibling . style . minHeight ;
173- if ( previousMinHeight ) {
174- sibling . style . setProperty ( '--ion-previous-min-height' , previousMinHeight ) ;
175- }
176- sibling . style . minHeight = `${ elementHeight } px` ;
177- } else {
178- const previousMinHeight = sibling . style . getPropertyValue ( '--ion-previous-min-height' ) ;
179- sibling . style . minHeight = previousMinHeight || 'auto' ;
180- sibling . style . removeProperty ( '--ion-previous-min-height' ) ;
175+ const siblings = this . el . parentElement ?. children || [ ] ;
176+ for ( const sibling of siblings ) {
177+ if ( sibling !== this . el && sibling instanceof HTMLElement ) {
178+ if ( lock ) {
179+ const elementHeight = sibling . getBoundingClientRect ( ) . height ;
180+ const previousMinHeight = sibling . style . minHeight ;
181+ if ( previousMinHeight ) {
182+ sibling . style . setProperty ( '--ion-previous-min-height' , previousMinHeight ) ;
181183 }
184+ sibling . style . minHeight = `${ elementHeight } px` ;
185+ } else {
186+ const previousMinHeight = sibling . style . getPropertyValue ( '--ion-previous-min-height' ) ;
187+ sibling . style . minHeight = previousMinHeight || 'auto' ;
188+ sibling . style . removeProperty ( '--ion-previous-min-height' ) ;
182189 }
183190 }
184191 }
0 commit comments