Skip to content

Commit df18fd6

Browse files
committed
fix(infinite-scroll): prevent locking sibling min height while it's already locked, prevent useless setTimeout from being set up while preserve rerender scroll property isn't set
1 parent f9e7dcd commit df18fd6

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

core/src/components/infinite-scroll/infinite-scroll.tsx

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class InfiniteScroll implements ComponentInterface {
1616
private thrPx = 0;
1717
private thrPc = 0;
1818
private scrollEl?: HTMLElement;
19+
private minHeightLocked = false;
1920

2021
/**
2122
* didFire exists so that ionInfinite
@@ -141,13 +142,14 @@ export class InfiniteScroll implements ComponentInterface {
141142

142143
if (distanceFromInfinite < 0) {
143144
if (!this.didFire) {
145+
if (this.preserveRerenderScrollPosition) {
146+
// Lock the min height of the siblings of the infinite scroll
147+
// if we are preserving the rerender scroll position
148+
this.lockSiblingMinHeight(true);
149+
}
150+
144151
this.isLoading = true;
145152
this.didFire = true;
146-
147-
// Lock the min height of the siblings of the infinite scroll
148-
// if we are preserving the rerender scroll position
149-
this.lockSiblingMinHeight(true);
150-
151153
this.ionInfinite.emit();
152154
return 3;
153155
}
@@ -166,19 +168,20 @@ export class InfiniteScroll implements ComponentInterface {
166168
* has been previously set by the user when we restore after complete is called.
167169
*/
168170
private lockSiblingMinHeight(lock: boolean) {
169-
if (!this.preserveRerenderScrollPosition) {
170-
return;
171-
}
172-
173-
// Loop through all the siblings of the infinite scroll, but ignore the infinite scroll itself
174171
const siblings = this.el.parentElement?.children || [];
175172
for (const sibling of siblings) {
173+
// Loop through all the siblings of the infinite scroll, but ignore ourself
176174
if (sibling !== this.el && sibling instanceof HTMLElement) {
177175
if (lock) {
178176
const elementHeight = sibling.getBoundingClientRect().height;
179-
const previousMinHeight = sibling.style.minHeight;
180-
if (previousMinHeight) {
181-
sibling.style.setProperty('--ion-previous-min-height', previousMinHeight);
177+
if (this.minHeightLocked) {
178+
// The previous min height is from us locking it before, so we can disregard it
179+
// We still need to lock the min height if we're already locked, though, because
180+
// the user could have triggered a new load before we've finished the previous one.
181+
const previousMinHeight = sibling.style.minHeight;
182+
if (previousMinHeight) {
183+
sibling.style.setProperty('--ion-previous-min-height', previousMinHeight);
184+
}
182185
}
183186
sibling.style.minHeight = `${elementHeight}px`;
184187
} else {
@@ -188,6 +191,8 @@ export class InfiniteScroll implements ComponentInterface {
188191
}
189192
}
190193
}
194+
195+
this.minHeightLocked = lock;
191196
}
192197

193198
/**
@@ -257,9 +262,11 @@ export class InfiniteScroll implements ComponentInterface {
257262

258263
// Unlock the min height of the siblings of the infinite scroll
259264
// if we are preserving the rerender scroll position
260-
setTimeout(() => {
261-
this.lockSiblingMinHeight(false);
262-
}, 100);
265+
if (this.preserveRerenderScrollPosition) {
266+
setTimeout(() => {
267+
this.lockSiblingMinHeight(false);
268+
}, 1000);
269+
}
263270
}
264271

265272
private canStart(): boolean {

0 commit comments

Comments
 (0)